POST requires exactly one file upload per request

Viewed 399

I'm using nestjs and i want to upload to amazon s3, but every requests fails and error is:
POST requires exactly one file upload per request

Here is my code implementation:

const formData = new FormData();
formData.append('policy', clientPayload.policy);
formData.append('key', clientPayload.key);
formData.append('x-amz-signature', clientPayload['x-amz-signature']);
formData.append('x-amz-algorithm', clientPayload['x-amz-algorithm']);
formData.append('x-amz-date', clientPayload['x-amz-date']);
formData.append('x-amz-credential', clientPayload['x-amz-credential']);
formData.append('success_action_status', '201');
formData.append('success_action_redirect', '');
formData.append('file', file.path);

try {
  const data = await this.http
    .post(AppConfig.awsServices.bucketUrl, formData, {
      headers: {
        'content-type':
          'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
      },
    })
    .toPromise();

And this is the response:

data: '<?xml version="1.0" encoding="UTF-8"?>\n' +
  '<Error><Code>InvalidArgument</Code><Message>POST requires exactly one file upload per request.</Message><ArgumentName>file</ArgumentName><ArgumentValue>0</ArgumentValue><RequestId>D602243726B03B53</RequestId><HostId>qjLwo8jak8yb3iWOXp4fbqAw391MW7d/3/9r8AzqA20hAvYd1Kgj0PJIAEv+v9sMckixT9WtUoA=</HostId></Error>'

Here is image attachment as well: Code example

And this is the request i'm trying to implement from docs:

https://dev.vdocipher.com/api/docs/book/upload/file.html

1 Answers

Looks like you're not actually uploading a file, you're just passing a path to the file. Change it to something like:

const formData = new FormData();
// your .append() calls here
// instead of formData.append('file', file.path);
form.append('file', fs.createReadStream('path-to-file'));

Also do not specify the content-type header manually, the httpService should take care of this:

 const data = await this.http
    .post(AppConfig.awsServices.bucketUrl, formData, { headers: { ...formData.getHeaders() })
    .toPromise();
Related