Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multipart uploads for creating S3 objects #14382

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions aws/resource_aws_s3_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -196,8 +197,8 @@ func resourceAwsS3BucketObject() *schema.Resource {
}
}

func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
func resourceAwsS3BucketObjectUpload(d *schema.ResourceData, meta interface{}) error {
uploader := s3manager.NewUploaderWithClient(meta.(*AWSClient).s3conn)

var body io.ReadSeeker

Expand Down Expand Up @@ -236,81 +237,81 @@ func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) erro
bucket := d.Get("bucket").(string)
key := d.Get("key").(string)

putInput := &s3.PutObjectInput{
uploadInput := &s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
ACL: aws.String(d.Get("acl").(string)),
Body: body,
}

if v, ok := d.GetOk("storage_class"); ok {
putInput.StorageClass = aws.String(v.(string))
uploadInput.StorageClass = aws.String(v.(string))
}

if v, ok := d.GetOk("cache_control"); ok {
putInput.CacheControl = aws.String(v.(string))
uploadInput.CacheControl = aws.String(v.(string))
}

if v, ok := d.GetOk("content_type"); ok {
putInput.ContentType = aws.String(v.(string))
uploadInput.ContentType = aws.String(v.(string))
}

if v, ok := d.GetOk("metadata"); ok {
putInput.Metadata = stringMapToPointers(v.(map[string]interface{}))
uploadInput.Metadata = stringMapToPointers(v.(map[string]interface{}))
}

if v, ok := d.GetOk("content_encoding"); ok {
putInput.ContentEncoding = aws.String(v.(string))
uploadInput.ContentEncoding = aws.String(v.(string))
}

if v, ok := d.GetOk("content_language"); ok {
putInput.ContentLanguage = aws.String(v.(string))
uploadInput.ContentLanguage = aws.String(v.(string))
}

if v, ok := d.GetOk("content_disposition"); ok {
putInput.ContentDisposition = aws.String(v.(string))
uploadInput.ContentDisposition = aws.String(v.(string))
}

if v, ok := d.GetOk("server_side_encryption"); ok {
putInput.ServerSideEncryption = aws.String(v.(string))
uploadInput.ServerSideEncryption = aws.String(v.(string))
}

if v, ok := d.GetOk("kms_key_id"); ok {
putInput.SSEKMSKeyId = aws.String(v.(string))
putInput.ServerSideEncryption = aws.String(s3.ServerSideEncryptionAwsKms)
uploadInput.SSEKMSKeyId = aws.String(v.(string))
uploadInput.ServerSideEncryption = aws.String(s3.ServerSideEncryptionAwsKms)
}

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
// The tag-set must be encoded as URL Query parameters.
putInput.Tagging = aws.String(keyvaluetags.New(v).IgnoreAws().UrlEncode())
uploadInput.Tagging = aws.String(keyvaluetags.New(v).IgnoreAws().UrlEncode())
}

if v, ok := d.GetOk("website_redirect"); ok {
putInput.WebsiteRedirectLocation = aws.String(v.(string))
uploadInput.WebsiteRedirectLocation = aws.String(v.(string))
}

if v, ok := d.GetOk("object_lock_legal_hold_status"); ok {
putInput.ObjectLockLegalHoldStatus = aws.String(v.(string))
uploadInput.ObjectLockLegalHoldStatus = aws.String(v.(string))
}

if v, ok := d.GetOk("object_lock_mode"); ok {
putInput.ObjectLockMode = aws.String(v.(string))
uploadInput.ObjectLockMode = aws.String(v.(string))
}

if v, ok := d.GetOk("object_lock_retain_until_date"); ok {
putInput.ObjectLockRetainUntilDate = expandS3ObjectLockRetainUntilDate(v.(string))
uploadInput.ObjectLockRetainUntilDate = expandS3ObjectLockRetainUntilDate(v.(string))
}

if _, err := s3conn.PutObject(putInput); err != nil {
return fmt.Errorf("Error putting object in S3 bucket (%s): %s", bucket, err)
if _, err := uploader.Upload(uploadInput); err != nil {
return fmt.Errorf("Error uploading object to S3 bucket (%s): %s", bucket, err)
}

d.SetId(key)
return resourceAwsS3BucketObjectRead(d, meta)
}

func resourceAwsS3BucketObjectCreate(d *schema.ResourceData, meta interface{}) error {
return resourceAwsS3BucketObjectPut(d, meta)
return resourceAwsS3BucketObjectUpload(d, meta)
}

func resourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -421,7 +422,7 @@ func resourceAwsS3BucketObjectUpdate(d *schema.ResourceData, meta interface{}) e
"website_redirect",
} {
if d.HasChange(key) {
return resourceAwsS3BucketObjectPut(d, meta)
return resourceAwsS3BucketObjectUpload(d, meta)
}
}

Expand Down