rch*_*rch 1 python file-upload amazon-s3 amazon-web-services angularjs
我正在使用ng-file-upload将照片上传到 S3。我已经使用 ng-file-upload演示工具测试了我的 S3 存储桶和策略/签名生成器,并成功使用它将照片上传到我的存储桶。
编辑:一个关键的区别似乎是添加了标题:
Authorization:Token 3j4fl8jk0lqfkj4izj2w3ljfopljlwep1010notreal
这存在于我的代码中,但不存在于演示页面中。也许这是我的角度应用程序。
通过 Bower 安装,获取相关组件,尝试上传文件,并收到如下错误:
400: <?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>InvalidArgument</Code>
<Message>Unsupported Authorization Type</Message>
<ArgumentName>Authorization</ArgumentName>
<ArgumentValue>Token 3j4fl8jk0lqfkj4izj2w3ljfopljlwep1010notreal</ArgumentValue>
<RequestId>F1F5FK-not-real-81FED9CA</RequestId>
<HostId>CQEW98p3D2e+pVz-not-real-codeu28m7asqpGjagL3409gj3f4kijKJpofk</HostId>
</Error>
Run Code Online (Sandbox Code Playgroud)
环顾四周,我注意到许多 400 错误,但与ArgumentValueas相关的情况并不多Token [some-code-here]。
对于 AWS 新手来说,查看AWS 文档有点不透明。InvalidArgument
这是我正在编码的策略(Python):
#exptime = '2100-07-31T06:23:35Z' #for debugging
policy_document = {"expiration": exptime,
"conditions": [
{"bucket": settings.AWS_STORAGE_BUCKET_NAME},
["starts-with", '$key', ""],
{"acl": "private"},
["starts-with", "$Content-Type", ""],
["starts-with", "$filename", ""],
["content-length-range", 0, 5000000]
]
}
Run Code Online (Sandbox Code Playgroud)
重申一下,上述演示中的该策略有效,因此我预计我的前端实现会出现问题:
$scope.upload = function(file) {
file.upload = Upload.upload({
url: 'https://mybucket.s3-us-west-2.amazonaws.com',
method: 'POST',
fields: {
key: file.name,
AWSAccessKeyId: myAccessKeyId,
acl: 'private',
policy: myPolicyEncoded,
signature: mySignatureEncoded,
"Content-Type": file.type === null ||
file.type === '' ? 'application/octet-stream' : file.type,
filename: file.name
},
file: file
});
}
Run Code Online (Sandbox Code Playgroud)
我花了一段时间才找到它,但正如我的编辑指出的那样,查看请求标头,Angular 应用程序的登录/身份验证过程中使用的令牌将作为默认值发送,而亚马逊的服务不喜欢这样。删除此特定情况的 http 请求中的标头解决了该问题。
我前端的上传服务如下所示:
$scope.upload = function(file) {
file.upload = Upload.upload({
url: 'https://mybucket.s3-us-west-2.amazonaws.com',
method: 'POST',
fields: {
key: file.name,
AWSAccessKeyId: myAccessKeyId,
acl: 'private',
policy: myPolicyEncoded,
signature: mySignatureEncoded,
"Content-Type": file.type === null ||
file.type === '' ? 'application/octet-stream' : file.type,
filename: file.name
},
headers: {
'Authorization': undefined
},
file: file
});
}
Run Code Online (Sandbox Code Playgroud)