AWS S3代理保存原始表单数据,而不是实际文件。为什么?

时间:2018-06-27 10:36:39

标签: ajax amazon-web-services amazon-s3 multipartform-data form-data

大家早上好。

我正尝试使用AWS API网关从Web浏览器直接将二进制文件(图片或音频文件)上传到S3存储桶,但要避免遵循lambda函数和已签名的URL,

https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html

因为在这种情况下multipart / form-data似乎是唯一的选择,所以我使用jQuery和Ajax使用FormData上载文件。这是代码段:

var fd = new FormData();
fd.append('Content-Type', file.type);
fd.append("success_action_status", null);
fd.append("file",<file>);

$.ajax({
    url: ***,
    data: fd,
    type: "PUT",
    processData: false,     
    crossDomain: true,
    headers: {
        "Authorization": ***,
        "x-amz-meta-filename" : file.name,
        "Content-Type" : "multipart/form-data"
    }
})
    .done(function(data ) {
        callback(null,data);
    })
    // Code to run if the request fails; the raw request and
    // status codes are passed to the function
    .fail(function( xhr, status, errorThrown ) {            
        callback({status: xhr.status, message: xhr.responseText}, null);
    });

我能够在网关中创建API,并且我还设置为将“ multipart / form-data”传递到S3,以防止将表单数据从二进制转换为文本。

不幸的是,尽管文件上传成功,但是S3会存储原始表单数据而不是实际文件,从而使第二次检索时不可读。

这是在S3中创建并使用文本编辑器打开的“二进制”文件的摘要。原始文件是图片...

------WebKitFormBoundaryHZlvlkeiOZpIVeJC
Content-Disposition: form-data; name="Content-Type"

image/jpeg
------WebKitFormBoundaryHZlvlkeiOZpIVeJC
Content-Disposition: form-data; name="success_action_status"

null
------WebKitFormBoundaryHZlvlkeiOZpIVeJC
Content-Disposition: form-data; name="file"; filename="index.jpg"
Content-Type: image/jpeg

ÿØÿà JFIF      ÿÛ „    ( %!1!%)+...383-7(-.+

我不知道这种行为,也不知道如何使S3存储文件而不是原始有效载荷。

非常感谢您的帮助。

谢谢。

0 个答案:

没有答案