1

I'd like to upload files to my s3 bucket via the aws golang sdk. I have a web server listening to POST requests and I'm expecting to receive multiple files of any type.

Using the sdk, the s3 struct PutObjectInput expects Body to be of type io.ReadSeeker and I'm not sure how to extract the content from the files uploaded and in turn satisfy the io.ReadSeeker interface.

images := r.MultipartForm.File

for _, files := range images {
    for _, f := range files {

        # In my handler, I can loop over the files
        # and see the content
        fmt.Println(f.Header)

        _, err = svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
                Bucket: aws.String("bucket"),
                Key:    aws.String("key"),
                Body:   FILE_CONTENT_HERE,
            })
    }
}

2 Answers 2

3

Use the FileHeader.Open method to get an io.ReadSeeker.

    f, err := f.Open()
    if err != nil {
       // handle error
    }

    _, err = svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
            Bucket: aws.String("bucket"),
            Key:    aws.String("key"),
            Body:   f,
        })

Open returns a File. This type satisfies the io.ReadSeeker interface.

Sign up to request clarification or add additional context in comments.

Comments

-2

Use the S3 Manager's Uploader.Upload method, http://docs.aws.amazon.com/sdk-for-go/api/service/s3/s3manager/#Uploader.Upload. We have an example at http://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/s3-example-basic-bucket-operations.html#s3-examples-bucket-ops-upload-file-to-bucket.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.