问题描述
我正在服务器端生成带有以下参数的预签名URL请求,用于 GeneratePresignedUrlRequest :bucket
,key
,expiration = in 1 hour
和method = PUT
.
I am generating in server side a pre-signed URL request with the following parameters for GeneratePresignedUrlRequest : bucket
, key
, expiration = in 1 hour
and method = PUT
.
在Angular应用中,我使用 ng-file-up 上传文件
In my Angular app, I am uploading the file using ng-file-upload
Upload.http({
url: $scope.signedUrl,
method: "PUT",
headers : {
'Content-Type': $scope.file.type
},
data: $scope.file
});
问题在于,除非我在GeneratePresignedUrlRequest.contentType
中设置文件的类型,否则我始终会收到403
响应.
The problem is that I always have a 403
response unless I set the type of the file in GeneratePresignedUrlRequest.contentType
.
问题是我无法预先预测用户将选择哪种文件类型(image/png
,image/jpeg
,text/plain
...).
The problem is that I can't predict in advance what type of file the user will choose (image/png
, image/jpeg
, text/plain
...).
如何生成一个接受所有content-type
的预签名URL?我尝试将其设置为null,它会继续发送403错误.
How can I generate a pre-signed url that accept all kinds of content-type
? I tried setting it to null, it keeps sending 403 errors.
谢谢.
推荐答案
我只是遇到了这个问题,并使其正常工作.将您的Upload.http代码替换为以下内容:
I just ran into this problem, and just got it working. Replace your Upload.http code with the following:
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);
问题最终导致S3正在寻找特定的Content-Type(binary/octet-stream
),它会在您省略Content-Type标头时推断出来.
The problem ends up being that S3 is looking for a specific Content-Type (binary/octet-stream
), which it infers when you omit the Content-Type header.
这篇关于如何在不知道Content-Type的情况下生成AWS S3预签名URL请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!