/
s3client.go
114 lines (99 loc) · 2.59 KB
/
s3client.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"time"
)
func main() {
cfg := MyConfig{
Key: "any",
Secret: "any",
Region: "US",
Endpoint: MyEndpoint{
URL: "http://localhost:8333",
},
Bucket: MyBucketConfig{
Name: "newbucket",
Versioning: false,
},
MaxBackoffDelay: aws.Int(int(time.Second * 5)),
MaxRetryAttempts: aws.Int(1),
}
awsCfg, err := MyAwsConfig(cfg)
if err != nil {
panic(err)
}
svc := s3.NewFromConfig(*awsCfg, func(o *s3.Options) {
o.UsePathStyle = true
})
// Use the S3 client to interact with SeaweedFS
// ...
// Example: List all buckets
result, err := svc.ListBuckets(context.Background(), &s3.ListBucketsInput{})
// no errors - got list of buckets
if err != nil {
panic(err)
}
// Print the list of buckets
for _, bucket := range result.Buckets {
println(*bucket.Name)
}
bucket := "bucket1"
_, err = svc.HeadBucket(context.Background(), &s3.HeadBucketInput{Bucket: &bucket})
// ERROR HERE
if err != nil {
println(err)
}
}
// === helpers
func MyAwsConfig(cfg MyConfig) (*aws.Config, error) {
cred := aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(cfg.Key, cfg.Secret, ""))
customResolver := aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: cfg.Endpoint.URL,
SigningRegion: cfg.Region,
}, nil
})
awsCfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(cfg.Region),
config.WithCredentialsProvider(cred),
config.WithEndpointResolverWithOptions(customResolver),
config.WithRetryer(func() aws.Retryer {
r := retry.AddWithMaxAttempts(retry.NewStandard(), *cfg.MaxRetryAttempts)
return retry.AddWithMaxBackoffDelay(r, time.Duration(*cfg.MaxBackoffDelay*1000*1000))
}))
return &awsCfg, err
}
type MyConfig struct {
// Access key of S3 AWS.
Key string
// Access secret of S3 AWS.
Secret string
// Region.
Region string
// AWS endpoint.
Endpoint MyEndpoint
// Bucket configuration.
Bucket MyBucketConfig
// File access.
FileAccess MyFileAccessType
// Maximum backoff delay (ms, default: 20 sec).
MaxBackoffDelay *int
// Maximum attempts to retry operation on error (default: 5).
MaxRetryAttempts *int
}
type MyBucketConfig struct {
// Name of bucket
Name string
// Enable or not versioning
Versioning bool
}
type MyEndpoint struct {
URL string
}
type MyFileAccessType byte