

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

# 上傳範例 (AWS 簽章第 2 版）****
<a name="HTTPPOSTExamples"></a>

**Topics**
+ [檔案上傳](#HTTPPOSTExamplesFileUpload)
+ [文字區域上傳](#HTTPPOSTExamplesTextArea)

**注意**  
本節討論的請求身分驗證是以 AWS Signature 第 2 版為基礎，這是一種通訊協定，用於驗證對 AWS 服務的傳入 API 請求。  
Amazon S3 現在支援 Signature 第 4 版，這是一種通訊協定，用於驗證對 AWS 服務的傳入 API 請求 AWS 區域。目前，在 2014 年 1 月 30 日之前 AWS 區域 建立的 將繼續支援先前的通訊協定 Signature 第 2 版。2014 年 1 月 30 日以後的任何新區域只會支援 Signature 第 4 版，因此傳送至這些區域的所有要求都必須使用 Signature 第 4 版提出。如需詳細資訊，請參閱《*Amazon Simple Storage Service API 參考*》中的[範例：使用 HTTP POST （使用 AWS Signature 第 4 版） 進行瀏覽器型上傳](https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html)。

## 檔案上傳
<a name="HTTPPOSTExamplesFileUpload"></a>

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

### 政策與表單建構
<a name="HTTPPOSTExamplesFileUploadPolicy"></a>

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

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

此政策需要進行下列操作：
+ 上傳必須發生在 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 編碼版本。

```
1. eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci9lcmljLyJdLAogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2pvaG5zbWl0aC5zMy5hbWF6b25hd3MuY29tL3N1Y2Nlc3NmdWxfdXBsb2FkLmh0bWwifSwKICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwKICAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXQogIF0KfQo=
```

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

下列表單支援對使用此政策的儲存 amzn-s3-demo-bucket 貯體提出 POST 請求。

```
 1. <html>
 2.   <head>
 3.     ...
 4.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 5.     ...
 6.   </head>
 7.   <body>
 8.   ...
 9.   <form action="https://amzn-s3-demo-bucket.s3.us-west-1.amazonaws.com/" method="post" enctype="multipart/form-data">
10.     Key to upload: <input type="input" name="key" value="user/eric/" /><br />
11.     <input type="hidden" name="acl" value="public-read" />
12.     <input type="hidden" name="success_action_redirect" value="https://awsexamplebucket1.s3.us-west-1.amazonaws.com/successful_upload.html" />
13.     Content-Type: <input type="input" name="Content-Type" value="image/jpeg" /><br />
14.     <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
15.     Tags for File: <input type="input" name="x-amz-meta-tag" value="" /><br />
16.     <input type="hidden" name="AWSAccessKeyId" value="AKIAIOSFODNN7EXAMPLE" />
17.     <input type="hidden" name="Policy" value="POLICY" />
18.     <input type="hidden" name="Signature" value="SIGNATURE" />
19.     File: <input type="file" name="file" /> <br />
20.     <!-- The elements after this will be ignored -->
21.     <input type="submit" name="submit" value="Upload to Amazon S3" />
22.   </form>
23.   ...
24. </html>
```

### 請求範例
<a name="HTTPPOSTExamplesFileUploadRequest"></a>

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

```
 1. POST / HTTP/1.1
 2. Host: awsexamplebucket1.s3.us-west-1.amazonaws.com
 3. 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
 4. Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
 5. Accept-Language: en-us,en;q=0.5
 6. Accept-Encoding: gzip,deflate
 7. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
 8. Keep-Alive: 300
 9. Connection: keep-alive
10. Content-Type: multipart/form-data; boundary=9431149156168
11. Content-Length: 118698 
12. 
13. --9431149156168
14. Content-Disposition: form-data; name="key"
15. 
16. user/eric/MyPicture.jpg
17. --9431149156168
18. Content-Disposition: form-data; name="acl"
19. 
20. public-read
21. --9431149156168
22. Content-Disposition: form-data; name="success_action_redirect"
23. 
24. https://awsexamplebucket1.s3.us-west-1.amazonaws.com/successful_upload.html
25. --9431149156168
26. Content-Disposition: form-data; name="Content-Type"
27. 
28. image/jpeg
29. --9431149156168
30. Content-Disposition: form-data; name="x-amz-meta-uuid"
31. 
32. 14365123651274
33. --9431149156168
34. Content-Disposition: form-data; name="x-amz-meta-tag"
35. 
36. Some,Tag,For,Picture
37. --9431149156168
38. Content-Disposition: form-data; name="AWSAccessKeyId"
39. 
40. AKIAIOSFODNN7EXAMPLE
41. --9431149156168
42. Content-Disposition: form-data; name="Policy"
43. 
44. eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci9lcmljLyJdLAogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2pvaG5zbWl0aC5zMy5hbWF6b25hd3MuY29tL3N1Y2Nlc3NmdWxfdXBsb2FkLmh0bWwifSwKICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwKICAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXQogIF0KfQo=
45. --9431149156168
46. Content-Disposition: form-data; name="Signature"
47. 
48. 0RavWzkygo6QX9caELEqKi9kDbU=
49. --9431149156168
50. Content-Disposition: form-data; name="file"; filename="MyFilename.jpg"
51. Content-Type: image/jpeg
52. 
53. ...file content...
54. --9431149156168
55. Content-Disposition: form-data; name="submit"
56. 
57. Upload to Amazon S3
58. --9431149156168--
```

### 回應範例
<a name="HTTPPOSTExamplesFileUploadResponse"></a>

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

## 文字區域上傳
<a name="HTTPPOSTExamplesTextArea"></a>

**Topics**
+ [政策與表單建構](#HTTPPOSTExamplesTextAreaPolicy)
+ [請求範例](#HTTPPOSTExamplesTextAreaRequest)
+ [回應範例](#HTTPPOSTExamplesTextAreaResponse)

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

### 政策與表單建構
<a name="HTTPPOSTExamplesTextAreaPolicy"></a>

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

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

此政策需要進行下列操作：
+ 上傳必須發生在 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 編碼版本。

```
1. eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXR
2. pb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci9lcmljLyJd
3. LAogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2pvaG5zbWl0a
4. C5zMy5hbWF6b25hd3MuY29tL25ld19wb3N0Lmh0bWwifSwKICAgIFsiZXEiLCAiJENvbnRlbnQtVHlwZSIsICJ0ZXh0L2h0bWwiXSwKI
5. CAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZy
6. IsICIiXQogIF0KfQo=
```

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

下列表單支援對使用此政策的儲存 amzn-s3-demo-bucket 貯體提出 POST 請求。

```
 1. <html>
 2.   <head>
 3.     ...
 4.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 5.     ...
 6.   </head>
 7.   <body>
 8.   ...
 9.   <form action="https://amzn-s3-demo-bucket.s3.us-west-1.amazonaws.com/" method="post" enctype="multipart/form-data">
10.     Key to upload: <input type="input" name="key" value="user/eric/" /><br />
11.     <input type="hidden" name="acl" value="public-read" />
12.     <input type="hidden" name="success_action_redirect" value="https://awsexamplebucket1.s3.us-west-1.amazonaws.com/new_post.html" />
13.     <input type="hidden" name="Content-Type" value="text/html" />
14.     <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
15.     Tags for File: <input type="input" name="x-amz-meta-tag" value="" /><br />
16.     <input type="hidden" name="AWSAccessKeyId" value="AKIAIOSFODNN7EXAMPLE" />
17.     <input type="hidden" name="Policy" value="POLICY" />
18.     <input type="hidden" name="Signature" value="SIGNATURE" />
19.     Entry: <textarea name="file" cols="60" rows="10">
20. 
21. Your blog post goes here.
22. 
23.     </textarea><br />
24.     <!-- The elements after this will be ignored -->
25.     <input type="submit" name="submit" value="Upload to Amazon S3" />
26.   </form>
27.   ...
28. </html>
```

### 請求範例
<a name="HTTPPOSTExamplesTextAreaRequest"></a>

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

```
 1. POST / HTTP/1.1
 2. Host: awsexamplebucket1.s3.us-west-1.amazonaws.com
 3. 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
 4. Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
 5. Accept-Language: en-us,en;q=0.5
 6. Accept-Encoding: gzip,deflate
 7. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
 8. Keep-Alive: 300
 9. Connection: keep-alive
10. Content-Type: multipart/form-data; boundary=178521717625888
11. Content-Length: 118635
12. 
13. -178521717625888
14. Content-Disposition: form-data; name="key"
15. 
16. ser/eric/NewEntry.html
17. --178521717625888
18. Content-Disposition: form-data; name="acl"
19. 
20. public-read
21. --178521717625888
22. Content-Disposition: form-data; name="success_action_redirect"
23. 
24. https://awsexamplebucket1.s3.us-west-1.amazonaws.com/new_post.html
25. --178521717625888
26. Content-Disposition: form-data; name="Content-Type"
27. 
28. text/html
29. --178521717625888
30. Content-Disposition: form-data; name="x-amz-meta-uuid"
31. 
32. 14365123651274
33. --178521717625888
34. Content-Disposition: form-data; name="x-amz-meta-tag"
35. 
36. Interesting Post
37. --178521717625888
38. Content-Disposition: form-data; name="AWSAccessKeyId"
39. 
40. AKIAIOSFODNN7EXAMPLE
41. --178521717625888
42. Content-Disposition: form-data; name="Policy"
43. eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci9lcmljLyJdLAogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2pvaG5zbWl0aC5zMy5hbWF6b25hd3MuY29tL25ld19wb3N0Lmh0bWwifSwKICAgIFsiZXEiLCAiJENvbnRlbnQtVHlwZSIsICJ0ZXh0L2h0bWwiXSwKICAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXQogIF0KfQo=
44. 
45. --178521717625888
46. Content-Disposition: form-data; name="Signature"
47. 
48. qA7FWXKq6VvU68lI9KdveT1cWgF=
49. --178521717625888
50. Content-Disposition: form-data; name="file"
51. 
52. ...content goes here...
53. --178521717625888
54. Content-Disposition: form-data; name="submit"
55. 
56. Upload to Amazon S3
57. --178521717625888--
```

### 回應範例
<a name="HTTPPOSTExamplesTextAreaResponse"></a>

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