concatObjects.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // +build example
  2. package main
  3. import (
  4. "log"
  5. "net/url"
  6. "os"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/session"
  9. "github.com/aws/aws-sdk-go/service/s3"
  10. )
  11. type client struct {
  12. s3Client *s3.S3
  13. bucket *string
  14. }
  15. // concatenate will contenate key1's object to key2's object under the key testKey
  16. func (c *client) concatenate(key1, key2, key3 string, uploadID *string) (*string, *string, error) {
  17. // The first part to be uploaded which is represented as part number 1
  18. foo, err := c.s3Client.UploadPartCopy(&s3.UploadPartCopyInput{
  19. Bucket: c.bucket,
  20. CopySource: aws.String(url.QueryEscape(*c.bucket + "/" + key1)),
  21. PartNumber: aws.Int64(1),
  22. Key: &key3,
  23. UploadId: uploadID,
  24. })
  25. if err != nil {
  26. return nil, nil, err
  27. }
  28. // The second part that is going to be appended to the newly created testKey
  29. // object.
  30. bar, err := c.s3Client.UploadPartCopy(&s3.UploadPartCopyInput{
  31. Bucket: c.bucket,
  32. CopySource: aws.String(url.QueryEscape(*c.bucket + "/" + key2)),
  33. PartNumber: aws.Int64(2),
  34. Key: &key3,
  35. UploadId: uploadID,
  36. })
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. // The ETags are needed to complete the process
  41. return foo.CopyPartResult.ETag, bar.CopyPartResult.ETag, nil
  42. }
  43. func main() {
  44. if len(os.Args) < 4 {
  45. log.Println("USAGE ERROR: AWS_REGION=us-east-1 go run concatenateObjects.go <bucket> <key for object 1> <key for object 2> <key for output>")
  46. return
  47. }
  48. bucket := os.Args[1]
  49. key1 := os.Args[2]
  50. key2 := os.Args[3]
  51. key3 := os.Args[4]
  52. sess := session.New(&aws.Config{})
  53. svc := s3.New(sess)
  54. c := client{svc, &bucket}
  55. // We let the service know that we want to do a multipart upload
  56. output, err := c.s3Client.CreateMultipartUpload(&s3.CreateMultipartUploadInput{
  57. Bucket: &bucket,
  58. Key: &key3,
  59. })
  60. if err != nil {
  61. log.Println("ERROR:", err)
  62. return
  63. }
  64. foo, bar, err := c.concatenate(key1, key2, key3, output.UploadId)
  65. if err != nil {
  66. log.Println("ERROR:", err)
  67. return
  68. }
  69. // We finally complete the multipart upload.
  70. _, err = c.s3Client.CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{
  71. Bucket: &bucket,
  72. Key: &key3,
  73. UploadId: output.UploadId,
  74. MultipartUpload: &s3.CompletedMultipartUpload{
  75. Parts: []*s3.CompletedPart{
  76. {
  77. ETag: foo,
  78. PartNumber: aws.Int64(1),
  79. },
  80. {
  81. ETag: bar,
  82. PartNumber: aws.Int64(2),
  83. },
  84. },
  85. },
  86. })
  87. if err != nil {
  88. log.Println("ERROR:", err)
  89. return
  90. }
  91. }