diff options
| author | Jeff Carr <[email protected]> | 2024-12-23 01:41:48 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-12-23 01:41:48 -0600 |
| commit | 1df95aec09c5ac799d84367ad3721e784eb76f30 (patch) | |
| tree | ac99686b0ae52c526d4cebc25e04f1da2c0da4a6 /cf-r2/upload.go | |
| parent | 91060915d061674def0bbf5d3c6464c62c9eba68 (diff) | |
more examplesv0.0.17
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'cf-r2/upload.go')
| -rw-r--r-- | cf-r2/upload.go | 58 |
1 files changed, 58 insertions, 0 deletions
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) +} |
