You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
4.5 KiB

5 years ago
5 years ago
  1. package basic
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/awserr"
  6. "github.com/aws/aws-sdk-go/aws/session"
  7. "github.com/aws/aws-sdk-go/service/s3"
  8. "io/ioutil"
  9. "os"
  10. "strings"
  11. "testing"
  12. )
  13. var (
  14. svc *s3.S3
  15. )
  16. func init() {
  17. // Initialize a session in us-west-2 that the SDK will use to load
  18. // credentials from the shared credentials file ~/.aws/credentials.
  19. sess, err := session.NewSession(&aws.Config{
  20. Region: aws.String("us-west-2"),
  21. Endpoint: aws.String("localhost:8333"),
  22. DisableSSL: aws.Bool(true),
  23. })
  24. if err != nil {
  25. exitErrorf("create session, %v", err)
  26. }
  27. // Create S3 service client
  28. svc = s3.New(sess)
  29. }
  30. func TestCreateBucket(t *testing.T) {
  31. input := &s3.CreateBucketInput{
  32. Bucket: aws.String("theBucket"),
  33. }
  34. result, err := svc.CreateBucket(input)
  35. if err != nil {
  36. if aerr, ok := err.(awserr.Error); ok {
  37. switch aerr.Code() {
  38. case s3.ErrCodeBucketAlreadyExists:
  39. fmt.Println(s3.ErrCodeBucketAlreadyExists, aerr.Error())
  40. case s3.ErrCodeBucketAlreadyOwnedByYou:
  41. fmt.Println(s3.ErrCodeBucketAlreadyOwnedByYou, aerr.Error())
  42. default:
  43. fmt.Println(aerr.Error())
  44. }
  45. } else {
  46. // Print the error, cast err to awserr.Error to get the Code and
  47. // Message from an error.
  48. fmt.Println(err.Error())
  49. }
  50. return
  51. }
  52. fmt.Println(result)
  53. }
  54. func TestListBuckets(t *testing.T) {
  55. input := &s3.PutObjectInput{
  56. ACL: aws.String("authenticated-read"),
  57. Body: aws.ReadSeekCloser(strings.NewReader("filetoupload")),
  58. Bucket: aws.String("theBucket"),
  59. Key: aws.String("exampleobject"),
  60. }
  61. result, err := svc.PutObject(input)
  62. if err != nil {
  63. if aerr, ok := err.(awserr.Error); ok {
  64. switch aerr.Code() {
  65. default:
  66. fmt.Println(aerr.Error())
  67. }
  68. } else {
  69. // Print the error, cast err to awserr.Error to get the Code and
  70. // Message from an error.
  71. fmt.Println(err.Error())
  72. }
  73. return
  74. }
  75. fmt.Println(result)
  76. }
  77. func TestPutObject(t *testing.T) {
  78. result, err := svc.ListBuckets(nil)
  79. if err != nil {
  80. exitErrorf("Unable to list buckets, %v", err)
  81. }
  82. fmt.Println("Buckets:")
  83. for _, b := range result.Buckets {
  84. fmt.Printf("* %s created on %s\n",
  85. aws.StringValue(b.Name), aws.TimeValue(b.CreationDate))
  86. }
  87. }
  88. func exitErrorf(msg string, args ...interface{}) {
  89. fmt.Fprintf(os.Stderr, msg+"\n", args...)
  90. os.Exit(1)
  91. }
  92. const (
  93. Bucket = "theBucket"
  94. object = "foo/bar"
  95. Data = "<data>"
  96. )
  97. func TestObjectOp(t *testing.T) {
  98. _, err := svc.CreateBucket(&s3.CreateBucketInput{
  99. Bucket: aws.String(Bucket),
  100. })
  101. if err != nil {
  102. exitErrorf("Unable to create bucket, %v", err)
  103. }
  104. _, err = svc.PutObject(&s3.PutObjectInput{
  105. Bucket: aws.String(Bucket),
  106. Key: aws.String(object),
  107. Body: strings.NewReader(Data),
  108. })
  109. if err != nil {
  110. exitErrorf("Unable to put object, %v", err)
  111. }
  112. dest := fmt.Sprintf("%s_bak", object)
  113. copyObj, err := svc.CopyObject(&s3.CopyObjectInput{
  114. Bucket: aws.String(Bucket),
  115. CopySource: aws.String(fmt.Sprintf("%s/%s", Bucket, object)),
  116. Key: aws.String(dest),
  117. })
  118. if err != nil {
  119. exitErrorf("Unable to copy object, %v", err)
  120. }
  121. t.Log("copy object result -> ", copyObj.CopyObjectResult)
  122. getObj, err := svc.GetObject(&s3.GetObjectInput{
  123. Bucket: aws.String(Bucket),
  124. Key: aws.String(dest),
  125. })
  126. if err != nil {
  127. exitErrorf("Unable to get copy object, %v", err)
  128. }
  129. data, err := ioutil.ReadAll(getObj.Body)
  130. if err != nil {
  131. exitErrorf("Unable to read object data, %v", err)
  132. }
  133. if string(data) != Data {
  134. t.Error("object data -> ", string(data))
  135. }
  136. listObj, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{
  137. Bucket: aws.String(Bucket),
  138. Prefix: aws.String("foo/"),
  139. })
  140. if err != nil {
  141. exitErrorf("Unable to list objects, %v", err)
  142. }
  143. count := 0
  144. for _, content := range listObj.Contents {
  145. key := aws.StringValue(content.Key)
  146. if key == dest {
  147. count++
  148. } else if key == object {
  149. count++
  150. }
  151. if count == 2 {
  152. break
  153. }
  154. }
  155. if count != 2 {
  156. exitErrorf("Unable to find two objects, %v", listObj.Contents)
  157. }
  158. _, err = svc.DeleteObject(&s3.DeleteObjectInput{
  159. Bucket: aws.String(Bucket),
  160. Key: aws.String(object),
  161. })
  162. if err != nil {
  163. exitErrorf("Unable to delete source object, %v", err)
  164. }
  165. _, err = svc.DeleteObject(&s3.DeleteObjectInput{
  166. Bucket: aws.String(Bucket),
  167. Key: aws.String(dest),
  168. })
  169. if err != nil {
  170. exitErrorf("Unable to delete object, %v", err)
  171. }
  172. _, err = svc.DeleteBucket(&s3.DeleteBucketInput{
  173. Bucket: aws.String(Bucket),
  174. })
  175. if err != nil {
  176. exitErrorf("Unable to delete bucket, %v", err)
  177. }
  178. }