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

问题描述

我使用以将照片上传到S3。我测试了我的S3桶,并与NG-文件上传我的政策/签名生成, 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:31
查看更多