

AWS Tools for PowerShell V4 已進入維護模式。

我們建議您遷移至 [AWS Tools for PowerShell V5](https://docs.aws.amazon.com/powershell/v5/userguide/)。如需如何遷移的其他詳細資訊和資訊，請參閱我們的[維護模式公告](https://aws.amazon.com/blogs/developer/aws-tools-for-powershell-v4-maintenance-mode-announcement/)。

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

# Amazon S3 和 Tools for Windows PowerShell
<a name="pstools-s3"></a>

在本節中，我們使用 Amazon S3 和 CloudFront AWS Tools for Windows PowerShell 建立靜態網站。在過程中，我們展示這些服務的數個常見任務。本演練是根據[託管靜態網站](https://aws.amazon.com/getting-started/projects/host-static-website/)入門指南所建立，該指南描述使用 [AWS 管理主控台](https://console.aws.amazon.com/s3/home)的類似程序。

此處列出的命令假設您已經設定 PowerShell 工作階段的預設憑證與預設區域；所以，cmdlet 並不會呼叫憑證和區域。

**注意**  
目前沒有適用於重新命名儲存貯體或物件的 Amazon S3 API，因此，沒有可執行此任務的單一 Tools for Windows PowerShell cmdlet。若要重新命名 S3 中的物件，建議您執行 [Copy-S3Object](https://docs.aws.amazon.com/powershell/v4/reference/items/Copy-S3Object.html) cmdlet 使用新名稱複製物件，然後執行 [Remove-S3Object](https://docs.aws.amazon.com/powershell/v4/reference/items/Remove-S3Object.html) cmdlet 刪除原始物件。

**另請參閱**
+  [在 中使用 AWS 服務 AWS Tools for PowerShell](pstools-using.md) 
+  [託管於 Amazon S3 的靜態網站](https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html) 
+  [Amazon S3 主控台](https://console.aws.amazon.com/s3/home) 

**Topics**
+ [建立 Amazon S3 儲存貯體、驗證其區域，或者加以移除](pstools-s3-bucket-create.md)
+ [設定 Amazon S3 儲存貯體做為網站並啟用記錄](pstools-s3-create-website.md)
+ [將物件上傳至 Amazon S3 儲存貯體](pstools-s3-upload-object.md)
+ [刪除 Amazon S3 物件與儲存貯體](pstools-s3-delete-website.md)
+ [將內嵌文字內容上傳到 Amazon S3](pstools-s3-upload-in-line-text.md)

# 建立 Amazon S3 儲存貯體、驗證其區域，或者加以移除
<a name="pstools-s3-bucket-create"></a>

使用 `New-S3Bucket` cmdlet 來建立新的 Amazon S3 儲存貯體。以下範例會建立名為 `website-example` 的儲存貯體。儲存貯體的名稱必須在所有區域中為唯一。範例會在`us-west-1`區域中建立儲存貯體。

```
PS > New-S3Bucket -BucketName website-example -Region us-west-2

CreationDate         BucketName
------------         ----------
8/16/19 8:45:38 PM   website-example
```

您可以使用 `Get-S3BucketLocation` cmdlet 驗證儲存貯體所在的區域。

```
PS > Get-S3BucketLocation -BucketName website-example

Value
-----
us-west-2
```

當你完成本教學後，您就可以使用下面的程式碼刪除這個儲存貯體。但建議您保留這個儲存貯體，因為我們在後續範例會用到它。

```
PS > Remove-S3Bucket -BucketName website-example
```

請注意，儲存貯體移除程序需要較長時間來完成。如果您嘗試立即重新建立同名的儲存貯體，`New-S3Bucket` cmdlet 會失敗，直到舊的儲存貯體完全消失為止。

## 另請參閱
<a name="pstools-seealso-s3-bucket-create"></a>
+  [在 中使用 AWS 服務 AWS Tools for PowerShell](pstools-using.md) 
+  [Put Bucket (Amazon S3 服務參考) ](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html) 
+  [AWS Amazon S3 的 PowerShell 區域](https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region) 

# 設定 Amazon S3 儲存貯體做為網站並啟用記錄
<a name="pstools-s3-create-website"></a>

使用 `Write-S3BucketWebsite` cmdlet 來設定 Amazon S3 儲存貯體做為靜態網站。以下範例指定預設內容網頁的 `index.html` 名稱以及預設錯誤網頁的 `error.html` 名稱。請注意，這個 cmdlet 不會建立這些頁面。需將它們[上傳為 Amazon S3 物件](pstools-s3-upload-object.md)。

```
PS > Write-S3BucketWebsite -BucketName website-example -WebsiteConfiguration_IndexDocumentSuffix index.html -WebsiteConfiguration_ErrorDocument error.html
RequestId      : A1813E27995FFDDD
AmazonId2      : T7hlDOeLqA5Q2XfTe8j2q3SLoP3/5XwhUU3RyJBGHU/LnC+CIWLeGgP0MY24xAlI
ResponseStream :
Headers        : {x-amz-id-2, x-amz-request-id, Content-Length, Date...}
Metadata       : {}
ResponseXml    :
```

## 另請參閱
<a name="pstools-seealso-s3-create-website"></a>
+  [在 中使用 AWS 服務 AWS Tools for PowerShell](pstools-using.md) 
+  [Put Bucket 網站 (Amazon S3 API 參考)](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTwebsite.html) 
+  [Put Bucket ACL (Amazon S3 API 參考)](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTacl.html) 

# 將物件上傳至 Amazon S3 儲存貯體
<a name="pstools-s3-upload-object"></a>

使用 `Write-S3Object` cmdlet 可從本機檔案系統，將檔案以物件形式上傳至 Amazon S3 儲存貯體。以下範例會建立和上傳兩個簡單的 HTML 檔案至 Amazon S3 儲存貯體，並驗證上傳物件是否存在。`-File` 的 `Write-S3Object` 參數指定本機檔案系統中的檔案名稱。`-Key` 參數指定 Amazon S3 中對應物件的名稱。

Amazon 會從副檔名推斷物件的內容類型，在此例中是「.html」。

```
PS > # Create the two files using here-strings and the Set-Content cmdlet
PS > $index_html = @"
>> <html>
>>   <body>
>>     <p>
>>       Hello, World!
>>     </p>
>>   </body>
>> </html>
>> "@
>>
PS > $index_html | Set-Content index.html
PS > $error_html = @"
>> <html>
>>   <body>
>>     <p>
>>       This is an error page.
>>     </p>
>>   </body>
>> </html>
>> "@
>>
>>$error_html | Set-Content error.html
>># Upload the files to Amazon S3 using a foreach loop
>>foreach ($f in "index.html", "error.html") {
>> Write-S3Object -BucketName website-example -File $f -Key $f -CannedACLName public-read
>> }
>>
PS > # Verify that the files were uploaded
PS > Get-S3BucketWebsite -BucketName website-example

IndexDocumentSuffix                                         ErrorDocument
-------------------                                         -------------
index.html                                                  error.html
```

 *固定的 ACL 選項* 

使用 Tools for Windows PowerShell 指定固定的 ACL 之值，與 適用於 .NET 的 AWS SDK所用的值相同。不過，請注意，這些值都不同於 Amazon S3 `Put Object` 動作所用的值。Tools for Windows PowerShell 支援下列固定的 ACL：
+ NoACL
+ private
+ public-read
+ public-read-write
+ aws-exec-read
+ authenticated-read
+ bucket-owner-read
+ bucket-owner-full-control
+ log-delivery-write

如需有關這些固定 ACL 設定的詳細資訊，請參閱[存取控制清單概觀](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl)。

## 分段上傳的相關注意事項
<a name="note-regarding-multipart-upload"></a>

如果您使用 Amazon S3 API 來上傳大小大於 5 GB 的檔案，您需要使用分段上傳。不過，Tools for Windows PowerShell 提供的 `Write-S3Object` cmdlet 可以透明的方式處理大於 5 GB 的檔案上傳。

### 測試網站
<a name="pstools-amazon-s3-test-website"></a>

此時，您可以使用瀏覽器瀏覽至網站來測試網站。Amazon S3 中託管的靜態網站 URL 遵循標準格式。

```
http://<bucket-name>.s3-website-<region>.amazonaws.com
```

例如：

```
http://website-example.s3-website-us-west-1.amazonaws.com
```

### 另請參閱
<a name="pstools-seealso-amazon-s3-test-website"></a>
+  [在 中使用 AWS 服務 AWS Tools for PowerShell](pstools-using.md) 
+  [Put 物件 (Amazon S3 API 參考)](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html) 
+  [固定的 ACL (Amazon S3 API 參考)](https://docs.aws.amazon.com/AmazonS3/latest/dev/ACLOverview.html#CannedACL) 

# 刪除 Amazon S3 物件與儲存貯體
<a name="pstools-s3-delete-website"></a>

本節會說明如何將您在先前章節中所建立的網站予以刪除。您僅需刪除 HTML 檔案的物件，接著刪除該網站的 Amazon S3 儲存貯體即可。

首先，執行 `Remove-S3Object` cmdlet，從 Amazon S3 儲存貯體中刪除 HTML 檔案的物件。

```
PS > foreach ( $obj in "index.html", "error.html" ) {
>> Remove-S3Object -BucketName website-example -Key $obj
>> }
>> 
IsDeleteMarker
--------------
False
```

經由 Amazon S3 處理請求的方式，系統預期顯示的成品即為 `False` 回應。在此情況下，該回應並不表示發生問題。

您現在可以執行 `Remove-S3Bucket` cmdlet，刪除該網站目前空白的 Amazon S3 儲存貯體。

```
PS > Remove-S3Bucket -BucketName website-example

RequestId      : E480ED92A2EC703D
AmazonId2      : k6tqaqC1nMkoeYwbuJXUx1/UDa49BJd6dfLN0Ls1mWYNPHjbc8/Nyvm6AGbWcc2P
ResponseStream :
Headers        : {x-amz-id-2, x-amz-request-id, Date, Server}
Metadata       : {}
ResponseXml    :
```

在 1.1 版和更新版本中 AWS Tools for PowerShell，您可以將 `-DeleteBucketContent` 參數新增至 `Remove-S3Bucket`，先刪除指定儲存貯體中的所有物件和物件版本，再嘗試移除儲存貯體本身。根據儲存貯體中的物件或物件版本數量，系統可能需要花費大量的時間才能完成此操作。若使用 Tools for Windows PowerShell 1.1 版以前的版本，則儲存貯體必須為空，`Remove-S3Bucket` 才能夠刪除它。

**注意**  
除非您新增 `-Force` 參數，否則 會在 cmdlet 執行之前 AWS Tools for PowerShell 提示您進行確認。

## 另請參閱
<a name="pstools-seealso-amazon-s3-delete-website"></a>
+  [在 中使用 AWS 服務 AWS Tools for PowerShell](pstools-using.md) 
+  [刪除物件 (Amazon S3 API 參考)](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETE.html) 
+  [DeleteBucket (Amazon S3 API 參考)](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketDELETE.html) 

# 將內嵌文字內容上傳到 Amazon S3
<a name="pstools-s3-upload-in-line-text"></a>

`Write-S3Object` cmdlet 支援將內嵌文字內容上傳至 Amazon S3 的能力。使用 `-Content` 參數 (別名 `-Text`)，您可以指定應上傳到 Amazon S3 的文字型內容，而不需先將它轉換為檔案。參數接受簡單的一行字串，以及包含多行的 here 字串。

```
PS > # Specifying content in-line, single line text:
PS > write-s3object amzn-s3-demo-bucket -key myobject.txt -content "file content"

PS > # Specifying content in-line, multi-line text: (note final newline needed to end in-line here-string)
PS > write-s3object amzn-s3-demo-bucket -key myobject.txt -content @"
>> line 1
>> line 2
>> line 3
>> "@
>> 
PS > # Specifying content from a variable: (note final newline needed to end in-line here-string)
PS > $x = @"
>> line 1
>> line 2
>> line 3
>> "@
>> 
PS > write-s3object amzn-s3-demo-bucket -key myobject.txt -content $x
```