Skip to content

Commit

Permalink
Update Aliyun oss sdk to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
duxphp committed Aug 9, 2024
1 parent ab80e84 commit ca6f45f
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 73 deletions.
60 changes: 28 additions & 32 deletions drivers/ossStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package drivers
import (
"context"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
"io"
"net/url"
"strings"
Expand All @@ -15,11 +16,12 @@ type OssStorage struct {
BucketName string
}

func NewOssStorage(accessKeyId, accessKeySecret, endpoint, bucketName, domain string) *OssStorage {
client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
if err != nil {
return nil
}
func NewOssStorage(accessKeyId, accessKeySecret, endpoint, bucketName, domain, region string) *OssStorage {

provider := credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret)
cfg := oss.LoadDefaultConfig().WithCredentialsProvider(provider).WithEndpoint(endpoint).WithRegion(region)
client := oss.NewClient(cfg)

return &OssStorage{
Client: client,
Domain: domain,
Expand All @@ -32,15 +34,11 @@ func (ofs *OssStorage) Write(ctx context.Context, path string, contents string,
}

func (ofs *OssStorage) WriteStream(ctx context.Context, path string, stream io.Reader, config map[string]interface{}) error {
bucket, err := ofs.Client.Bucket(ofs.BucketName)
if err != nil {
return err
}
options := []oss.Option{}
if val, ok := config["Content-Type"]; ok {
options = append(options, oss.ContentType(val.(string)))
}
err = bucket.PutObject(path, stream, options...)
u := ofs.Client.NewUploader()
_, err := u.UploadFrom(ctx, &oss.PutObjectRequest{
Bucket: oss.Ptr(ofs.BucketName),
Key: oss.Ptr(path),
}, stream)
if err != nil {
return err
}
Expand All @@ -61,23 +59,21 @@ func (ofs *OssStorage) Read(ctx context.Context, path string) (string, error) {
}

func (ofs *OssStorage) ReadStream(ctx context.Context, path string) (io.Reader, error) {
bucket, err := ofs.Client.Bucket(ofs.BucketName)
if err != nil {
return nil, err
}
body, err := bucket.GetObject(path)
res, err := ofs.Client.GetObject(ctx, &oss.GetObjectRequest{
Bucket: oss.Ptr(ofs.BucketName),
Key: oss.Ptr(path),
})
if err != nil {
return nil, err
}
return body, nil
return res.Body, nil
}

func (ofs *OssStorage) Delete(ctx context.Context, path string) error {
bucket, err := ofs.Client.Bucket(ofs.BucketName)
if err != nil {
return err
}
err = bucket.DeleteObject(path)
_, err := ofs.Client.DeleteObject(ctx, &oss.DeleteObjectRequest{
Bucket: oss.Ptr(ofs.BucketName),
Key: oss.Ptr(path),
})
if err != nil {
return err
}
Expand All @@ -92,13 +88,13 @@ func (ofs *OssStorage) PublicUrl(ctx context.Context, path string) (string, erro
}

func (ofs *OssStorage) PrivateUrl(ctx context.Context, path string) (string, error) {
bucket, err := ofs.Client.Bucket(ofs.BucketName)
res, err := ofs.Client.Presign(ctx, &oss.GetObjectRequest{
Bucket: oss.Ptr(ofs.BucketName),
Key: oss.Ptr(path),
},
oss.PresignExpires(3600))
if err != nil {
return "", err
}
finalUrl, err := bucket.SignURL(path, oss.HTTPGet, 3600)
if err != nil {
return "", err
}
return finalUrl, nil
return res.URL, nil
}
29 changes: 18 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
module github.com/duxweb/go-storage

go 1.18
go 1.21.0

toolchain go1.22.4

require (
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible
github.com/go-resty/resty/v2 v2.7.0
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.23.3+incompatible
github.com/qiniu/go-sdk/v7 v7.14.0
github.com/tencentyun/cos-go-sdk-v5 v0.7.41
github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.0.2
github.com/go-resty/resty/v2 v2.14.0
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.24.6+incompatible
github.com/qiniu/go-sdk/v7 v7.21.1
github.com/tencentyun/cos-go-sdk-v5 v0.7.54
)

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/alex-ant/gomath v0.0.0-20160516115720-89013a210a82 // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gofrs/flock v0.12.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/matishsiao/goInfo v0.0.0-20210923090445-da2e3fa8d45f // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mozillazg/go-httpheader v0.3.1 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/time v0.3.0 // indirect
github.com/mozillazg/go-httpheader v0.4.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.6.0 // indirect
)
Loading

0 comments on commit ca6f45f

Please sign in to comment.