maybe use cloudflare R2
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
c5fb46c60d
commit
91060915d0
|
@ -14,3 +14,4 @@ go-clone-test/go-clone-test
|
|||
go-clone-test/goclonetest
|
||||
go-clone-test/goclonetesttest
|
||||
going2git/going2git
|
||||
cf-r2/cf-r2
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue