Created
November 11, 2021 23:50
-
-
Save halkyon/108ed1a5aa47aae86c16dd0cff41afd9 to your computer and use it in GitHub Desktop.
multipart upload programatically in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"os" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3" | |
"storj.io/common/memory" | |
"storj.io/common/testrand" | |
) | |
func main() { | |
sess, err := session.NewSession(&aws.Config{ | |
Endpoint: aws.String(os.Getenv("AWS_ENDPOINT")), | |
Region: aws.String("us-east-1"), | |
}) | |
if err != nil { | |
panic(err) | |
} | |
svc := s3.New(sess) | |
var ( | |
testbucket = aws.String("test-files") | |
testkey = aws.String("testkey") | |
) | |
m, err := svc.CreateMultipartUpload(&s3.CreateMultipartUploadInput{ | |
Bucket: testbucket, | |
Key: testkey, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("started UploadId=%s\n", *m.UploadId) | |
var completed []*s3.CompletedPart | |
for i := 1; i <= 10; i++ { | |
p, err := svc.UploadPart(&s3.UploadPartInput{ | |
Body: aws.ReadSeekCloser(bytes.NewReader(testrand.Bytes(memory.KiB * 1))), | |
Bucket: testbucket, | |
Key: testkey, | |
PartNumber: aws.Int64(int64(i)), | |
UploadId: m.UploadId, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
completed = append(completed, &s3.CompletedPart{ | |
ETag: p.ETag, | |
PartNumber: aws.Int64(int64(i)), | |
}) | |
fmt.Printf("uploaded part %d\n", i) | |
} | |
_, err = svc.CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{ | |
Bucket: testbucket, | |
Key: testkey, | |
UploadId: m.UploadId, | |
MultipartUpload: &s3.CompletedMultipartUpload{ | |
Parts: completed, | |
}, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment