2022-09-16 10:53:45 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-12-01 09:50:09 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-09-27 16:08:21 +00:00
|
|
|
osssdk "github.com/aliyun/aliyun-oss-go-sdk/oss"
|
2022-09-16 10:53:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ossClient struct {
|
2023-12-01 09:50:09 +00:00
|
|
|
scType string
|
|
|
|
bucketStr string
|
|
|
|
client osssdk.Client
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOssClient(vars map[string]interface{}) (*ossClient, error) {
|
2024-01-31 07:08:14 +00:00
|
|
|
endpoint := loadParamFromVars("endpoint", vars)
|
|
|
|
accessKey := loadParamFromVars("accessKey", vars)
|
|
|
|
secretKey := loadParamFromVars("secretKey", vars)
|
|
|
|
bucketStr := loadParamFromVars("bucket", vars)
|
|
|
|
scType := loadParamFromVars("scType", vars)
|
2023-12-01 09:50:09 +00:00
|
|
|
if len(scType) == 0 {
|
2023-06-27 10:57:59 +00:00
|
|
|
scType = "Standard"
|
|
|
|
}
|
2022-09-27 16:08:21 +00:00
|
|
|
client, err := osssdk.New(endpoint, accessKey, secretKey)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
|
|
|
|
return &ossClient{scType: scType, bucketStr: bucketStr, client: *client}, nil
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (o ossClient) ListBuckets() ([]interface{}, error) {
|
|
|
|
response, err := o.client.ListBuckets()
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var result []interface{}
|
|
|
|
for _, bucket := range response.Buckets {
|
|
|
|
result = append(result, bucket.Name)
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (o ossClient) Exist(path string) (bool, error) {
|
|
|
|
bucket, err := o.client.Bucket(o.bucketStr)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return bucket.IsObjectExist(path)
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (o ossClient) Size(path string) (int64, error) {
|
|
|
|
bucket, err := o.client.Bucket(o.bucketStr)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
2023-12-01 09:50:09 +00:00
|
|
|
return 0, err
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
lor, err := bucket.ListObjectsV2(osssdk.Prefix(path))
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
2023-12-01 09:50:09 +00:00
|
|
|
return 0, err
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
if len(lor.Objects) == 0 {
|
|
|
|
return 0, fmt.Errorf("no such file %s", path)
|
|
|
|
}
|
|
|
|
return lor.Objects[0].Size, nil
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (o ossClient) Delete(path string) (bool, error) {
|
|
|
|
bucket, err := o.client.Bucket(o.bucketStr)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
if err := bucket.DeleteObject(path); err != nil {
|
2022-09-16 10:53:45 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (o ossClient) Upload(src, target string) (bool, error) {
|
|
|
|
bucket, err := o.client.Bucket(o.bucketStr)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
if err := bucket.UploadFile(target, src,
|
|
|
|
200*1024*1024,
|
|
|
|
osssdk.Routines(5),
|
|
|
|
osssdk.Checkpoint(true, ""),
|
|
|
|
osssdk.ObjectStorageClass(osssdk.StorageClassType(o.scType))); err != nil {
|
2022-09-16 10:53:45 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (o ossClient) Download(src, target string) (bool, error) {
|
|
|
|
bucket, err := o.client.Bucket(o.bucketStr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
if err := bucket.DownloadFile(src, target, 200*1024*1024, osssdk.Routines(5), osssdk.Checkpoint(true, "")); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2022-09-27 16:08:21 +00:00
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (o *ossClient) ListObjects(prefix string) ([]string, error) {
|
|
|
|
bucket, err := o.client.Bucket(o.bucketStr)
|
2022-09-28 10:11:36 +00:00
|
|
|
if err != nil {
|
2023-12-01 09:50:09 +00:00
|
|
|
return nil, err
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
lor, err := bucket.ListObjectsV2(osssdk.Prefix(prefix))
|
2022-09-28 10:11:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-08 08:44:12 +00:00
|
|
|
var result []string
|
2022-09-28 10:11:36 +00:00
|
|
|
for _, obj := range lor.Objects {
|
|
|
|
result = append(result, obj.Key)
|
|
|
|
}
|
|
|
|
return result, nil
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|