基本上与此相同的问题,但使用POST
而不是PUT
. 使用 POST,我可以添加其他条件,例如文件大小限制、内容类型限制等。
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
});
此响应与此类似:
{
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'
}
}
我在前端使用这个响应,使用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);
我通过删除 ACL 尝试了其他条件,并且上传工作完美无缺。我在 ACL 条件下遗漏了什么吗?