【问题标题】:dropzone.js direct upload to S3 with content-typedropzone.js 使用 content-type 直接上传到 S3
【发布时间】:2017-11-16 20:58:30
【问题描述】:

我目前正在使用 dropzone.js 通过预签名 URL 将图像上传到 S3。一切正常,除了我无法设置正在上传的文件的内容类型。默认情况下,它们都是使用binary/octet-stream 上传的,我无法直接在浏览器中查看它们。

我的 S3 预签名策略如下所示:

const policy = s3PolicyV4.generate({
    key: key,
    bucket: process.env.S3_BUCKET,
    contentType: 'multipart/form-data',
    region: process.env.REGION,
    accessKey: process.env.ACCESS_KEY_ID,
    secretKey: process.env.SECRET_ACCESS_KEY,
});

我尝试在此处更改 contentType 键但没有成功,并且在进行一些研究后我也尝试添加此键。

conditions: [
   ["starts-with", "$Content-Type", ""]
]

这是我将预签名 URL 的值添加到 dropzone.js 选项的前端代码。

$.ajax({
    type: "POST",
    contentType: "application/json",
    dataType: "json",
    url: api_endpoint,
    cache: false,
    success: function(data) {
        s3_filename_key = data.key;
        $this.options.params = {
            key: data.key,
            acl: data.acl,
            success_action_status: data.success_action_status,
            "X-Amz-Credential": data['X-Amz-Credential'],
            "X-Amz-Algorithm": data['X-Amz-Algorithm'],
            "X-Amz-Date": data['X-Amz-Date'],
            "Policy": data.Policy,
            "X-Amz-Signature": data['X-Amz-Signature']
        }
        done();
    },
    error: function(data) {}
});

当我将 Content-Type 添加到 dropzone 选项时,我得到了这个结果 - Invalid according to Policy: Extra input fields: content-type

这是我的存储桶 CORS 配置。

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <ExposeHeader>ETag</ExposeHeader>
    <ExposeHeader>Content-length</ExposeHeader>
    <AllowedHeader>*</AllowedHeader>
    <AllowedHeader>Content-*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

【问题讨论】:

  • 你解决过这个问题吗?我也面临同样的问题。
  • 在你的 dropzone options.params 我添加了一个额外的密钥 Content-Type。我在 dropzone 配置中还有一个 acceptedFiles 列表。和[["starts-with", "$Content-Type", "image/"]] 在 S3 条件中

标签: javascript amazon-s3 dropzone.js


【解决方案1】:

尝试添加正确的 contentType,而不是您定义了 enctype。例如:

const policy = s3PolicyV4.generate({
 key: key,
 bucket: process.env.S3_BUCKET,
 contentType: 'application/json',
 region: process.env.REGION,
 accessKey: process.env.ACCESS_KEY_ID,
 secretKey: process.env.SECRET_ACCESS_KEY,
});

Multypart/form-data 是表单的 enctype 属性。

【讨论】:

  • 我使用application/pdf对我上传的PDF进行了尝试
【解决方案2】:

我们在 nodejs 中使用以下解决方案。

const mimeobj = FileType(buffer);

const s3Options = {
    ACL: 'public-read',
    Body: buffer,
    ContentType: mimeobj.mime,
    Key: UUID + '.' + mimeobj.ext,
    Bucket: 'your-bucket-name'
};

S3.upload(s3Options).send((err, data) => {

    if (err) {
        return reply(Boom.badImplementation('Internal Server Error'));
    }

    const responseObj = {
        orgFileName: decodedFileName,
        name:uuId + '.' + mimeobj.ext, 
        url: data.Location
    };

    responseArray.push(responseObj);

    count++;
    if (count === fileCount) {
        return reply({ files: responseArray });
    }
});

【讨论】:

    猜你喜欢
    • 2014-06-24
    • 2023-04-02
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2018-01-11
    相关资源
    最近更新 更多