上傳範例 (AWS Signature 第 2 版) - Amazon Simple Storage Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

上傳範例 (AWS Signature 第 2 版)

注意

本節中所討論的請求身份驗證採用 AWS Signature 第 2 版,此通訊協定可對 AWS 服務的輸入 API 請求進行身份驗證。

在所有 AWS 區域 中,Amazon S3 現在支援 Signature 第 4 版,此通訊協定可對 AWS 服務的傳入 API 請求進行身份驗證。目前,2014 年 1 月 30 日以前所建立的 AWS 區域 會繼續支援舊版通訊協定 Signature 第 2 版。2014 年 1 月 30 日以後的任何新區域只會支援 Signature 第 4 版,因此傳送至這些區域的所有要求都必須使用 Signature 第 4 版提出。如需詳細資訊,請參閱《Amazon Simple Storage Service API 參考》中的範例:使用 HTTP POST 經由瀏覽器上傳 (使用 AWS Signature 第 4 版)

檔案上傳

此範例顯示建構政策與表單以用來上傳檔案附件的完整程序。

政策與表單建構

下列政策支援上傳至 Amazon S3 的 awsexamplebucket1 儲存貯體。

{ "expiration": "2007-12-01T12:00:00.000Z", "conditions": [ {"bucket": "awsexamplebucket1"}, ["starts-with", "$key", "user/eric/"], {"acl": "public-read"}, {"success_action_redirect": "https://awsexamplebucket1.s3.us-west-1.amazonaws.com/successful_upload.html"}, ["starts-with", "$Content-Type", "image/"], {"x-amz-meta-uuid": "14365123651274"}, ["starts-with", "$x-amz-meta-tag", ""] ] }

此政策需要進行下列操作:

  • 上傳必須發生在 2007 年 12 月 1 日 12:00 UTC 以前。

  • 內容必須上傳到 awsexamplebucket1 儲存貯體。

  • 金鑰必須以 "user/eric/" 開頭。

  • ACL 已設定為 public-read。

  • success_action_redirect 已設定為 https://awsexamplebucket1.s3.us-west-1.amazonaws.com/successful_upload.html。

  • 物件是映像檔。

  • x-amz-meta-uuid 標籤必須設定為 14365123651274。

  • x-amz-meta-tag 可包含任意值。

以下為此政策的 Base64 編碼版本。

eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci9lcmljLyJdLAogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2pvaG5zbWl0aC5zMy5hbWF6b25hd3MuY29tL3N1Y2Nlc3NmdWxfdXBsb2FkLmh0bWwifSwKICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwKICAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXQogIF0KfQo=

使用您的登入資料建立簽章;例如,0RavWzkygo6QX9caELEqKi9kDbU= 是上述政策文件的簽章。

下列表單支援對使用此政策的 DOC-EXAMPLE-BUCKET 儲存貯體提出 POST 要求。

<html> <head> ... <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> ... </head> <body> ... <form action="https://DOC-EXAMPLE-BUCKET.s3.us-west-1.amazonaws.com/" method="post" enctype="multipart/form-data"> Key to upload: <input type="input" name="key" value="user/eric/" /><br /> <input type="hidden" name="acl" value="public-read" /> <input type="hidden" name="success_action_redirect" value="https://awsexamplebucket1.s3.us-west-1.amazonaws.com/successful_upload.html" /> Content-Type: <input type="input" name="Content-Type" value="image/jpeg" /><br /> <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" /> Tags for File: <input type="input" name="x-amz-meta-tag" value="" /><br /> <input type="hidden" name="AWSAccessKeyId" value="AKIAIOSFODNN7EXAMPLE" /> <input type="hidden" name="Policy" value="POLICY" /> <input type="hidden" name="Signature" value="SIGNATURE" /> File: <input type="file" name="file" /> <br /> <!-- The elements after this will be ignored --> <input type="submit" name="submit" value="Upload to Amazon S3" /> </form> ... </html>

請求範例

