Skip to content

Commit ec9e132

Browse files
committed
feat: object storage add GetSignURL
Signed-off-by: Gaius <[email protected]>
1 parent 3f6f100 commit ec9e132

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

client/daemon/objectstorage/objectstorage.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ func (o *objectStorage) getObject(ctx *gin.Context) {
146146
urlMeta.Digest = meta.Digest
147147

148148
taskID := idgen.TaskID(ctx.Request.URL.String(), urlMeta)
149+
o.peeTaskManager.StartStreamTask(ctx, &peer.StreamTaskRequest{
150+
URL: url,
151+
URLMeta: meta,
152+
Range: rg,
153+
PeerID: peerID,
154+
},
155+
)
149156
}
150157

151158
// createObject uses to upload object data.

pkg/objectstorage/constants.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,26 @@ const (
2828
// MetaDigest is key of digest meta.
2929
MetaDigest = "digest"
3030
)
31+
32+
// Method is the client operation method .
33+
type Method string
34+
35+
const (
36+
// MethodHead is the head operation.
37+
MethodHead Method = "HEAD"
38+
39+
// MethodGet is the get operation.
40+
MethodGet Method = "GET"
41+
42+
// MethodPut is the put operation.
43+
MethodPut Method = "PUT"
44+
45+
// MethodPost is the post operation.
46+
MethodPost Method = "POST"
47+
48+
// MethodDelete is the delete operation.
49+
MethodDelete Method = "Delete"
50+
51+
// MethodList is the list operation.
52+
MethodList Method = "List"
53+
)

pkg/objectstorage/objectstorage.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ type ObjectStorage interface {
8686

8787
// ListObjectMetadatas returns metadata of objects.
8888
ListObjectMetadatas(ctx context.Context, bucketName, prefix, marker string, limit int64) ([]*ObjectMetadata, error)
89+
90+
// GetSignURL returns sign url of object.
91+
GetSignURL(ctx context.Context, bucketName, objectKey string, method Method, expire time.Duration) (string, error)
8992
}
9093

9194
// New object storage interface.

pkg/objectstorage/oss.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"strconv"
24+
"time"
2425

2526
aliyunoss "github.com/aliyun/aliyun-oss-go-sdk/oss"
2627
"github.com/go-http-utils/headers"
@@ -166,3 +167,31 @@ func (o *oss) ListObjectMetadatas(ctx context.Context, bucketName, prefix, marke
166167

167168
return metadatas, nil
168169
}
170+
171+
// GetSignURL returns sign url of object.
172+
func (o *oss) GetSignURL(ctx context.Context, bucketName, objectKey string, method Method, expire time.Duration) (string, error) {
173+
var ossHTTPMethod aliyunoss.HTTPMethod
174+
switch method {
175+
case MethodGet:
176+
ossHTTPMethod = aliyunoss.HTTPGet
177+
case MethodPut:
178+
ossHTTPMethod = aliyunoss.HTTPPut
179+
case MethodHead:
180+
ossHTTPMethod = aliyunoss.HTTPHead
181+
case MethodPost:
182+
ossHTTPMethod = aliyunoss.HTTPPost
183+
case MethodDelete:
184+
ossHTTPMethod = aliyunoss.HTTPDelete
185+
case MethodList:
186+
ossHTTPMethod = aliyunoss.HTTPGet
187+
default:
188+
return "", fmt.Errorf("not support method %s", method)
189+
}
190+
191+
bucket, err := o.client.Bucket(bucketName)
192+
if err != nil {
193+
return "", err
194+
}
195+
196+
return bucket.SignURL(objectKey, ossHTTPMethod, int64(expire.Seconds()))
197+
}

pkg/objectstorage/s3.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23+
"time"
2324

2425
"github.com/aws/aws-sdk-go/aws"
2526
"github.com/aws/aws-sdk-go/aws/credentials"
27+
"github.com/aws/aws-sdk-go/aws/request"
2628
"github.com/aws/aws-sdk-go/aws/session"
2729
awss3 "github.com/aws/aws-sdk-go/service/s3"
2830
)
@@ -169,3 +171,38 @@ func (s *s3) ListObjectMetadatas(ctx context.Context, bucketName, prefix, marker
169171

170172
return metadatas, nil
171173
}
174+
175+
// GetSignURL returns sign url of object.
176+
func (s *s3) GetSignURL(ctx context.Context, bucketName, objectKey string, method Method, expire time.Duration) (string, error) {
177+
var req *request.Request
178+
switch method {
179+
case MethodGet:
180+
req, _ = s.client.GetObjectRequest(&awss3.GetObjectInput{
181+
Bucket: aws.String(bucketName),
182+
Key: aws.String(objectKey),
183+
})
184+
case MethodPut:
185+
req, _ = s.client.PutObjectRequest(&awss3.PutObjectInput{
186+
Bucket: aws.String(bucketName),
187+
Key: aws.String(objectKey),
188+
})
189+
case MethodHead:
190+
req, _ = s.client.HeadObjectRequest(&awss3.HeadObjectInput{
191+
Bucket: aws.String(bucketName),
192+
Key: aws.String(objectKey),
193+
})
194+
case MethodDelete:
195+
req, _ = s.client.DeleteObjectRequest(&awss3.DeleteObjectInput{
196+
Bucket: aws.String(bucketName),
197+
Key: aws.String(objectKey),
198+
})
199+
case MethodList:
200+
req, _ = s.client.ListObjectsRequest(&awss3.ListObjectsInput{
201+
Bucket: aws.String(bucketName),
202+
})
203+
default:
204+
return "", fmt.Errorf("not support method %s", method)
205+
}
206+
207+
return req.Presign(expire)
208+
}

0 commit comments

Comments
 (0)