summaryrefslogtreecommitdiffstats
path: root/examples/rgw/golang/object-upload
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rgw/golang/object-upload')
-rw-r--r--examples/rgw/golang/object-upload/README.md16
-rw-r--r--examples/rgw/golang/object-upload/fortuna.txt1
-rw-r--r--examples/rgw/golang/object-upload/go.mod5
-rw-r--r--examples/rgw/golang/object-upload/go.sum24
-rw-r--r--examples/rgw/golang/object-upload/object-upload.go84
5 files changed, 130 insertions, 0 deletions
diff --git a/examples/rgw/golang/object-upload/README.md b/examples/rgw/golang/object-upload/README.md
new file mode 100644
index 000000000..1bf1c24ae
--- /dev/null
+++ b/examples/rgw/golang/object-upload/README.md
@@ -0,0 +1,16 @@
+# Introduction
+This directory contains Golang code examples on how to upload an object to an S3 bucket running on a Ceph RGW cluster.
+
+# Prerequisite
+Linux machine running an RGW Ceph cluster. Preferrably started with the ``OSD=1 MON=1 MDS=0 MGR=0 RGW=1 ../src/vstart.sh --debug --new `` command.
+Go installed on the Linux machine.
+
+## Workflow Procedure
+1. Install AWS CLI version one on your Linux machine as explained [here](https://docs.aws.amazon.com/cli/v1/userguide/install-linux.html)
+2. Create a bucket on the Ceph cluster with the command
+```
+aws --endpoint-url http://localhost:8000 s3 mb s3://sample-bucket
+```
+3. Navigate through your file system to where the Golang example code exist on your terminal.
+4. Run ``` go mod tidy ``` to install the required Go packages on the system.
+5. Run the Golang program as ``` go run object-upload.go -b sample-bucket -f fortuna.txt ``` on the terminal window to test out object upload to Ceph RGW cluster.
diff --git a/examples/rgw/golang/object-upload/fortuna.txt b/examples/rgw/golang/object-upload/fortuna.txt
new file mode 100644
index 000000000..146240570
--- /dev/null
+++ b/examples/rgw/golang/object-upload/fortuna.txt
@@ -0,0 +1 @@
+This file is being used to test Go upload capabilities to Ceph RGW cluster.
diff --git a/examples/rgw/golang/object-upload/go.mod b/examples/rgw/golang/object-upload/go.mod
new file mode 100644
index 000000000..9cb158af1
--- /dev/null
+++ b/examples/rgw/golang/object-upload/go.mod
@@ -0,0 +1,5 @@
+module ceph/object-upload
+
+go 1.13
+
+require github.com/aws/aws-sdk-go v1.43.41
diff --git a/examples/rgw/golang/object-upload/go.sum b/examples/rgw/golang/object-upload/go.sum
new file mode 100644
index 000000000..8ab2f12b3
--- /dev/null
+++ b/examples/rgw/golang/object-upload/go.sum
@@ -0,0 +1,24 @@
+github.com/aws/aws-sdk-go v1.43.41 h1:HaazVplP8/t6SOfybQlNUmjAxLWDKdLdX8BSEHFlJdY=
+github.com/aws/aws-sdk-go v1.43.41/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
+github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
+github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
+github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
+golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
+golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
+gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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)
+}