此要求假設上傳的映像為 117,108 個位元組 (不包含映像資料)。

POST / HTTP/1.1 Host: awsexamplebucket1.s3.us-west-1.amazonaws.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Content-Type: multipart/form-data; boundary=9431149156168 Content-Length: 118698 --9431149156168 Content-Disposition: form-data; name="key" user/eric/MyPicture.jpg --9431149156168 Content-Disposition: form-data; name="acl" public-read --9431149156168 Content-Disposition: form-data; name="success_action_redirect" https://awsexamplebucket1.s3.us-west-1.amazonaws.com/successful_upload.html --9431149156168 Content-Disposition: form-data; name="Content-Type" image/jpeg --9431149156168 Content-Disposition: form-data; name="x-amz-meta-uuid" 14365123651274 --9431149156168 Content-Disposition: form-data; name="x-amz-meta-tag" Some,Tag,For,Picture --9431149156168 Content-Disposition: form-data; name="AWSAccessKeyId" AKIAIOSFODNN7EXAMPLE --9431149156168 Content-Disposition: form-data; name="Policy" eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci9lcmljLyJdLAogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2pvaG5zbWl0aC5zMy5hbWF6b25hd3MuY29tL3N1Y2Nlc3NmdWxfdXBsb2FkLmh0bWwifSwKICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwKICAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXQogIF0KfQo= --9431149156168 Content-Disposition: form-data; name="Signature" 0RavWzkygo6QX9caELEqKi9kDbU= --9431149156168 Content-Disposition: form-data; name="file"; filename="MyFilename.jpg" Content-Type: image/jpeg ...file content... --9431149156168 Content-Disposition: form-data; name="submit" Upload to Amazon S3 --9431149156168--

回應範例

HTTP/1.1 303 Redirect x-amz-request-id: 1AEE782442F35865 x-amz-id-2: cxzFLJRatFHy+NGtaDFRR8YvI9BHmgLxjvJzNiGGICARZ/mVXHj7T+qQKhdpzHFh Content-Type: application/xml Date: Wed, 14 Nov 2007 21:21:33 GMT Connection: close Location: https://awsexamplebucket1.s3.us-west-1.amazonaws.com/successful_upload.html?bucket=awsexamplebucket1&key=user/eric/MyPicture.jpg&etag=&quot;39d459dfbc0faabbb5e179358dfb94c3&quot; Server: AmazonS3

文字區域上傳

下列範例顯示建構政策與表單以上傳文字區域的完整程序。上傳文字區域有助於提交使用者建立的內容,例如部落格張貼內容。

政策與表單建構

下列政策支援將文字區域上傳至 Amazon S3 的 awsexamplebucket1 儲存貯體。

{ "expiration": "2007-12-01T12:00:00.000Z", "conditions": [ {"bucket": "awsexamplebucket1"}, ["starts-with", "$key", "user/eric/"], {"acl": "public-read"}, {"success_action_redirect": "https://awsexamplebucket1.s3.us-west-1.amazonaws.com/new_post.html"}, ["eq", "$Content-Type", "text/html"], {"x-amz-meta-uuid": "14365123651274"}, ["starts-with", "$x-amz-meta-tag", ""] ] }

此政策需要進行下列操作:

  • 上傳必須發生在 2007-12-01 12:00 GMT 以前。

  • 內容必須上傳到 awsexamplebucket1 儲存貯體。

  • 金鑰必須以 "user/eric/" 開頭。

  • ACL 已設定為 public-read。

  • success_action_redirect 已設定為 https://awsexamplebucket1.s3.us-west-1.amazonaws.com/new_post.html。

  • 物件是 HTML 文字。

  • x-amz-meta-uuid 標籤必須設定為 14365123651274。

  • x-amz-meta-tag 可包含任意值。

以下為此政策的 Base64 編碼版本。

eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXR pb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci9lcmljLyJd LAogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2pvaG5zbWl0a C5zMy5hbWF6b25hd3MuY29tL25ld19wb3N0Lmh0bWwifSwKICAgIFsiZXEiLCAiJENvbnRlbnQtVHlwZSIsICJ0ZXh0L2h0bWwiXSwKI CAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZy IsICIiXQogIF0KfQo=

使用您的登入資料建立簽章。例如,qA7FWXKq6VvU68lI9KdveT1cWgF= 是上述政策文件的簽章。

下列表單支援對使用此政策的 DOC-EXAMPLE-BUCKET 儲存貯體提出 POST 要求。

<html> <head> ... <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> ... </head> <body> ... <form action="https://DOC-EXAMPLE-BUCKET.s3.us-west-1.amazonaws.com/" method="post" enctype="multipart/form-data"> Key to upload: <input type="input" name="key" value="user/eric/" /><br /> <input type="hidden" name="acl" value="public-read" /> <input type="hidden" name="success_action_redirect" value="https://awsexamplebucket1.s3.us-west-1.amazonaws.com/new_post.html" /> <input type="hidden" name="Content-Type" value="text/html" /> <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" /> Tags for File: <input type="input" name="x-amz-meta-tag" value="" /><br /> <input type="hidden" name="AWSAccessKeyId" value="AKIAIOSFODNN7EXAMPLE" /> <input type="hidden" name="Policy" value="POLICY" /> <input type="hidden" name="Signature" value="SIGNATURE" /> Entry: <textarea name="file" cols="60" rows="10"> Your blog post goes here. </textarea><br /> <!-- The elements after this will be ignored --> <input type="submit" name="submit" value="Upload to Amazon S3" /> </form> ... </html>

請求範例

此要求假設上傳的映像為 117,108 個位元組 (不包含映像資料)。

POST / HTTP/1.1 Host: awsexamplebucket1.s3.us-west-1.amazonaws.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Content-Type: multipart/form-data; boundary=178521717625888 Content-Length: 118635 -178521717625888 Content-Disposition: form-data; name="key" ser/eric/NewEntry.html --178521717625888 Content-Disposition: form-data; name="acl" public-read --178521717625888 Content-Disposition: form-data; name="success_action_redirect" https://awsexamplebucket1.s3.us-west-1.amazonaws.com/new_post.html --178521717625888 Content-Disposition: form-data; name="Content-Type" text/html --178521717625888 Content-Disposition: form-data; name="x-amz-meta-uuid" 14365123651274 --178521717625888 Content-Disposition: form-data; name="x-amz-meta-tag" Interesting Post --178521717625888 Content-Disposition: form-data; name="AWSAccessKeyId" AKIAIOSFODNN7EXAMPLE --178521717625888 Content-Disposition: form-data; name="Policy" eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci9lcmljLyJdLAogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2pvaG5zbWl0aC5zMy5hbWF6b25hd3MuY29tL25ld19wb3N0Lmh0bWwifSwKICAgIFsiZXEiLCAiJENvbnRlbnQtVHlwZSIsICJ0ZXh0L2h0bWwiXSwKICAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXQogIF0KfQo= --178521717625888 Content-Disposition: form-data; name="Signature" qA7FWXKq6VvU68lI9KdveT1cWgF= --178521717625888 Content-Disposition: form-data; name="file" ...content goes here... --178521717625888 Content-Disposition: form-data; name="submit" Upload to Amazon S3 --178521717625888--

回應範例

HTTP/1.1 303 Redirect x-amz-request-id: 1AEE782442F35865 x-amz-id-2: cxzFLJRatFHy+NGtaDFRR8YvI9BHmgLxjvJzNiGGICARZ/mVXHj7T+qQKhdpzHFh Content-Type: application/xml Date: Wed, 14 Nov 2007 21:21:33 GMT Connection: close Location: https://awsexamplebucket1.s3.us-west-1.amazonaws.com/new_post.html?bucket=awsexamplebucket1&key=user/eric/NewEntry.html&etag=40c3271af26b7f1672e41b8a274d28d4 Server: AmazonS3