oss.go
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package aliyun
import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"io"
"openapi/pkg/constant"
)
/*
AK AccessKey 完全访问权限
STS (Security Token Service) 临时访问控制
*/
type ossConfig struct {
AccessID string
AccessKey string
RoleAcs string
EndPoint string
BuckName string
CallbackUrl string
}
func DefaultBucket() (*oss.Bucket, error) {
client, err := oss.New(constant.OssEndPoint, constant.AccessKeyID, constant.AccessKeySecret)
if err != nil {
return nil, err
}
// Get bucket
bucket, err := client.Bucket(constant.BuckName)
if err != nil {
return nil, err
}
return bucket, nil
}
type Object struct {
Key string
Value io.Reader
}
// CreateObjects creates some objects
func CreateObjects(bucket *oss.Bucket, objects ...Object) error {
for _, object := range objects {
err := bucket.PutObject(object.Key, object.Value)
if err != nil {
return err
}
}
return nil
}
// DeleteObjects deletes some objects.
// object = key
func DeleteObjects(bucket *oss.Bucket, objects ...string) error {
for _, object := range objects {
err := bucket.DeleteObject(object)
if err != nil {
return err
}
}
return nil
}