100 lines
2.8 KiB
Go
100 lines
2.8 KiB
Go
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
|
|
}
|