]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
examples/golang/put-bucket-notification-creation : Add put bucket notification creati...
authorFaith <faithuniterh@tutanota.com>
Tue, 30 Aug 2022 12:06:09 +0000 (12:06 +0000)
committerFaith <faithuniterh@tutanota.com>
Fri, 2 Sep 2022 08:56:20 +0000 (08:56 +0000)
Signed-off-by: Faith <faithuniterh@tutanota.com>
examples/golang/put-bucket-notification-creation/README.md [new file with mode: 0644]
examples/golang/put-bucket-notification-creation/go.mod [new file with mode: 0644]
examples/golang/put-bucket-notification-creation/go.sum [new file with mode: 0644]
examples/golang/put-bucket-notification-creation/put-bucket-notification-creation.go [new file with mode: 0644]

diff --git a/examples/golang/put-bucket-notification-creation/README.md b/examples/golang/put-bucket-notification-creation/README.md
new file mode 100644 (file)
index 0000000..47555c0
--- /dev/null
@@ -0,0 +1,24 @@
+# Introduction
+This directory contains Golang code examples on how to create a put bucket notification to a topic and 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 topic on the Ceph cluster with the command 
+```
+aws --region default --endpoint-url http://localhost:8000 sns create-topic --name=sample-topic --attributes='{"push-endpoint": "http://localhost:10900"}'
+```
+3. Create a bucket to which the topic will be attached to with the command
+```
+aws --endpoint-url http://localhost:8000 s3 mb s3://sample-bucket
+```
+4. Navigate through your file system to where the Golang example code exists on your terminal.
+5. Run ``` go mod tidy ``` to install the required Go packages on the system.
+6. Run the Golang program as ``` go run put-bucket-notification-creation.go -b sample-bucket -t arn:aws:sns:default::sample-topic ``` on the terminal window to create the put bucket notification with the suffix filter rule.   
+7. Upload  any jpg file you have to the bucket with the command
+```
+aws --endpoint-url http://localhost:8000 s3 cp your-jpg-file.jpg s3://sample-bucket
+```
diff --git a/examples/golang/put-bucket-notification-creation/go.mod b/examples/golang/put-bucket-notification-creation/go.mod
new file mode 100644 (file)
index 0000000..4740ead
--- /dev/null
@@ -0,0 +1,5 @@
+module put-bucket-notification-creation
+
+go 1.13
+
+require github.com/aws/aws-sdk-go v1.44.62
diff --git a/examples/golang/put-bucket-notification-creation/go.sum b/examples/golang/put-bucket-notification-creation/go.sum
new file mode 100644 (file)
index 0000000..7cee1fa
--- /dev/null
@@ -0,0 +1,24 @@
+github.com/aws/aws-sdk-go v1.44.62 h1:N8qOPnBhl2ZCIFiqyB640Xt5CeX9D8CEVhG/Vj7jGJU=
+github.com/aws/aws-sdk-go v1.44.62/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/golang/put-bucket-notification-creation/put-bucket-notification-creation.go b/examples/golang/put-bucket-notification-creation/put-bucket-notification-creation.go
new file mode 100644 (file)
index 0000000..5e8b7f4
--- /dev/null
@@ -0,0 +1,96 @@
+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"
+)
+
+func main() {
+       bucket := flag.String("b", "", "Name of the bucket to add notification to")
+       topic := flag.String("t", "", "The topic onto which the notification is attached to")
+       flag.Parse()
+
+       if *bucket == "" {
+               fmt.Println("You must supply the name of the bucket")
+               fmt.Println("-b BUCKET")
+               return
+       }
+
+       if *topic == "" {
+               fmt.Println("You must supply the name of the topic ARN")
+               fmt.Println("-t TOPIC ARN")
+               return
+       }
+
+       //Ceph RGW Credentials
+       access_key := "0555b35654ad1656d804"
+       secret_key := "h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q=="
+       token_id := ""
+       url := "http://127.0.0.1:8000"
+
+       defaultResolver := endpoints.DefaultResolver()
+       CustResolverFn := 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(CustResolverFn),
+               },
+       }))
+
+       svc := s3.New(sess)
+
+       suffixRule := []*s3.FilterRule{
+               {
+                       Name:  aws.String("suffix"),
+                       Value: aws.String("jpg"),
+               },
+       }
+
+       input := &s3.PutBucketNotificationConfigurationInput{
+               Bucket: bucket,
+               NotificationConfiguration: &s3.NotificationConfiguration{
+                       TopicConfigurations: []*s3.TopicConfiguration{
+                               {
+                                       Events: []*string{aws.String("s3:ObjectCreated:*")},
+                                       Filter: &s3.NotificationConfigurationFilter{
+                                               Key: &s3.KeyFilter{
+                                                       FilterRules: suffixRule,
+                                               },
+                                       },
+                                       Id:       aws.String("notif1"), //Raises MalformedXML if absent
+                                       TopicArn: topic,
+                               },
+                       },
+               },
+       }
+
+       _, err := svc.PutBucketNotificationConfiguration(input)
+
+       if err != nil {
+               exitErrorf("Unable to create Put Bucket Notification because of %s", err)
+       }
+       fmt.Println("Put bucket notification added to  ", *topic)
+}
+
+func exitErrorf(msg string, args ...interface{}) {
+       fmt.Fprintf(os.Stderr, msg+"\n", args...)
+       os.Exit(1)
+}