From 91060915d061674def0bbf5d3c6464c62c9eba68 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 23 Dec 2024 01:34:56 -0600 Subject: [PATCH] maybe use cloudflare R2 Signed-off-by: Jeff Carr --- .gitignore | 1 + cf-r2-awsapi/main.go | 99 ++++++++++++++++++++++++++++++++++++++++++++ cf-r2/main.go | 26 ++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 cf-r2-awsapi/main.go create mode 100644 cf-r2/main.go diff --git a/.gitignore b/.gitignore index 34a0698..2b26862 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ go-clone-test/go-clone-test go-clone-test/goclonetest go-clone-test/goclonetesttest going2git/going2git +cf-r2/cf-r2 diff --git a/cf-r2-awsapi/main.go b/cf-r2-awsapi/main.go new file mode 100644 index 0000000..bea845d --- /dev/null +++ b/cf-r2-awsapi/main.go @@ -0,0 +1,99 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/aws/aws-sdk-go-v2/service/s3/types" +) + +const ( + r2AccountID = "" // Replace with your Cloudflare R2 Account ID + r2AccessKey = "" // Replace with your Access Key ID + r2SecretKey = "" // Replace with your Secret Access Key + r2BucketName = "example-bucket" // Replace with your bucket name + r2Endpoint = "https://.r2.cloudflarestorage.com" // Replace with your endpoint +) + +func main() { + // Initialize the AWS Config for R2 + cfg, err := config.LoadDefaultConfig(context.TODO(), + config.WithCredentialsProvider(aws.NewCredentialsCache( + aws.NewStaticCredentialsProvider(r2AccessKey, r2SecretKey, ""), + )), + config.WithEndpointResolver(aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { + return aws.Endpoint{URL: r2Endpoint}, nil + })), + ) + if err != nil { + log.Fatalf("Unable to load AWS config: %v", err) + } + + // Create the S3 client + client := s3.NewFromConfig(cfg) + + // Upload a file + err = uploadFile(client, "example.txt", "example.txt") + if err != nil { + log.Fatalf("Upload failed: %v", err) + } + + // Download the file + err = downloadFile(client, "example.txt", "downloaded-example.txt") + if err != nil { + log.Fatalf("Download failed: %v", err) + } +} + +// Upload a file to R2 +func uploadFile(client *s3.Client, objectKey, filePath string) error { + file, err := os.Open(filePath) + if err != nil { + return fmt.Errorf("failed to open file %s: %w", filePath, err) + } + defer file.Close() + + _, err = client.PutObject(context.TODO(), &s3.PutObjectInput{ + Bucket: aws.String(r2BucketName), + Key: aws.String(objectKey), + Body: file, + ACL: types.ObjectCannedACLPublicRead, // Optional: Make the file public + }) + if err != nil { + return fmt.Errorf("failed to upload file to R2: %w", err) + } + + fmt.Printf("File %s uploaded to R2 as %s\n", filePath, objectKey) + return nil +} + +// Download a file from R2 +func downloadFile(client *s3.Client, objectKey, destPath string) error { + resp, err := client.GetObject(context.TODO(), &s3.GetObjectInput{ + Bucket: aws.String(r2BucketName), + Key: aws.String(objectKey), + }) + if err != nil { + return fmt.Errorf("failed to download file from R2: %w", err) + } + defer resp.Body.Close() + + outFile, err := os.Create(destPath) + if err != nil { + return fmt.Errorf("failed to create file %s: %w", destPath, err) + } + defer outFile.Close() + + _, err = outFile.ReadFrom(resp.Body) + if err != nil { + return fmt.Errorf("failed to write file to %s: %w", destPath, err) + } + + fmt.Printf("File %s downloaded from R2 and saved as %s\n", objectKey, destPath) + return nil +} diff --git a/cf-r2/main.go b/cf-r2/main.go new file mode 100644 index 0000000..ce3c910 --- /dev/null +++ b/cf-r2/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "log" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +func main() { + endpoint := "play.min.io" + accessKeyID := "Q3AM3UQ867SPQQA43P2F" + secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" + useSSL := true + + // Initialize minio client object. + minioClient, err := minio.New(endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), + Secure: useSSL, + }) + if err != nil { + log.Fatalln(err) + } + + log.Printf("%#v\n", minioClient) // minioClient is now set up +}