andy mccullough
andy mccullough

Reputation: 9541

Javascript - Uploading CSV to S3 by presigned URL - file contains boundary data

I am uploading a CSV file to S3 using a presigned URL, but when I open the file in S3 I notice that the data is wrapped in the multipart formdata / boundary information. How do I get only the raw data to be in the file?

I have -

Generating the presigned URL

   return s3.getSignedUrl('putObject', {
        Bucket: BUCKET_NAME,
        Key: 'temp.csv',
        Expires: 30,
    });

In postman - I have it set to a PUT request - I have no extra headers - I have the file added to the body of the request using the postman UI

Request Body -

Request Body

Request Headers -

Request Headers

Contents of the CSV look like -

enter image description here

Upvotes: 1

Views: 1987

Answers (2)

Somnath Swami
Somnath Swami

Reputation: 1

Just send you file directly don't use formData object to send file. I have used Axios library but you can also use js fetch method just remember in data send file like I sent fileContents

let fileContent = event.target.files[0];
use following code to call api


    axios({
      method: 'PUT',
      url: apiToken,
      headers: {
        'Content-Type': 'text/csv'
      },
      data: fileContent
    }).then(res => {
      if(res.status === 200){
        console.log('File uploaded successfully !');
      }else{

        console.error("Error while Uploading File Try again !");
      }
    })

Upvotes: 0

andy mccullough
andy mccullough

Reputation: 9541

Got this working - looks to be because of Postman setting the Content-Type to multipart/form-data. I changed the request body to be of type binary and uploaded the file to the postman UI that way. I manually set the Content-Type to text/csv

Upvotes: 3

Related Questions