diff --git a/sdks/storage/cdsapi/presigned.go b/sdks/storage/cdsapi/presigned.go index 5e755fb..b357e71 100644 --- a/sdks/storage/cdsapi/presigned.go +++ b/sdks/storage/cdsapi/presigned.go @@ -33,10 +33,23 @@ type PresignedObjectDownloadByPath struct { 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) } +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" type PresignedObjectUpload struct { diff --git a/sdks/storage/cdsapi/presigned_test.go b/sdks/storage/cdsapi/presigned_test.go index 37c5b87..f4071c9 100644 --- a/sdks/storage/cdsapi/presigned_test.go +++ b/sdks/storage/cdsapi/presigned_test.go @@ -16,7 +16,7 @@ func Test_Presigned(t *testing.T) { Convey("下载文件", t, func() { pre := cli.Presigned() - url, err := pre.ObjectDownload(PresignedObjectDownloadByPath{ + url, err := pre.ObjectDownloadByPath(PresignedObjectDownloadByPath{ UserID: 1, PackageID: 3, 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{ - URL: "http://localhost:7890", + URL: "http://localhost:7890", + AccessKey: "123456", + SecretKey: "123456", }) Convey("下载文件", t, func() { pre := cli.Presigned() - url, err := pre.ObjectDownload(PresignedObjectDownloadByPath{ + url, err := pre.ObjectDownloadByPath(PresignedObjectDownloadByPath{ UserID: 1, PackageID: 3, 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) { cli := NewClient(&Config{ URL: "http://localhost:7890", @@ -94,7 +116,9 @@ func Test_PresignedNewMultipartUpload(t *testing.T) { func Test_PresignedObjectUploadPart(t *testing.T) { cli := NewClient(&Config{ - URL: "http://localhost:7890", + URL: "http://localhost:7890", + AccessKey: "123456", + SecretKey: "123456", }) Convey("上传分片", t, func() { @@ -111,7 +135,9 @@ func Test_PresignedObjectUploadPart(t *testing.T) { func Test_PresignedCompleteMultipartUpload(t *testing.T) { cli := NewClient(&Config{ - URL: "http://localhost:7890", + URL: "http://localhost:7890", + AccessKey: "123456", + SecretKey: "123456", }) Convey("合并分片", t, func() { diff --git a/sdks/storage/cdsapi/signer.go b/sdks/storage/cdsapi/signer.go index d0bea77..67e39e5 100644 --- a/sdks/storage/cdsapi/signer.go +++ b/sdks/storage/cdsapi/signer.go @@ -95,7 +95,7 @@ func SignWithPayloadHash(req *http.Request, payloadHash string, accessKey, secre // // expiration为签名过期时间,单位为秒。 // -// 签名时不会包含请求体的哈希值。 +// 签名时不会包含请求体的哈希值。注:不要设置任何额外的Header(除了自动添加的Host),以免签名校验不通过 func Presign(req *http.Request, accessKey, secretKey string, expiration int) (string, error) { query := req.URL.Query() query.Add("X-Expires", fmt.Sprintf("%v", expiration))