Feat: cos policy upload/download in server-side

This commit is contained in:
HFO4
2020-01-25 11:43:13 +08:00
parent c34f211d7e
commit 4abd5b2346
4 changed files with 67 additions and 11 deletions

View File

@@ -79,6 +79,7 @@ func TestDriver_Source(t *testing.T) {
SecretKey: "sk",
BucketName: "test",
Server: "test.com",
IsPrivate: true,
},
}
@@ -138,6 +139,18 @@ func TestDriver_Source(t *testing.T) {
asserts.EqualValues("838860800", query.Get("x-oss-traffic-limit"))
asserts.NotEmpty(query.Get("response-content-disposition"))
}
// 公共空间
{
handler.Policy.IsPrivate = false
res, err := handler.Source(context.Background(), "/123", url.URL{}, 10, false, 0)
asserts.NoError(err)
resURL, err := url.Parse(res)
asserts.NoError(err)
query := resURL.Query()
asserts.Empty(query.Get("Signature"))
asserts.Empty(query.Get("Expires"))
}
}
func TestDriver_Thumb(t *testing.T) {

View File

@@ -236,10 +236,21 @@ func (handler Driver) Source(
}
func (handler Driver) signSourceURL(ctx context.Context, path string, ttl int64, options []oss.Option) (string, error) {
// 是否带有 Version ID
if _, ok := ctx.Value(VersionID).(int64); ok {
cdnURL, err := url.Parse(handler.Policy.BaseURL)
if err != nil {
return "", err
}
// 公有空间不需要签名
if !handler.Policy.IsPrivate {
file, err := url.Parse(path)
if err != nil {
return "", err
}
sourceURL := cdnURL.ResolveReference(file)
return sourceURL.String(), nil
}
signedURL, err := handler.bucket.SignURL(path, oss.HTTPGet, ttl, options...)
if err != nil {
return "", err
@@ -250,10 +261,6 @@ func (handler Driver) signSourceURL(ctx context.Context, path string, ttl int64,
if err != nil {
return "", err
}
cdnURL, err := url.Parse(handler.Policy.BaseURL)
if err != nil {
return "", err
}
finalURL.Host = cdnURL.Host
finalURL.Scheme = cdnURL.Scheme