From 1df95aec09c5ac799d84367ad3721e784eb76f30 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 23 Dec 2024 01:41:48 -0600 Subject: [PATCH] more examples Signed-off-by: Jeff Carr --- .gitignore | 2 ++ cf-r2/download.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ cf-r2/upload.go | 58 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 cf-r2/download.go create mode 100644 cf-r2/upload.go diff --git a/.gitignore b/.gitignore index 2b26862..b243d3e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ go-clone-test/goclonetest go-clone-test/goclonetesttest going2git/going2git cf-r2/cf-r2 +cf-r2/download +cf-r2/upload diff --git a/cf-r2/download.go b/cf-r2/download.go new file mode 100644 index 0000000..b2f4184 --- /dev/null +++ b/cf-r2/download.go @@ -0,0 +1,66 @@ +package main + +import ( + "context" + "fmt" + "log" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +const ( + r2Endpoint = ".r2.cloudflarestorage.com" // Replace with your R2 endpoint + r2AccessKey = "" // Replace with your Access Key + r2SecretKey = "" // Replace with your Secret Key + r2BucketName = "example-bucket" // Replace with your bucket name +) + +func main() { + // Initialize MinIO client for Cloudflare R2 + client, err := minio.New(r2Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(r2AccessKey, r2SecretKey, ""), + Secure: true, // Use HTTPS + }) + if err != nil { + log.Fatalf("Failed to initialize client: %v", err) + } + + // 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) + } +} + +// UploadFile uploads a file to the R2 bucket +func uploadFile(client *minio.Client, objectName, filePath string) error { + ctx := context.Background() + _, err := client.FPutObject(ctx, r2BucketName, objectName, filePath, minio.PutObjectOptions{ + ContentType: "application/octet-stream", // Adjust MIME type if needed + }) + if err != nil { + return fmt.Errorf("failed to upload file: %w", err) + } + + fmt.Printf("Successfully uploaded %s as %s\n", filePath, objectName) + return nil +} + +// DownloadFile downloads a file from the R2 bucket +func downloadFile(client *minio.Client, objectName, destPath string) error { + ctx := context.Background() + err := client.FGetObject(ctx, r2BucketName, objectName, destPath, minio.GetObjectOptions{}) + if err != nil { + return fmt.Errorf("failed to download file: %w", err) + } + + fmt.Printf("Successfully downloaded %s to %s\n", objectName, destPath) + return nil +} diff --git a/cf-r2/upload.go b/cf-r2/upload.go new file mode 100644 index 0000000..399276b --- /dev/null +++ b/cf-r2/upload.go @@ -0,0 +1,58 @@ +// FileUploader.go MinIO example +package main + +import ( + "context" + "log" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +func main() { + ctx := context.Background() + 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) + } + + // Make a new bucket called testbucket. + bucketName := "testbucket" + location := "us-east-1" + + err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location}) + if err != nil { + // Check to see if we already own this bucket (which happens if you run this twice) + exists, errBucketExists := minioClient.BucketExists(ctx, bucketName) + if errBucketExists == nil && exists { + log.Printf("We already own %s\n", bucketName) + } else { + log.Fatalln(err) + } + } else { + log.Printf("Successfully created %s\n", bucketName) + } + + // Upload the test file + // Change the value of filePath if the file is in another location + objectName := "testdata" + filePath := "/tmp/testdata" + contentType := "application/octet-stream" + + // Upload the test file with FPutObject + info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType}) + if err != nil { + log.Fatalln(err) + } + + log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size) +}