Cannot upload file to DigitalOcean spaces using ACL as public-read using signed POST URL?

Basically the same question as this but using POST instead of PUT. Using POST, I can add other conditions like file size limit, content-type limit, etc.

import { createPresignedPost } from '@aws-sdk/s3-presigned-post';

// client S3Client instance from AWS SDK v3.
const url = await createPresignedPost(client, {
  Bucket: 'myhpbucket',
  Key: 'accordion.gif',
  Conditions: [
    ['eq', '$acl', 'public-read'],
    ['starts-with', '$Content-Type', 'image/'],
    ['content-length-range', 1, 512 * 1024]  // 512KB size restriction
  ],
  Expires: 3 * 3600 // 3 hour expiry
});

This response is similar to this:

{
  url: 'https://fra1.digitaloceanspaces.com/myhpbucket',
  fields: {
    bucket: 'myhpbucket',
    'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
    'X-Amz-Credential': 'some-value',
    'X-Amz-Date': '20210517T125438Z',
    key: 'accordion.gif',
    Policy: 'policy-id',
    'X-Amz-Signature': 'signature'
  }
}

I use this response on the front-end using XHR2.

const data = new FormData();

data.append('bucket', 'myhpbucket');
data.append('key', 'accordion.gif');
data.append('Policy', 'policy-id');
data.append('X-Amz-Date', '20210517T125438Z');
data.append('X-Amz-Signature', 'signature');
data.append('X-Amz-Algorithm', 'AWS4-HMAC-SHA256');
data.append('X-Amz-Credential', 'some-value');

// Required for satisfying condition
data.append('Content-Type', 'image/gif');

// Added file as last key of the form data
data.append('file', fileInput.files[0], '/C:/Users/patilh6/Desktop/accordion.gif');

const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://fra1.digitaloceanspaces.com/precommit');

// ACL request header to satisfy condition
xhr.setRequestHeader('x-amz-acl', 'public-read');

xhr.send(data);

I tried with other conditions by removing ACL and upload worked flawlessly. Am I missing something here with ACL condition?

Source: Ask Javascript Questions

LEAVE A COMMENT