Skip to content

Commit

Permalink
feat(components): add r2 s3 api
Browse files Browse the repository at this point in the history
  • Loading branch information
alomerry committed May 2, 2024
1 parent 8255962 commit 20621f6
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 152 deletions.
7 changes: 7 additions & 0 deletions components/oss/oss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package oss

import "context"

type OSS interface {
UploadFromLocal(ctx context.Context, bucket, filePath, ossPath string) (any, error)
}
63 changes: 63 additions & 0 deletions components/oss/s3/r2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package s3

import (
"context"
"fmt"
"github.com/alomerry/go-tools/components/oss"
"github.com/alomerry/go-tools/static/cons"
"github.com/aws/aws-sdk-go-v2/aws"
"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"
"log"
"os"
)

type CloudflareR2 struct {
client *s3.Client
}

func NewCloudflareR2(accountId, r2Key, r2Secret string) oss.OSS {
c := &CloudflareR2{}
r2Resolver := aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: fmt.Sprintf("https://%s.r2.cloudflarestorage.com", accountId),
}, nil
})

cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithEndpointResolverWithOptions(r2Resolver),
config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(
r2Key,
r2Secret,
cons.EmptyStr,
)),
config.WithRegion("auto"),
)
if err != nil {
log.Fatal(err)
}

c.client = s3.NewFromConfig(cfg)

return c
}

func (c *CloudflareR2) UploadFromLocal(ctx context.Context, bucket, filePath, ossPath string) (any, error) {
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
resp, err := c.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: &bucket,
Key: &ossPath,
Body: file,
ContentType: nil, // TODO
})
if err != nil {
log.Fatal(err)
}
return resp, nil
}
14 changes: 14 additions & 0 deletions components/oss/s3/r2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package s3

//func TestUploadFromLocalByCloudflareR2(t *testing.T) {
// oss := NewCloudflareR2(
// "742fd424ac4c88375eefc680a5c8ed2f",
// "00bd0c0bd54c2728aedbe9d98e2071d0",
// "d0e3cf529d3bafc220c10326ddd24a3386a88f46f43df902732588949422319e",
// // env.GetCloudflareAccountId(),
// // env.GetCloudflareR2AccountKey(),
// // env.GetCloudflareR2AccountSK(),
// )
//
// fmt.Println(oss.UploadFromLocal(context.TODO(), cons.OssBucketCdn, "/Users/alomerry/workspace/go-tools/output/avatar.jpg", "blog/666.jpg"))
//}
24 changes: 18 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,36 @@ module github.com/alomerry/go-tools
go 1.21

require (
github.com/aws/aws-sdk-go-v2 v1.26.1
github.com/aws/aws-sdk-go-v2/config v1.27.11
github.com/aws/aws-sdk-go-v2/credentials v1.17.11
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
github.com/emirpasic/gods v1.18.1
github.com/google/martian v2.1.0+incompatible
github.com/magiconair/properties v1.8.7
github.com/spf13/cast v1.5.1
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
github.com/tealeg/xlsx/v3 v3.3.0
github.com/tickstep/aliyunpan-api v0.2.1
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.10
)

require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/frankban/quicktest v1.14.5 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand All @@ -25,24 +42,19 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/peterbourgon/diskv/v3 v3.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/fastuuid v1.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/shabbyrobe/xmlwriter v0.0.0-20200208144257-9fca06d00ffa // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tickstep/library-go v0.1.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
Loading

0 comments on commit 20621f6

Please sign in to comment.