Browse Source

增加通过ID下载文件的预签名接口

feature_gxh
Sydonian 8 months ago
parent
commit
1589ed2406
3 changed files with 47 additions and 8 deletions
  1. +14
    -1
      sdks/storage/cdsapi/presigned.go
  2. +32
    -6
      sdks/storage/cdsapi/presigned_test.go
  3. +1
    -1
      sdks/storage/cdsapi/signer.go

+ 14
- 1
sdks/storage/cdsapi/presigned.go View File

@@ -33,10 +33,23 @@ type PresignedObjectDownloadByPath struct {
Length *int64 `form:"length" url:"length,omitempty"` Length *int64 `form:"length" url:"length,omitempty"`
} }


func (c *PresignedService) ObjectDownload(req PresignedObjectDownloadByPath, expireIn int) (string, error) {
func (c *PresignedService) ObjectDownloadByPath(req PresignedObjectDownloadByPath, expireIn int) (string, error) {
return c.presign(req, PresignedObjectDownloadByPathPath, http.MethodGet, expireIn) return c.presign(req, PresignedObjectDownloadByPathPath, http.MethodGet, expireIn)
} }


const PresignedObjectDownloadPath = "/v1/presigned/object/download"

type PresignedObjectDownload struct {
UserID cdssdk.UserID `form:"userID" url:"userID" binding:"required"`
ObjectID cdssdk.ObjectID `form:"objectID" url:"objectID" binding:"required"`
Offset int64 `form:"offset" url:"offset,omitempty"`
Length *int64 `form:"length" url:"length,omitempty"`
}

func (c *PresignedService) ObjectDownload(req PresignedObjectDownload, expireIn int) (string, error) {
return c.presign(req, PresignedObjectDownloadPath, http.MethodGet, expireIn)
}

const PresignedObjectUploadPath = "/v1/presigned/object/upload" const PresignedObjectUploadPath = "/v1/presigned/object/upload"


type PresignedObjectUpload struct { type PresignedObjectUpload struct {


+ 32
- 6
sdks/storage/cdsapi/presigned_test.go View File

@@ -16,7 +16,7 @@ func Test_Presigned(t *testing.T) {


Convey("下载文件", t, func() { Convey("下载文件", t, func() {
pre := cli.Presigned() pre := cli.Presigned()
url, err := pre.ObjectDownload(PresignedObjectDownloadByPath{
url, err := pre.ObjectDownloadByPath(PresignedObjectDownloadByPath{
UserID: 1, UserID: 1,
PackageID: 3, PackageID: 3,
Path: "example.java", Path: "example.java",
@@ -39,14 +39,16 @@ func Test_Presigned(t *testing.T) {
}) })
} }


func Test_PresignedObjectDownload(t *testing.T) {
func Test_PresignedObjectDownloadByPath(t *testing.T) {
cli := NewClient(&Config{ cli := NewClient(&Config{
URL: "http://localhost:7890",
URL: "http://localhost:7890",
AccessKey: "123456",
SecretKey: "123456",
}) })


Convey("下载文件", t, func() { Convey("下载文件", t, func() {
pre := cli.Presigned() pre := cli.Presigned()
url, err := pre.ObjectDownload(PresignedObjectDownloadByPath{
url, err := pre.ObjectDownloadByPath(PresignedObjectDownloadByPath{
UserID: 1, UserID: 1,
PackageID: 3, PackageID: 3,
Path: "example.java", Path: "example.java",
@@ -58,6 +60,26 @@ func Test_PresignedObjectDownload(t *testing.T) {
}) })
} }


func Test_PresignedObjectDownload(t *testing.T) {
cli := NewClient(&Config{
URL: "http://localhost:7890",
AccessKey: "123456",
SecretKey: "123456",
})

Convey("下载文件", t, func() {
pre := cli.Presigned()
url, err := pre.ObjectDownload(PresignedObjectDownload{
UserID: 1,
ObjectID: 1039,
// Offset: 1,
// Length: types.Ref(int64(100)),
}, 100)
So(err, ShouldEqual, nil)
t.Logf("url: %s", url)
})
}

func Test_PresignedObjectUpload(t *testing.T) { func Test_PresignedObjectUpload(t *testing.T) {
cli := NewClient(&Config{ cli := NewClient(&Config{
URL: "http://localhost:7890", URL: "http://localhost:7890",
@@ -94,7 +116,9 @@ func Test_PresignedNewMultipartUpload(t *testing.T) {


func Test_PresignedObjectUploadPart(t *testing.T) { func Test_PresignedObjectUploadPart(t *testing.T) {
cli := NewClient(&Config{ cli := NewClient(&Config{
URL: "http://localhost:7890",
URL: "http://localhost:7890",
AccessKey: "123456",
SecretKey: "123456",
}) })


Convey("上传分片", t, func() { Convey("上传分片", t, func() {
@@ -111,7 +135,9 @@ func Test_PresignedObjectUploadPart(t *testing.T) {


func Test_PresignedCompleteMultipartUpload(t *testing.T) { func Test_PresignedCompleteMultipartUpload(t *testing.T) {
cli := NewClient(&Config{ cli := NewClient(&Config{
URL: "http://localhost:7890",
URL: "http://localhost:7890",
AccessKey: "123456",
SecretKey: "123456",
}) })


Convey("合并分片", t, func() { Convey("合并分片", t, func() {


+ 1
- 1
sdks/storage/cdsapi/signer.go View File

@@ -95,7 +95,7 @@ func SignWithPayloadHash(req *http.Request, payloadHash string, accessKey, secre
// //
// expiration为签名过期时间,单位为秒。 // expiration为签名过期时间,单位为秒。
// //
// 签名时不会包含请求体的哈希值。
// 签名时不会包含请求体的哈希值。注:不要设置任何额外的Header(除了自动添加的Host),以免签名校验不通过
func Presign(req *http.Request, accessKey, secretKey string, expiration int) (string, error) { func Presign(req *http.Request, accessKey, secretKey string, expiration int) (string, error) {
query := req.URL.Query() query := req.URL.Query()
query.Add("X-Expires", fmt.Sprintf("%v", expiration)) query.Add("X-Expires", fmt.Sprintf("%v", expiration))


Loading…
Cancel
Save