|
| 1 | +// Copyright 2023 The OpenAgent Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package storage |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss" |
| 25 | + "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials" |
| 26 | +) |
| 27 | + |
| 28 | +type AliyunOssStorageProvider struct { |
| 29 | + client *oss.Client |
| 30 | + bucket string |
| 31 | + endpoint string |
| 32 | +} |
| 33 | + |
| 34 | +func NewAliyunOssStorageProvider(accessKeyId string, accessKeySecret string, region string, bucket string, endpoint string) (*AliyunOssStorageProvider, error) { |
| 35 | + cfg := oss.LoadDefaultConfig(). |
| 36 | + WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")). |
| 37 | + WithRegion(region). |
| 38 | + WithEndpoint(endpoint) |
| 39 | + |
| 40 | + client := oss.NewClient(cfg) |
| 41 | + return &AliyunOssStorageProvider{ |
| 42 | + client: client, |
| 43 | + bucket: bucket, |
| 44 | + endpoint: endpoint, |
| 45 | + }, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (p *AliyunOssStorageProvider) getObjectUrl(key string) string { |
| 49 | + endpoint := p.endpoint |
| 50 | + if !strings.HasPrefix(endpoint, "http") { |
| 51 | + endpoint = "https://" + endpoint |
| 52 | + } |
| 53 | + endpoint = strings.TrimRight(endpoint, "/") |
| 54 | + return fmt.Sprintf("%s/%s/%s", endpoint, p.bucket, key) |
| 55 | +} |
| 56 | + |
| 57 | +func (p *AliyunOssStorageProvider) ListObjects(prefix string) ([]*Object, error) { |
| 58 | + result, err := p.client.ListObjectsV2(context.TODO(), &oss.ListObjectsV2Request{ |
| 59 | + Bucket: oss.Ptr(p.bucket), |
| 60 | + Prefix: oss.Ptr(prefix), |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + var objects []*Object |
| 67 | + for _, obj := range result.Contents { |
| 68 | + key := oss.ToString(obj.Key) |
| 69 | + size := obj.Size |
| 70 | + lastModified := "" |
| 71 | + if obj.LastModified != nil { |
| 72 | + lastModified = obj.LastModified.Format(time.RFC3339) |
| 73 | + } |
| 74 | + objects = append(objects, &Object{ |
| 75 | + Key: key, |
| 76 | + LastModified: lastModified, |
| 77 | + Size: size, |
| 78 | + Url: p.getObjectUrl(key), |
| 79 | + }) |
| 80 | + } |
| 81 | + return objects, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (p *AliyunOssStorageProvider) PutObject(user string, parent string, key string, fileBuffer *bytes.Buffer) (string, error) { |
| 85 | + _, err := p.client.PutObject(context.TODO(), &oss.PutObjectRequest{ |
| 86 | + Bucket: oss.Ptr(p.bucket), |
| 87 | + Key: oss.Ptr(key), |
| 88 | + Body: fileBuffer, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return "", err |
| 92 | + } |
| 93 | + return p.getObjectUrl(key), nil |
| 94 | +} |
| 95 | + |
| 96 | +func (p *AliyunOssStorageProvider) DeleteObject(key string) error { |
| 97 | + _, err := p.client.DeleteObject(context.TODO(), &oss.DeleteObjectRequest{ |
| 98 | + Bucket: oss.Ptr(p.bucket), |
| 99 | + Key: oss.Ptr(key), |
| 100 | + }) |
| 101 | + return err |
| 102 | +} |
0 commit comments