diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /examples/rgw/golang/object-upload/object-upload.go | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | examples/rgw/golang/object-upload/object-upload.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/examples/rgw/golang/object-upload/object-upload.go b/examples/rgw/golang/object-upload/object-upload.go new file mode 100644 index 000000000..82f09caa2 --- /dev/null +++ b/examples/rgw/golang/object-upload/object-upload.go @@ -0,0 +1,84 @@ +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/endpoints" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/s3/s3manager" +) + +func main() { + bucket := flag.String("b", "", "The name of the bucket") + filename := flag.String("f", "", "Complete file path to object to be uploaded") + flag.Parse() + + if *bucket == "" { + fmt.Println("You must supply the name of the bucket") + fmt.Println("-b BUCKET") + return + } + + if *filename == "" { + fmt.Println("You must supply the object to be uploaded") + fmt.Println("-f FILE/FILEPATH") + return + } + + file, err := os.Open(*filename) + if err != nil { + exitErrorf("Unable to open file %q, %v", filename, err) + } + + defer file.Close() + + //Ceph RGW Cluster credentials + access_key := "0555b35654ad1656d804" + secret_key := "h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q==" + token_id := "" + url := "http://127.0.0.1:8000" + + defaultResolver := endpoints.DefaultResolver() + s3CustResolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) { + if service == "s3" { + return endpoints.ResolvedEndpoint{ + URL: url, + }, nil + } + + return defaultResolver.EndpointFor(service, region, optFns...) + } + + sess := session.Must(session.NewSessionWithOptions(session.Options{ + Config: aws.Config{ + Region: aws.String("default"), + Credentials: credentials.NewStaticCredentials(access_key, secret_key, token_id), + S3ForcePathStyle: aws.Bool(true), + EndpointResolver: endpoints.ResolverFunc(s3CustResolverFn), + }, + })) + + uploader := s3manager.NewUploader(sess) + + // Upload the file's body to S3 bucket as an object with the key being the + // same as the filename. + _, err = uploader.Upload(&s3manager.UploadInput{ + Bucket: bucket, + Key: filename, + Body: file, + }) + if err != nil { + exitErrorf("Unable to upload %q to %q, %v", *filename, *bucket, err) + } + + fmt.Printf("Successfully uploaded %q to %q\n", *filename, *bucket) +} + +func exitErrorf(msg string, args ...interface{}) { + fmt.Fprintf(os.Stderr, msg+"\n", args...) + os.Exit(1) +} |