Skip to content

Commit 20621f6

Browse files
committed
feat(components): add r2 s3 api
1 parent 8255962 commit 20621f6

File tree

17 files changed

+207
-152
lines changed

17 files changed

+207
-152
lines changed

components/oss/oss.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package oss
2+
3+
import "context"
4+
5+
type OSS interface {
6+
UploadFromLocal(ctx context.Context, bucket, filePath, ossPath string) (any, error)
7+
}

components/oss/s3/r2.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package s3
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/alomerry/go-tools/components/oss"
7+
"github.com/alomerry/go-tools/static/cons"
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
"github.com/aws/aws-sdk-go-v2/config"
10+
"github.com/aws/aws-sdk-go-v2/credentials"
11+
"github.com/aws/aws-sdk-go-v2/service/s3"
12+
"log"
13+
"os"
14+
)
15+
16+
type CloudflareR2 struct {
17+
client *s3.Client
18+
}
19+
20+
func NewCloudflareR2(accountId, r2Key, r2Secret string) oss.OSS {
21+
c := &CloudflareR2{}
22+
r2Resolver := aws.EndpointResolverWithOptionsFunc(
23+
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
24+
return aws.Endpoint{
25+
URL: fmt.Sprintf("https://%s.r2.cloudflarestorage.com", accountId),
26+
}, nil
27+
})
28+
29+
cfg, err := config.LoadDefaultConfig(context.TODO(),
30+
config.WithEndpointResolverWithOptions(r2Resolver),
31+
config.WithCredentialsProvider(
32+
credentials.NewStaticCredentialsProvider(
33+
r2Key,
34+
r2Secret,
35+
cons.EmptyStr,
36+
)),
37+
config.WithRegion("auto"),
38+
)
39+
if err != nil {
40+
log.Fatal(err)
41+
}
42+
43+
c.client = s3.NewFromConfig(cfg)
44+
45+
return c
46+
}
47+
48+
func (c *CloudflareR2) UploadFromLocal(ctx context.Context, bucket, filePath, ossPath string) (any, error) {
49+
file, err := os.Open(filePath)
50+
if err != nil {
51+
log.Fatal(err)
52+
}
53+
resp, err := c.client.PutObject(ctx, &s3.PutObjectInput{
54+
Bucket: &bucket,
55+
Key: &ossPath,
56+
Body: file,
57+
ContentType: nil, // TODO
58+
})
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
return resp, nil
63+
}

components/oss/s3/r2_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package s3
2+
3+
//func TestUploadFromLocalByCloudflareR2(t *testing.T) {
4+
// oss := NewCloudflareR2(
5+
// "742fd424ac4c88375eefc680a5c8ed2f",
6+
// "00bd0c0bd54c2728aedbe9d98e2071d0",
7+
// "d0e3cf529d3bafc220c10326ddd24a3386a88f46f43df902732588949422319e",
8+
// // env.GetCloudflareAccountId(),
9+
// // env.GetCloudflareR2AccountKey(),
10+
// // env.GetCloudflareR2AccountSK(),
11+
// )
12+
//
13+
// fmt.Println(oss.UploadFromLocal(context.TODO(), cons.OssBucketCdn, "/Users/alomerry/workspace/go-tools/output/avatar.jpg", "blog/666.jpg"))
14+
//}

go.mod

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,36 @@ module github.com/alomerry/go-tools
33
go 1.21
44

55
require (
6+
github.com/aws/aws-sdk-go-v2 v1.26.1
7+
github.com/aws/aws-sdk-go-v2/config v1.27.11
8+
github.com/aws/aws-sdk-go-v2/credentials v1.17.11
9+
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
610
github.com/emirpasic/gods v1.18.1
711
github.com/google/martian v2.1.0+incompatible
812
github.com/magiconair/properties v1.8.7
913
github.com/spf13/cast v1.5.1
1014
github.com/spf13/viper v1.16.0
1115
github.com/stretchr/testify v1.8.4
1216
github.com/tealeg/xlsx/v3 v3.3.0
13-
github.com/tickstep/aliyunpan-api v0.2.1
1417
gorm.io/driver/mysql v1.5.6
1518
gorm.io/gorm v1.25.10
1619
)
1720

1821
require (
22+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
23+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
24+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
25+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
26+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
27+
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 // indirect
28+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
29+
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.7 // indirect
30+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
31+
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.5 // indirect
32+
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect
33+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
34+
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect
35+
github.com/aws/smithy-go v1.20.2 // indirect
1936
github.com/davecgh/go-spew v1.1.1 // indirect
2037
github.com/frankban/quicktest v1.14.5 // indirect
2138
github.com/fsnotify/fsnotify v1.6.0 // indirect
@@ -25,24 +42,19 @@ require (
2542
github.com/hashicorp/hcl v1.0.0 // indirect
2643
github.com/jinzhu/inflection v1.0.0 // indirect
2744
github.com/jinzhu/now v1.1.5 // indirect
28-
github.com/json-iterator/go v1.1.12 // indirect
2945
github.com/kr/pretty v0.3.1 // indirect
3046
github.com/kr/text v0.2.0 // indirect
3147
github.com/mitchellh/mapstructure v1.5.0 // indirect
32-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
33-
github.com/modern-go/reflect2 v1.0.2 // indirect
3448
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
3549
github.com/peterbourgon/diskv/v3 v3.0.1 // indirect
3650
github.com/pmezard/go-difflib v1.0.0 // indirect
3751
github.com/rogpeppe/fastuuid v1.2.0 // indirect
3852
github.com/rogpeppe/go-internal v1.9.0 // indirect
39-
github.com/satori/go.uuid v1.2.0 // indirect
4053
github.com/shabbyrobe/xmlwriter v0.0.0-20200208144257-9fca06d00ffa // indirect
4154
github.com/spf13/afero v1.9.5 // indirect
4255
github.com/spf13/jwalterweatherman v1.1.0 // indirect
4356
github.com/spf13/pflag v1.0.5 // indirect
4457
github.com/subosito/gotenv v1.4.2 // indirect
45-
github.com/tickstep/library-go v0.1.0 // indirect
4658
golang.org/x/sys v0.13.0 // indirect
4759
golang.org/x/text v0.13.0 // indirect
4860
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect

0 commit comments

Comments
 (0)