如何在不知道Content-Type的情况下生成AWS S3预先签名的URL请求?

c4k*_*c4k 7 amazon-s3 amazon-web-services angularjs pre-signed-url

我生成的服务器端与以下参数预先签署的URL请求GeneratePresignedUrlRequest:bucket,key,expiration = in 1 hourmethod = PUT.

在我的Angular应用程序中,我使用ng-file-upload上传文件

Upload.http({
    url: $scope.signedUrl,
    method: "PUT",
    headers : {
        'Content-Type': $scope.file.type
    },
    data: $scope.file
});
Run Code Online (Sandbox Code Playgroud)

问题是我总是有一个403响应,除非我设置文件的类型GeneratePresignedUrlRequest.contentType.

问题是,我不能提前预知的用户会选择什么类型的文件(image/png,image/jpeg,text/plain...).

如何生成接受各种类型的预签名网址content-type?我尝试将其设置为null,它一直发送403错误.

谢谢.

小智 6

我刚刚遇到了这个问题,并且刚刚开始工作。将您的 Upload.http 代码替换为以下内容:

var reader = new FileReader();
var xhr = new XMLHttpRequest();

xhr.open("PUT", $scope.signedUrl);
reader.onload = function(evt) {
  xhr.send(evt.target.result);
};
reader.readAsArrayBuffer($scope.file);
Run Code Online (Sandbox Code Playgroud)

问题最终在于 S3 正在寻找特定的 Content-Type ( binary/octet-stream),当您省略 Content-Type 标头时,它会推断出该内容。


Tit*_*ore -3

一种可能的解决方案可能是如果您跟踪扩展程序?例如:以“.jpg”结尾 -> 内容类型 =“image/jpeg”,以“.zip”结尾 -> 内容类型 =“application/octet-stream”。

参考:通过javascript获取文档中文件上传的文件名