通过NG-文件上传上传到Amazon S3;错误400 [英] Uploading to Amazon S3 via ng-file-upload; Error 400

查看:3761
本文介绍了通过NG-文件上传上传到Amazon S3;错误400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NG-文件上传以将照片上传到S3。我测试了我的S3桶,并与NG-文件上传我的政策/签名生成演示工具并已成功用它的照片上传到我的桶中。


编辑:的一个关键的区别似乎是增加了一个头的:

授权:令牌3j4fl8jk0lqfkj4izj2w3ljfopljlwep1010notreal

这是在我的code present但不是在演示页。也许是我的角度应用程序。


通过安装鲍尔,采取了相关的组件,试图上传文件,并得到看起来像这样的错误:

  400?< XML版本=1.0编码=UTF-8>?
 <错误>
 < code基InvalidArgument< / code>
 <消息>不支持的授权类型< /信息>
 < ArgumentName>授权< / ArgumentName>
 < ArgumentValue>令牌3j4fl8jk0lqfkj4izj2w3ljfopljlwep1010notreal< / ArgumentValue>
 <&的requestId GT; F1F5FK - 不实81FED9CA< /&的requestId GT;
 <主机标识> CQEW98p3D2e + PVZ - 不实codeu28m7asqpGjagL3409gj3f4kijKJpofk< /主机ID>
 < /错误>

搜索周围,我注意到很多400错误,但与 ArgumentValue 令牌[some- code-这里就不多的情况下]

看着 AWS文档 InvalidArgument 是有人相当新的AWS有点不透明。

这是我在编码策略(蟒蛇):

  #exptime ='2100-07-31T06:23:35Z#for调试
policy_document = {过期:exptime,
    条件: [
      {桶:settings.AWS_STORAG​​E_BUCKET_NAME},
      [开始,以'$钥匙,],
      {以acl:私有},
      [开始-带,$内容类型,],
      [开始-带,$文件名,],
      [内容长度范围,0,5000000]
    ]
  }

要重申,这一政策与上述演示工作,所以我希望在我的前端设计了一个问题:

  $ scope.upload =功能(文件){
    file.upload = Upload.upload({
                网址:'https://mybucket.s3-us-west-2.amazonaws.com',
                方法:POST,
                字段:{
                    键:file.name,
                    AWSAccessKeyId:myAccessKeyId,
                    ACL:私人,
                    政策:myPolicyEn codeD,
                    签名:mySignatureEn codeD,
                    内容类型:file.type ===空||
                        file.type ==='? 应用程序/八位字节流:file.type,
                    文件名:file.name
                },
                文件:文件
            });
}


解决方案

我花了一段时间才能找到它,但我的编辑指出,展望请求头中的角应用程序的登录/ AUTH过程中使用的令牌是发送作为默认,和亚马逊的服务不喜欢这一点。删除页眉在这种特殊情况下的http请求解决问题。

在我的前端上传服务这样的样子:

  $ scope.upload =功能(文件){
    file.upload = Upload.upload({
                网址:'https://mybucket.s3-us-west-2.amazonaws.com',
                方法:POST,
                字段:{
                    键:file.name,
                    AWSAccessKeyId:myAccessKeyId,
                    ACL:私人,
                    政策:myPolicyEn codeD,
                    签名:mySignatureEn codeD,
                    内容类型:file.type ===空||
                        file.type ==='? 应用程序/八位字节流:file.type,
                    文件名:file.name
                },
                标题:{
                    '授权':未定义
                },
                文件:文件
            });
}

I am using ng-file-upload to upload a photo to S3. I have tested my S3 bucket and my policy/signature generator with the ng-file-upload demo tool and have successfully used it to upload the photo to my bucket.


Edit: A key difference seems to be the addition of a header:

Authorization:Token 3j4fl8jk0lqfkj4izj2w3ljfopljlwep1010notreal

That is present in my code but not in the demo page. Perhaps it's my angular app.


Installed through bower, took the relevant components, tried to upload a file, and get an error that looks like this:

 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>

Searching around, I've noted many 400 Errors but not many cases with the ArgumentValue as Token [some-code-here].

Looking at AWS documentation, InvalidArgument is a bit opaque for someone rather new to AWS.

And here is the policy that I'm encoding (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] 
    ]
  }

To reiterate, this policy with the aforementioned demo worked, so I expect a problem on my front-end implementation:

$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
            });
}

解决方案

Took me a while to find it, but as my edit pointed out, looking into the request header a token used in the Angular app's login/auth process was being sent as a default, and Amazon's service didn't like that. Removing the header in the http request for this specific case solved the issue.

The upload service on my front-end thus looks like:

$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
            });
}

这篇关于通过NG-文件上传上传到Amazon S3;错误400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