summaryrefslogtreecommitdiff
path: root/cf-r2
diff options
context:
space:
mode:
Diffstat (limited to 'cf-r2')
-rw-r--r--cf-r2/download.go66
-rw-r--r--cf-r2/upload.go58
2 files changed, 124 insertions, 0 deletions
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 = "<account-id>.r2.cloudflarestorage.com" // Replace with your R2 endpoint
+ r2AccessKey = "<your-access-key>" // Replace with your Access Key
+ r2SecretKey = "<your-secret-key>" // 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)
+}