basic_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package basic
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "strings"
  7. "testing"
  8. "github.com/aws/aws-sdk-go/aws"
  9. "github.com/aws/aws-sdk-go/aws/awserr"
  10. "github.com/aws/aws-sdk-go/aws/session"
  11. "github.com/aws/aws-sdk-go/service/s3"
  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 TestPutObject(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 TestListBucket(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 TestListObjectV2(t *testing.T) {
  89. listObj, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{
  90. Bucket: aws.String(Bucket),
  91. Prefix: aws.String("foo"),
  92. Delimiter: aws.String("/"),
  93. })
  94. if err != nil {
  95. exitErrorf("Unable to list objects, %v", err)
  96. }
  97. for _, content := range listObj.Contents {
  98. fmt.Println(aws.StringValue(content.Key))
  99. }
  100. fmt.Printf("list: %s\n", listObj)
  101. }
  102. func exitErrorf(msg string, args ...interface{}) {
  103. fmt.Fprintf(os.Stderr, msg+"\n", args...)
  104. os.Exit(1)
  105. }
  106. const (
  107. Bucket = "theBucket"
  108. object = "foo/bar"
  109. Data = "<data>"
  110. )
  111. func TestObjectOp(t *testing.T) {
  112. _, err := svc.CreateBucket(&s3.CreateBucketInput{
  113. Bucket: aws.String(Bucket),
  114. })
  115. if err != nil {
  116. exitErrorf("Unable to create bucket, %v", err)
  117. }
  118. _, err = svc.PutObject(&s3.PutObjectInput{
  119. Bucket: aws.String(Bucket),
  120. Key: aws.String(object),
  121. Body: strings.NewReader(Data),
  122. })
  123. if err != nil {
  124. exitErrorf("Unable to put object, %v", err)
  125. }
  126. dest := fmt.Sprintf("%s_bak", object)
  127. copyObj, err := svc.CopyObject(&s3.CopyObjectInput{
  128. Bucket: aws.String(Bucket),
  129. CopySource: aws.String(fmt.Sprintf("%s/%s", Bucket, object)),
  130. Key: aws.String(dest),
  131. })
  132. if err != nil {
  133. exitErrorf("Unable to copy object, %v", err)
  134. }
  135. t.Log("copy object result -> ", copyObj.CopyObjectResult)
  136. getObj, err := svc.GetObject(&s3.GetObjectInput{
  137. Bucket: aws.String(Bucket),
  138. Key: aws.String(dest),
  139. })
  140. if err != nil {
  141. exitErrorf("Unable to get copy object, %v", err)
  142. }
  143. data, err := io.ReadAll(getObj.Body)
  144. if err != nil {
  145. exitErrorf("Unable to read object data, %v", err)
  146. }
  147. if string(data) != Data {
  148. t.Error("object data -> ", string(data))
  149. }
  150. listObj, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{
  151. Bucket: aws.String(Bucket),
  152. Prefix: aws.String("foo/"),
  153. })
  154. if err != nil {
  155. exitErrorf("Unable to list objects, %v", err)
  156. }
  157. count := 0
  158. for _, content := range listObj.Contents {
  159. key := aws.StringValue(content.Key)
  160. if key == dest {
  161. count++
  162. } else if key == object {
  163. count++
  164. }
  165. if count == 2 {
  166. break
  167. }
  168. }
  169. if count != 2 {
  170. exitErrorf("Unable to find two objects, %v", listObj.Contents)
  171. }
  172. _, err = svc.DeleteObject(&s3.DeleteObjectInput{
  173. Bucket: aws.String(Bucket),
  174. Key: aws.String(object),
  175. })
  176. if err != nil {
  177. exitErrorf("Unable to delete source object, %v", err)
  178. }
  179. _, err = svc.DeleteObject(&s3.DeleteObjectInput{
  180. Bucket: aws.String(Bucket),
  181. Key: aws.String(dest),
  182. })
  183. if err != nil {
  184. exitErrorf("Unable to delete object, %v", err)
  185. }
  186. _, err = svc.DeleteBucket(&s3.DeleteBucketInput{
  187. Bucket: aws.String(Bucket),
  188. })
  189. if err != nil {
  190. exitErrorf("Unable to delete bucket, %v", err)
  191. }
  192. }