From 0b17b379a8f6983bfbf2152ba04ffd2473a863e9 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 11 Jul 2020 09:11:29 -0700 Subject: [PATCH] basic s3 tests --- test/s3/basic/basic_test.go | 111 ++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 test/s3/basic/basic_test.go diff --git a/test/s3/basic/basic_test.go b/test/s3/basic/basic_test.go new file mode 100644 index 000000000..72d55939b --- /dev/null +++ b/test/s3/basic/basic_test.go @@ -0,0 +1,111 @@ +package basic + +import ( + "fmt" + "os" + "strings" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/s3" +) + +var ( + svc *s3.S3 +) + +func init() { + // Initialize a session in us-west-2 that the SDK will use to load + // credentials from the shared credentials file ~/.aws/credentials. + sess, err := session.NewSession(&aws.Config{ + Region: aws.String("us-west-2"), + Endpoint: aws.String("localhost:8333"), + DisableSSL: aws.Bool(true), + }) + if err != nil { + exitErrorf("create session, %v", err) + } + + // Create S3 service client + svc = s3.New(sess) +} + +func TestCreateBucket(t *testing.T) { + + input := &s3.CreateBucketInput{ + Bucket: aws.String("theBucket"), + } + + result, err := svc.CreateBucket(input) + if err != nil { + if aerr, ok := err.(awserr.Error); ok { + switch aerr.Code() { + case s3.ErrCodeBucketAlreadyExists: + fmt.Println(s3.ErrCodeBucketAlreadyExists, aerr.Error()) + case s3.ErrCodeBucketAlreadyOwnedByYou: + fmt.Println(s3.ErrCodeBucketAlreadyOwnedByYou, aerr.Error()) + default: + fmt.Println(aerr.Error()) + } + } else { + // Print the error, cast err to awserr.Error to get the Code and + // Message from an error. + fmt.Println(err.Error()) + } + return + } + + fmt.Println(result) + +} + +func TestListBuckets(t *testing.T) { + + input := &s3.PutObjectInput{ + ACL: aws.String("authenticated-read"), + Body: aws.ReadSeekCloser(strings.NewReader("filetoupload")), + Bucket: aws.String("theBucket"), + Key: aws.String("exampleobject"), + } + + result, err := svc.PutObject(input) + if err != nil { + if aerr, ok := err.(awserr.Error); ok { + switch aerr.Code() { + default: + fmt.Println(aerr.Error()) + } + } else { + // Print the error, cast err to awserr.Error to get the Code and + // Message from an error. + fmt.Println(err.Error()) + } + return + } + + fmt.Println(result) + +} + +func TestPutObject(t *testing.T) { + + result, err := svc.ListBuckets(nil) + if err != nil { + exitErrorf("Unable to list buckets, %v", err) + } + + fmt.Println("Buckets:") + + for _, b := range result.Buckets { + fmt.Printf("* %s created on %s\n", + aws.StringValue(b.Name), aws.TimeValue(b.CreationDate)) + } + +} + +func exitErrorf(msg string, args ...interface{}) { + fmt.Fprintf(os.Stderr, msg+"\n", args...) + os.Exit(1) +}