2017-06-19 61 views
1

我想通过带有签名URL的Cloudfront将文件上传到S3。 Cloudfront行为中允许HTTP PUT。 铲斗政策Amazon S3无法通过Cloudfront上传文件

   { 
     "Sid": "2", 
     "Effect": "Allow", 
     "Principal": { 
      "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity E1C2T5UJU07REZ" 
     }, 
     "Action": [ 
      "s3:PutObject", 
      "s3:PutObjectAcl", 
      "s3:GetObject" 
     ], 
     "Resource": "arn:aws:s3:::testback/*" 
    } 

HTTP PUT是允许在CORS配置。 Cloudfront用户也具有读取,写入权限。 当我尝试使用签名的URL上载文件时。

curl -v -X PUT -F [email protected] http://my-host.cloudfront.net/hello.txt?Expires=1514764800&Signature=MySig&Key-Pair-Id=My-KeyPair 

我得到了一个错误:

InvalidRequest The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256. 5D5DEF3D06F4583C8rBCpTUzYwm1ccc8IfkNtUnkuLxr3RZ2n7xn1j+VvP5dpG+3NMpHKPiNQ5tKpJjVliZ9UBI52vk=

登录:

2017-06-19 03:23:08 FRA54 726 My-IP PUT
my-host.cloudfront.net /hello.txt 400 -
curl/7.50.1
Expires=1514764800&Signature=My-Sig&Key-Pair-Id=My-KeyPair - Error MMHwKFzGuBzrlgP0yV71elcwEp2RVBAwJRJD1A5rO4Na6UmeKvcZPQ==
my-host.cloudfront.net http 838 0.235 - --
Error HTTP/1.1

而且,获取,删除工作正常。

回答

1

从CloudFront的文档:

If you're using an origin access identity and if your bucket is in one of the regions that requires signature version 4 for authentication, note the following:

DELETE , GET , HEAD , OPTIONS , and PATCH requests are supported without qualifications.

If you want to submit PUT requests to CloudFront to upload objects to your Amazon S3 bucket, you must add an x-amz-content-sha256 header to the request, and the header value must contain a SHA256 hash of the body of the request.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html#private-content-origin-access-identity-signature-version-4

你需要添加在x-amz-content-sha256请求头的对象体的SHA256的十六进制表示。

CloudFront会在PUT中使用将此头文件包含在内部作为内部切换到V4签名算法的神奇触发器似乎令人意外...但由于它对上传是强制性的,因此V4签名将无效没有它,所以也许它甚至不会尝试V4(CloudFront在签名版V4之前)。对于其他操作,V4不需要修改用户代理的行为,但对PUT而言,如果CloudFront在缺省情况下使用V2作为默认值,那么即使这是错误的,也可以使现有代码不会不会在已经工作的地方突破。无论如何,这看起来应该是你的解决方案。

+0

@unknown是否解决了您的问题?是否[此AWS论坛帖子](https://forums.aws.amazon.com/thread.jspa?threadID=258104)您的也是? –

+0

是的,它是帮助。 – unknown