2017-06-19 87 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