Mike
Mike

Reputation: 2044

Multiple file upload using s3

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,
            })
    }
}

Upvotes: 1

Views: 2454

Answers (2)

Thundercat
Thundercat

Reputation: 120960

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.

Upvotes: 3

Related Questions