-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
207 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.