diff options
| author | Jeff Carr <[email protected]> | 2024-12-23 01:34:56 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-12-23 01:34:56 -0600 |
| commit | 91060915d061674def0bbf5d3c6464c62c9eba68 (patch) | |
| tree | b5561d7149f948960dd08c443e4f7b65f96706b4 /cf-r2-awsapi | |
| parent | c5fb46c60d4a0383e0e3586bd2d4968e8a8907b7 (diff) | |
maybe use cloudflare R2
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'cf-r2-awsapi')
| -rw-r--r-- | cf-r2-awsapi/main.go | 99 |
1 files changed, 99 insertions, 0 deletions
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 = "<your-account-id>" // Replace with your Cloudflare R2 Account ID + r2AccessKey = "<your-access-key-id>" // Replace with your Access Key ID + r2SecretKey = "<your-secret-access-key>" // Replace with your Secret Access Key + r2BucketName = "example-bucket" // Replace with your bucket name + r2Endpoint = "https://<account-id>.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 +} |
