

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用 Tools for PowerShell V4 的 Amazon S3 範例
<a name="powershell_4_s3_code_examples"></a>

下列程式碼範例示範如何使用 AWS Tools for PowerShell V4 搭配 Amazon S3 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `Copy-S3Object`
<a name="s3_CopyObject_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Copy-S3Object`。

**Tools for PowerShell V4**  
**範例 1：此命令會將物件"sample.txt" 從儲存貯體 "test-files" 複製到相同的儲存貯體，但具有新索引鍵 "sample-copy.txt"。**  

```
Copy-S3Object -BucketName amzn-s3-demo-bucket -Key sample.txt -DestinationKey sample-copy.txt
```
**範例 2：此命令會將物件"sample.txt" 從儲存貯體 "test-files" 複製到儲存貯體 "backup-files"，索引鍵為 "sample-copy.txt"。**  

```
Copy-S3Object -BucketName amzn-s3-demo-source-bucket -Key sample.txt -DestinationKey sample-copy.txt -DestinationBucket amzn-s3-demo-destination-bucket
```
**範例 3：此命令會將物件"sample.txt" 從儲存貯體 "test-files" 下載至名為 "local-sample.txt" 的本機檔案。**  

```
Copy-S3Object -BucketName amzn-s3-demo-bucket -Key sample.txt -LocalFile local-sample.txt
```
**範例 4：將單一物件下載至指定的檔案。下載的檔案位於 c:\$1downloads\$1data\$1archive.zip**  

```
Copy-S3Object -BucketName amzn-s3-demo-bucket -Key data/archive.zip -LocalFolder c:\downloads
```
**範例 5：將符合指定索引鍵字首的所有物件下載至本機資料夾。相對索引鍵階層會保留為整體下載位置中的子資料夾。**  

```
Copy-S3Object -BucketName amzn-s3-demo-bucket -KeyPrefix data -LocalFolder c:\downloads
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [CopyObject](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3ACL`
<a name="s3_GetACL_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3ACL`。

**Tools for PowerShell V4**  
**範例 1： 命令會取得 S3 物件之物件擁有者的詳細資訊。**  

```
Get-S3ACL -BucketName 'amzn-s3-demo-bucket' -key 'initialize.ps1' -Select AccessControlList.Owner
```
**輸出：**  

```
DisplayName Id
----------- --
testusername      9988776a6554433d22f1100112e334acb45566778899009e9887bd7f66c5f544
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetACL](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3Bucket`
<a name="s3_ListBuckets_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3Bucket`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回所有 S3 儲存貯體。**  

```
Get-S3Bucket
```
**範例 2：此命令會傳回名為 "test-files" 的儲存貯體**  

```
Get-S3Bucket -BucketName amzn-s3-demo-bucket
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListBuckets](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketAccelerateConfiguration`
<a name="s3_GetBucketAccelerateConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketAccelerateConfiguration`。

**Tools for PowerShell V4**  
**範例 1：如果為指定的儲存貯體啟用傳輸加速設定，此命令會傳回已啟用的值。**  

```
Get-S3BucketAccelerateConfiguration -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
Value                                  
-----                                    
Enabled
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketAccelerateConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketAnalyticsConfiguration`
<a name="s3_GetBucketAnalyticsConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketAnalyticsConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回指定 S3 儲存貯體中名為 'testfilter' 的分析篩選條件詳細資訊。**  

```
Get-S3BucketAnalyticsConfiguration -BucketName 'amzn-s3-demo-bucket' -AnalyticsId 'testfilter'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketAnalyticsConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketAnalyticsConfigurationList`
<a name="s3_ListBucketAnalyticsConfigurations_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketAnalyticsConfigurationList`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回給定 S3 儲存貯體的前 100 個分析組態。**  

```
Get-S3BucketAnalyticsConfigurationList -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListBucketAnalyticsConfigurations](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketEncryption`
<a name="s3_GetBucketEncryption_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketEncryption`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回與指定儲存貯體相關聯的所有伺服器端加密規則。**  

```
Get-S3BucketEncryption -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketEncryption](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketInventoryConfiguration`
<a name="s3_GetBucketInventoryConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketInventoryConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回給定 S3 儲存貯體之名為 'testinventory' 的庫存詳細資訊。**  

```
Get-S3BucketInventoryConfiguration -BucketName 'amzn-s3-demo-bucket' -InventoryId 'testinventory'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketInventoryConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketInventoryConfigurationList`
<a name="s3_ListBucketInventoryConfigurations_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketInventoryConfigurationList`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回給定 S3 儲存貯體的前 100 個庫存組態。**  

```
Get-S3BucketInventoryConfigurationList -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListBucketInventoryConfigurations](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketLocation`
<a name="s3_GetBucketLocation_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketLocation`。

**Tools for PowerShell V4**  
**範例 1：如果存在限制，此命令會傳回儲存貯體 'amzn-s3-demo-bucket' 的位置限制。**  

```
Get-S3BucketLocation -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
Value
-----
ap-south-1
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketLocation](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketLogging`
<a name="s3_GetBucketLogging_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketLogging`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回指定儲存貯體的記錄狀態。**  

```
Get-S3BucketLogging -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
TargetBucketName   Grants TargetPrefix
----------------   ------ ------------
testbucket1        {}     testprefix
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketLogging](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketMetricsConfiguration`
<a name="s3_GetBucketMetricsConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketMetricsConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回給定 S3 儲存貯體之名為 'testfilter' 的指標篩選條件詳細資訊。**  

```
Get-S3BucketMetricsConfiguration -BucketName 'amzn-s3-demo-bucket' -MetricsId 'testfilter'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketMetricsConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketNotification`
<a name="s3_GetBucketNotification_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketNotification`。

**Tools for PowerShell V4**  
**範例 1：此範例會擷取給定儲存貯體的通知組態**  

```
Get-S3BucketNotification -BucketName amzn-s3-demo-bucket | select -ExpandProperty TopicConfigurations
```
**輸出：**  

```
Id   Topic
--   -----
mimo arn:aws:sns:eu-west-1:123456789012:topic-1
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketNotification](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketPolicy`
<a name="s3_GetBucketPolicy_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketPolicy`。

**Tools for PowerShell V4**  
**範例 1：此命令會輸出與給定 S3 儲存貯體相關聯的儲存貯體政策。**  

```
Get-S3BucketPolicy -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketPolicy](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketPolicyStatus`
<a name="s3_GetBucketPolicyStatus_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketPolicyStatus`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回給定 S3 儲存貯體的政策狀態，其指出儲存貯體是否為公有。**  

```
Get-S3BucketPolicyStatus -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketPolicyStatus](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketReplication`
<a name="s3_GetBucketReplication_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketReplication`。

**Tools for PowerShell V4**  
**範例 1：傳回在名為 'amzn-s3-demo-bucket' 的儲存貯體上設定的複寫組態資訊。**  

```
Get-S3BucketReplication -BucketName amzn-s3-demo-bucket
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketReplication](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketRequestPayment`
<a name="s3_GetBucketRequestPayment_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketRequestPayment`。

**Tools for PowerShell V4**  
**範例 1：傳回名為 'amzn-s3-demo-bucket' 之儲存貯體的請求付款組態。根據預設，儲存貯體擁有者會支付從儲存貯體下載的費用。**  

```
Get-S3BucketRequestPayment -BucketName amzn-s3-demo-bucket
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**的 [GetBucketRequestPayment](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketTagging`
<a name="s3_GetBucketTagging_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketTagging`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回與給定儲存貯體相關聯的所有標籤。**  

```
Get-S3BucketTagging -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketTagging](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketVersioning`
<a name="s3_GetBucketVersioning_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketVersioning`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回與給定儲存貯體相關的版本控制狀態。**  

```
Get-S3BucketVersioning -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketVersioning](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3BucketWebsite`
<a name="s3_GetBucketWebsite_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3BucketWebsite`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回給定 S3 儲存貯體之靜態網站組態的詳細資訊。**  

```
Get-S3BucketWebsite -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetBucketWebsite](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3CORSConfiguration`
<a name="s3_GetCORSConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3CORSConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回物件，其包含對應至給定 S3 儲存貯體的所有 CORS 組態規則。**  

```
Get-S3CORSConfiguration -BucketName 'amzn-s3-demo-bucket' -Select Configuration.Rules
```
**輸出：**  

```
AllowedMethods : {PUT, POST, DELETE}
AllowedOrigins : {http://www.example1.com}
Id             : 
ExposeHeaders  : {}
MaxAgeSeconds  : 0
AllowedHeaders : {*}

AllowedMethods : {PUT, POST, DELETE}
AllowedOrigins : {http://www.example2.com}
Id             : 
ExposeHeaders  : {}
MaxAgeSeconds  : 0
AllowedHeaders : {*}

AllowedMethods : {GET}
AllowedOrigins : {*}
Id             : 
ExposeHeaders  : {}
MaxAgeSeconds  : 0
AllowedHeaders : {}
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetCORSConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3LifecycleConfiguration`
<a name="s3_GetLifecycleConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3LifecycleConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此範例會擷取儲存貯體的生命週期組態。**  

```
Get-S3LifecycleConfiguration -BucketName amzn-s3-demo-bucket
```
**輸出：**  

```
Rules
-----
{Remove-in-150-days, Archive-to-Glacier-in-30-days}
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetLifecycleConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3Object`
<a name="s3_ListObjects_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3Object`。

**Tools for PowerShell V4**  
**範例 1：此命令會擷取儲存貯體 "test-files" 中所有項目的相關資訊。**  

```
Get-S3Object -BucketName amzn-s3-demo-bucket
```
**範例 2：此命令會從儲存貯體 "test-files" 擷取項目 "sample.txt" 的相關資訊。**  

```
Get-S3Object -BucketName amzn-s3-demo-bucket -Key sample.txt
```
**範例 3：此命令會從儲存貯體 "test-files" 擷取字首為 "sample" 之所有項目的相關資訊。**  

```
Get-S3Object -BucketName amzn-s3-demo-bucket -KeyPrefix sample
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListObjects](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3ObjectLockConfiguration`
<a name="s3_GetObjectLockConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3ObjectLockConfiguration`。

**Tools for PowerShell V4**  
**範例 1：如果為給定的 S3 儲存貯體啟用物件鎖定組態，此命令會傳回值 'Enabled'。**  

```
Get-S3ObjectLockConfiguration -BucketName 'amzn-s3-demo-bucket' -Select ObjectLockConfiguration.ObjectLockEnabled
```
**輸出：**  

```
Value
-----
Enabled
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetObjectLockConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3ObjectMetadata`
<a name="s3_GetObjectMetadata_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3ObjectMetadata`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回給定 S3 儲存貯體中金索引鍵為 'ListTrusts.txt' 的物件中繼資料。**  

```
Get-S3ObjectMetadata -BucketName 'amzn-s3-demo-bucket' -Key 'ListTrusts.txt'
```
**輸出：**  

```
Headers                                       : Amazon.S3.Model.HeadersCollection
Metadata                                      : Amazon.S3.Model.MetadataCollection
DeleteMarker                                  : 
AcceptRanges                                  : bytes
ContentRange                                  : 
Expiration                                    : 
RestoreExpiration                             : 
RestoreInProgress                             : False
LastModified                                  : 01/01/2020 08:02:05
ETag                                          : "d000011112a222e333e3bb4ee5d43d21"
MissingMeta                                   : 0
VersionId                                     : null
Expires                                       : 01/01/0001 00:00:00
WebsiteRedirectLocation                       : 
ServerSideEncryptionMethod                    : AES256
ServerSideEncryptionCustomerMethod            : 
ServerSideEncryptionKeyManagementServiceKeyId : 
ReplicationStatus                             : 
PartsCount                                    : 
ObjectLockLegalHoldStatus                     : 
ObjectLockMode                                : 
ObjectLockRetainUntilDate                     : 01/01/0001 00:00:00
StorageClass                                  : 
RequestCharged                                :
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetObjectMetadata](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3ObjectRetention`
<a name="s3_GetObjectRetention_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3ObjectRetention`。

**Tools for PowerShell V4**  
**範例 1：命令會傳回模式和日期，直到保留物件為止。**  

```
Get-S3ObjectRetention -BucketName 'amzn-s3-demo-bucket' -Key 'testfile.txt'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetObjectRetention](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3ObjectTagSet`
<a name="s3_GetObjectTagging_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3ObjectTagSet`。

**Tools for PowerShell V4**  
**範例 1：範例會傳回與給定 S3 儲存貯體上存在之物件相關聯的標籤。**  

```
Get-S3ObjectTagSet -Key 'testfile.txt' -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
Key  Value
---  -----
test value
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetObjectTagging](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3PreSignedURL`
<a name="s3_GetPreSignedURL_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3PreSignedURL`。

**Tools for PowerShell V4**  
**範例 1： 命令會傳回指定金鑰和到期日期的預先簽章 URL。**  

```
Get-S3PreSignedURL -BucketName 'amzn-s3-demo-bucket' -Key 'testkey' -Expires '2023-11-16'
```
**範例 2： 命令會傳回具有指定金鑰和到期日期的目錄儲存貯體的預先簽章 URL。**  

```
[Amazon.AWSConfigsS3]::UseSignatureVersion4 = $true
      Get-S3PreSignedURL -BucketName amzn-s3-demo-bucket--usw2-az1--x-s3 -Key 'testkey' -Expire '2023-11-17'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetPreSignedURL](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3PublicAccessBlock`
<a name="s3_GetPublicAccessBlock_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3PublicAccessBlock`。

**Tools for PowerShell V4**  
**範例 1： 命令會傳回給定 S3 儲存貯體的公有存取區塊組態。**  

```
Get-S3PublicAccessBlock -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetPublicAccessBlock](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Get-S3Version`
<a name="s3_ListVersions_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Get-S3Version`。

**Tools for PowerShell V4**  
**範例 1：此命令會傳回給定 S3 儲存貯體中所有物件版本的中繼資料。**  

```
Get-S3Version -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
IsTruncated         : False
KeyMarker           : 
VersionIdMarker     : 
NextKeyMarker       : 
NextVersionIdMarker : 
Versions            : {EC2.txt, EC2MicrosoftWindowsGuide.txt, ListDirectories.json, ListTrusts.json}
Name                : amzn-s3-demo-bucket
Prefix              : 
MaxKeys             : 1000
CommonPrefixes      : {}
Delimiter           :
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListVersions](https://docs.aws.amazon.com/powershell/v4/reference)。

### `New-S3Bucket`
<a name="s3_PutBucket_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `New-S3Bucket`。

**Tools for PowerShell V4**  
**範例 1：此命令會建立名為 "sample-bucket" 的新私有儲存貯體。**  

```
New-S3Bucket -BucketName amzn-s3-demo-bucket
```
**範例 2：此命令會建立名為 "sample-bucket" 且具有讀寫權限的新儲存貯體。**  

```
New-S3Bucket -BucketName amzn-s3-demo-bucket -PublicReadWrite
```
**範例 3：此命令會建立名為 "sample-bucket" 且具有唯讀權限的新儲存貯體。**  

```
New-S3Bucket -BucketName amzn-s3-demo-bucket -PublicReadOnly
```
**範例 4：此命令會使用 PutBucketConfiguration 建立新的目錄儲存貯體，名為 "amzn-s3-demo-bucket--use1-az5--x-s3"。**  

```
$bucketConfiguration = @{
      BucketInfo = @{
          DataRedundancy = 'SingleAvailabilityZone'
          Type = 'Directory'
      }
      Location = @{
          Name = 'usw2-az1'
          Type = 'AvailabilityZone'
      }
    }
New-S3Bucket -BucketName amzn-s3-demo-bucket--usw2-az1--x-s3 -BucketConfiguration $bucketConfiguration -Region us-west-2
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucket](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Read-S3Object`
<a name="s3_GetObject_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Read-S3Object`。

**Tools for PowerShell V4**  
**範例 1：此命令會從儲存貯體 "amzn-s3-demo-bucket" 擷取項目 "sample.txt"，並將其儲存至目前位置中名為 "local-sample.txt" 的檔案。在呼叫此命令之前，檔案 "local-sample.txt" 不需存在。**  

```
Read-S3Object -BucketName amzn-s3-demo-bucket -Key sample.txt -File local-sample.txt
```
**範例 2：此命令會從儲存貯體 "amzn-s3-demo-bucket" 擷取虛擬目錄 "DIR"，並將其儲存至目前位置中名為 "Local-DIR" 的資料夾。呼叫此命令之前，資料夾 "Local-DIR" 不需存在。**  

```
Read-S3Object -BucketName amzn-s3-demo-bucket -KeyPrefix DIR -Folder Local-DIR
```
**範例 3：將所有金鑰以 '.json' 結尾的物件，從儲存貯體名稱具有 'config' 的儲存貯體，下載到指定資料夾中的檔案。物件金鑰用於設定檔案名稱。**  

```
Get-S3Bucket | ? { $_.BucketName -like '*config*' } | Get-S3Object | ? { $_.Key -like '*.json' } | Read-S3Object -Folder C:\ConfigObjects
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetObject](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3Bucket`
<a name="s3_DeleteBucket_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3Bucket`。

**Tools for PowerShell V4**  
**範例 1：此命令會從儲存貯體 'test-files' 移除所有物件和物件版本，然後刪除儲存貯體。繼續之前，此命令會先提示確認。新增 - 強制切換以隱藏確認。請注意，不是空的儲存貯體無法刪除。**  

```
Remove-S3Bucket -BucketName amzn-s3-demo-bucket -DeleteBucketContent
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucket](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3BucketAnalyticsConfiguration`
<a name="s3_DeleteBucketAnalyticsConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3BucketAnalyticsConfiguration`。

**Tools for PowerShell V4**  
**範例 1： 命令會移除給定 S3 儲存貯體中名為 'testfilter' 的分析篩選條件。**  

```
Remove-S3BucketAnalyticsConfiguration -BucketName 'amzn-s3-demo-bucket' -AnalyticsId 'testfilter'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucketAnalyticsConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3BucketEncryption`
<a name="s3_DeleteBucketEncryption_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3BucketEncryption`。

**Tools for PowerShell V4**  
**範例 1：這會停用針對提供的 S3 儲存貯體啟用的加密。**  

```
Remove-S3BucketEncryption -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-S3BucketEncryption (DeleteBucketEncryption)" on target "s3casetestbucket".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucketEncryption](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3BucketInventoryConfiguration`
<a name="s3_DeleteBucketInventoryConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3BucketInventoryConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此命令會移除與給定 S3 儲存貯體對應之名為 'testInventoryName' 的庫存。**  

```
Remove-S3BucketInventoryConfiguration -BucketName 'amzn-s3-demo-bucket' -InventoryId 'testInventoryName'
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-S3BucketInventoryConfiguration (DeleteBucketInventoryConfiguration)" on target "amzn-s3-demo-bucket".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucketInventoryConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3BucketMetricsConfiguration`
<a name="s3_DeleteBucketMetricsConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3BucketMetricsConfiguration`。

**Tools for PowerShell V4**  
**範例 1： 命令會移除給定 S3 儲存貯體中名為 'testmetrics' 的指標篩選條件。**  

```
Remove-S3BucketMetricsConfiguration -BucketName 'amzn-s3-demo-bucket' -MetricsId 'testmetrics'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucketMetricsConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3BucketPolicy`
<a name="s3_DeleteBucketPolicy_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3BucketPolicy`。

**Tools for PowerShell V4**  
**範例 1： 命令會移除與給定 S3 儲存貯體相關聯的儲存貯體政策。**  

```
Remove-S3BucketPolicy -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucketPolicy](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3BucketReplication`
<a name="s3_DeleteBucketReplication_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3BucketReplication`。

**Tools for PowerShell V4**  
**範例 1：刪除與名為 'amzn-s3-demo-bucket' 的儲存貯體相關聯的複寫組態。請注意，此操作需要 s3：DeleteReplicationConfiguration 動作的許可權。在繼續操作之前，系統會提示您確認 - 若要隱藏確認，請使用 -Force 切換變數。**  

```
Remove-S3BucketReplication -BucketName amzn-s3-demo-bucket
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucketReplication](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3BucketTagging`
<a name="s3_DeleteBucketTagging_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3BucketTagging`。

**Tools for PowerShell V4**  
**範例 1：此命令會移除與給定 S3 儲存貯體相關聯的所有標籤。**  

```
Remove-S3BucketTagging -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-S3BucketTagging (DeleteBucketTagging)" on target "amzn-s3-demo-bucket".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucketTagging](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3BucketWebsite`
<a name="s3_DeleteBucketWebsite_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3BucketWebsite`。

**Tools for PowerShell V4**  
**範例 1：此命令會停用給定 S3 儲存貯體的靜態網站託管屬性。**  

```
Remove-S3BucketWebsite -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-S3BucketWebsite (DeleteBucketWebsite)" on target "amzn-s3-demo-bucket".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteBucketWebsite](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3CORSConfiguration`
<a name="s3_DeleteCORSConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3CORSConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此命令會移除給定 S3 儲存貯體的 CORS 組態。**  

```
Remove-S3CORSConfiguration -BucketName 'amzn-s3-demo-bucket'
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-S3CORSConfiguration (DeleteCORSConfiguration)" on target "amzn-s3-demo-bucket".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteCORSConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3LifecycleConfiguration`
<a name="s3_DeleteLifecycleConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3LifecycleConfiguration`。

**Tools for PowerShell V4**  
**範例 1： 命令會移除給定 S3 儲存貯體的所有生命週期規則。**  

```
Remove-S3LifecycleConfiguration -BucketName 'amzn-s3-demo-bucket'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteLifecycleConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3MultipartUpload`
<a name="s3_AbortMultipartUpload_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3MultipartUpload`。

**Tools for PowerShell V4**  
**範例 1：此命令中止 5 天前建立的分段上傳。**  

```
Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -DaysBefore 5
```
**範例 2：此命令中止 2014 年 1 月 2 日之前建立的分段上傳。**  

```
Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -InitiatedDate "Thursday, January 02, 2014"
```
**範例 3：此命令中止 2014 年 1 月 2 日 10:45:37 之前建立的分段上傳。**  

```
Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -InitiatedDate "2014/01/02 10:45:37"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中 [AbortMultipartUpload](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3Object`
<a name="s3_DeleteObjects_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3Object`。

**Tools for PowerShell V4**  
**範例 1：此命令會從儲存貯體 "test-files" 中移除物件"sample.txt"。在執行命令之前，系統會提示您確認；若要隱藏提示，請使用 -Force 切換變數。**  

```
Remove-S3Object -BucketName amzn-s3-demo-bucket -Key sample.txt
```
**範例 2：此命令會移除儲存貯體 "test-files" 中物件"sample.txt" 的指定版本，假設儲存貯體已設定為啟用物件版本。**  

```
Remove-S3Object -BucketName amzn-s3-demo-bucket -Key sample.txt -VersionId HLbxnx6V9omT6AQYVpks8mmFKQcejpqt
```
**範例 3：此命令會將儲存貯體 "test-files" 中的物件"sample1.txt"、"sample2.txt" 和 "sample3.txt" 移除，以成為單一批次操作。無論是刪除成功或錯誤狀態，服務回應都會列出處理的所有索引鍵。若要僅取得無法由服務處理之索引鍵的錯誤，請新增 -ReportErrorsOnly 參數 (也可以使用別名 -Quiet 指定此參數。**  

```
Remove-S3Object -BucketName amzn-s3-demo-bucket -KeyCollection @( "sample1.txt", "sample2.txt", "sample3.txt" )
```
**範例 4：此範例搭配 -KeyCollection 參數使用內嵌表達式，以取得要刪除之物件的索引鍵。Get-S3Object 會傳回 Amazon.S3.Model.S3Object 執行個體的集合，每個執行個體都有一個字串類型的索引鍵成員來識別物件。**  

```
Remove-S3Object -bucketname "amzn-s3-demo-bucket" -KeyCollection (Get-S3Object "test-files" -KeyPrefix "prefix/subprefix" | select -ExpandProperty Key)
```
**範例 5：此範例會取得儲存貯體中具有索引鍵字首「字首/子字首」的所有物件，並將其刪除。請注意，一次只會處理一個傳入物件。對於大型集合，請考慮將集合傳遞至 Cmdlet 的 -InputObject (別名 -S3ObjectCollection) 參數，透過一次服務呼叫來批次執行刪除作業。**  

```
Get-S3Object -BucketName "amzn-s3-demo-bucket" -KeyPrefix "prefix/subprefix" | Remove-S3Object -Force
```
**範例 6：此範例會將代表刪除標記的 Amazon.S3.Model.S3ObjectVersion 執行個體集合傳輸至 Cmdlet，以進行刪除。請注意，一次只會處理一個傳入物件。對於大型集合，請考慮將集合傳遞至 Cmdlet 的 -InputObject (別名 -S3ObjectCollection) 參數，透過一次服務呼叫來批次執行刪除作業。**  

```
(Get-S3Version -BucketName "amzn-s3-demo-bucket").Versions | Where {$_.IsDeleteMarker -eq "True"} | Remove-S3Object -Force
```
**範例 7：此指令碼示範如何透過建構要與 -KeyAndVersionCollection 參數搭配使用的物件陣列，執行一組物件的批次刪除 (在此案例中為刪除標記)。**  

```
$keyVersions = @()
$markers = (Get-S3Version -BucketName $BucketName).Versions | Where {$_.IsDeleteMarker -eq "True"}
foreach ($marker in $markers) { $keyVersions += @{ Key = $marker.Key; VersionId = $marker.VersionId } }
Remove-S3Object -BucketName $BucketName -KeyAndVersionCollection $keyVersions -Force
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteObjects](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3ObjectTagSet`
<a name="s3_DeleteObjectTagging_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3ObjectTagSet`。

**Tools for PowerShell V4**  
**範例 1：此命令會移除與給定 S3 儲存貯體中具索引鍵 'testfile.txt' 之物件相關聯的所有標籤。**  

```
Remove-S3ObjectTagSet -Key 'testfile.txt' -BucketName 'amzn-s3-demo-bucket' -Select '^Key'
```
**輸出：**  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-S3ObjectTagSet (DeleteObjectTagging)" on target "testfile.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y
testfile.txt
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeleteObjectTagging](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Remove-S3PublicAccessBlock`
<a name="s3_DeletePublicAccessBlock_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Remove-S3PublicAccessBlock`。

**Tools for PowerShell V4**  
**範例 1：此命令會關閉給定儲存貯體的封鎖公開存取設定。**  

```
Remove-S3PublicAccessBlock -BucketName 'amzn-s3-demo-bucket' -Force -Select '^BucketName'
```
**輸出：**  

```
amzn-s3-demo-bucket
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DeletePublicAccessBlock](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Set-S3BucketEncryption`
<a name="s3_PutBucketEncryption_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Set-S3BucketEncryption`。

**Tools for PowerShell V4**  
**範例 1：此命令會在給定的儲存貯體上使用 Amazon S3 受管金鑰 (SSE-S3)，以啟用預設 AES256 伺服器端加密。 **  

```
$Encryptionconfig = @{ServerSideEncryptionByDefault = @{ServerSideEncryptionAlgorithm = "AES256"}}
Set-S3BucketEncryption -BucketName 'amzn-s3-demo-bucket' -ServerSideEncryptionConfiguration_ServerSideEncryptionRule $Encryptionconfig
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucketEncryption](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Test-S3Bucket`
<a name="s3_Test-S3Bucket_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Test-S3Bucket`。

**Tools for PowerShell V4**  
**範例 1：如果有儲存貯體，則此命令會傳回 True，否則傳回 False。即使儲存貯體不屬於使用者，命令也會傳回 True。**  

```
Test-S3Bucket -BucketName amzn-s3-demo-bucket
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [Test-S3Bucket](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3BucketAccelerateConfiguration`
<a name="s3_PutBucketAccelerateConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3BucketAccelerateConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此命令會使給定 S3 儲存貯體的傳輸加速。**  

```
$statusVal = New-Object Amazon.S3.BucketAccelerateStatus('Enabled')
Write-S3BucketAccelerateConfiguration -BucketName 'amzn-s3-demo-bucket' -AccelerateConfiguration_Status $statusVal
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucketAccelerateConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3BucketNotification`
<a name="s3_PutBucketNotification_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3BucketNotification`。

**Tools for PowerShell V4**  
**範例 1：此範例會設定 S3 事件 ObjectRemovedDelete 的 SNS 主題組態，並啟用給定 s3 儲存貯體的通知**  

```
$topic =  [Amazon.S3.Model.TopicConfiguration] @{
  Id = "delete-event"
  Topic = "arn:aws:sns:eu-west-1:123456789012:topic-1"
  Event = [Amazon.S3.EventType]::ObjectRemovedDelete
}

Write-S3BucketNotification -BucketName amzn-s3-demo-bucket -TopicConfiguration $topic
```
**範例 2：此範例會為傳送至 Lambda 函式的給定儲存貯體，啟用 ObjectCreatedAll 通知。**  

```
$lambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{
  Events = "s3:ObjectCreated:*"
  FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:rdplock"
  Id = "ObjectCreated-Lambda"
  Filter = @{
    S3KeyFilter = @{
      FilterRules = @(
        @{Name="Prefix";Value="dada"}
        @{Name="Suffix";Value=".pem"}
      )
    }
  }
}

Write-S3BucketNotification -BucketName amzn-s3-demo-bucket -LambdaFunctionConfiguration $lambdaConfig
```
**範例 3：此範例會根據不同的索引鍵尾碼建立 2 個不同的 Lambda 組態，並在單一命令中同時設定兩者。**  

```
#Lambda Config 1

$firstLambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{
  Events = "s3:ObjectCreated:*"
  FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:verifynet"
  Id = "ObjectCreated-dada-ps1"
  Filter = @{
    S3KeyFilter = @{
      FilterRules = @(
        @{Name="Prefix";Value="dada"}
        @{Name="Suffix";Value=".ps1"}
      )
    }
  }
}

#Lambda Config 2

$secondlambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{
  Events = [Amazon.S3.EventType]::ObjectCreatedAll
  FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:verifyssm"
  Id = "ObjectCreated-dada-json"
  Filter = @{
    S3KeyFilter = @{
      FilterRules = @(
        @{Name="Prefix";Value="dada"}
        @{Name="Suffix";Value=".json"}
      )
    }
  }
}

Write-S3BucketNotification -BucketName amzn-s3-demo-bucket -LambdaFunctionConfiguration $firstLambdaConfig,$secondlambdaConfig
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucketNotification](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3BucketReplication`
<a name="s3_PutBucketReplication_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3BucketReplication`。

**Tools for PowerShell V4**  
**範例 1：此範例設定了一個複寫組態，其中的一條規則允許將儲存貯體 'amzn-s3-demo-bucket' 中鍵名字首為 "TaxDocs" 所建立的任何新物件，複製到 'amzn-s3-demo-bucket' 儲存貯體。**  

```
$rule1 = New-Object Amazon.S3.Model.ReplicationRule
$rule1.ID = "Rule-1"
$rule1.Status = "Enabled"
$rule1.Prefix = "TaxDocs"
$rule1.Destination = @{ BucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket" }
    
$params = @{
    BucketName = "amzn-s3-demo-bucket"
    Configuration_Role = "arn:aws:iam::35667example:role/CrossRegionReplicationRoleForS3"
    Configuration_Rule = $rule1
}

Write-S3BucketReplication @params
```
**範例 2：此範例設定具有多個規則的複寫組態，可將任何以鍵名字首 "TaxDocs" 或 "OtherDocs" 建立的新物件，複寫至 'amzn-s3-demo-bucket' 儲存貯體。索引鍵字首不得重疊。**  

```
$rule1 = New-Object Amazon.S3.Model.ReplicationRule
$rule1.ID = "Rule-1"
$rule1.Status = "Enabled"
$rule1.Prefix = "TaxDocs"
$rule1.Destination = @{ BucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket" }
    
$rule2 = New-Object Amazon.S3.Model.ReplicationRule
$rule2.ID = "Rule-2"
$rule2.Status = "Enabled"
$rule2.Prefix = "OtherDocs"
$rule2.Destination = @{ BucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket" }
    
$params = @{
    BucketName = "amzn-s3-demo-bucket"
    Configuration_Role = "arn:aws:iam::35667example:role/CrossRegionReplicationRoleForS3"
    Configuration_Rule = $rule1,$rule2
}

Write-S3BucketReplication @params
```
**範例 3：此範例會更新指定之儲存貯體上的複寫組態，以停用控制將鍵名字首為 "TaxDocs" 之物件複寫至 'amzn-s3-demo-bucket' 儲存貯體的規則。**  

```
$rule1 = New-Object Amazon.S3.Model.ReplicationRule
$rule1.ID = "Rule-1"
$rule1.Status = "Disabled"
$rule1.Prefix = "TaxDocs"
$rule1.Destination = @{ BucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket" }
    
$params = @{
    BucketName = "amzn-s3-demo-bucket"
    Configuration_Role = "arn:aws:iam::35667example:role/CrossRegionReplicationRoleForS3"
    Configuration_Rule = $rule1
}

Write-S3BucketReplication @params
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucketReplication](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3BucketRequestPayment`
<a name="s3_PutBucketRequestPayment_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3BucketRequestPayment`。

**Tools for PowerShell V4**  
**範例 1：更新名為 'amzn-s3-demo-bucket' 之儲存貯體的請求付費組態，如此一來，請求從儲存貯體下載的人員將需支付下載費用。根據預設，儲存貯體擁有者需支付下載費用。若要將請求付費設回預設，請針對 RequestPaymentConfiguration\$1Payer 參數使用 'BucketOwner'。**  

```
Write-S3BucketRequestPayment -BucketName amzn-s3-demo-bucket -RequestPaymentConfiguration_Payer Requester
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucketRequestPayment](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3BucketTagging`
<a name="s3_PutBucketTagging_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3BucketTagging`。

**Tools for PowerShell V4**  
**範例 1：此命令會將兩個標籤套用至名為 `cloudtrail-test-2018` 的儲存貯體：一個是索引鍵為 Stage 且值為 Test 的標籤，另一個是索引鍵為 Environment 且值為 Alpha 的標籤。若要驗證標籤是否已新增至儲存貯體，請執行 `Get-S3BucketTagging -BucketName bucket_name`。結果應該會顯示您在第一個命令中套用至儲存貯體的標籤。請注意，`Write-S3BucketTagging` 會覆寫儲存貯體上設定的整個現有標籤。若要新增或刪除個別標籤，請執行資源群組和標記 API Cmdlet，`Add-RGTResourceTag` 和 `Remove-RGTResourceTag`。或者，在 AWS 管理主控台中使用標籤編輯器來管理 S3 儲存貯體標籤。**  

```
Write-S3BucketTagging -BucketName amzn-s3-demo-bucket -TagSet @( @{ Key="Stage"; Value="Test" }, @{ Key="Environment"; Value="Alpha" } )
```
**範例 2：此命令會將名為 `cloudtrail-test-2018` 的儲存貯體傳輸至 `Write-S3BucketTagging` cmdlet。範例會將標籤 Stage:Production 和 Department:Finance 套用至儲存貯體。請注意，`Write-S3BucketTagging` 會覆寫儲存貯體上設定的整個現有標籤。**  

```
Get-S3Bucket -BucketName amzn-s3-demo-bucket | Write-S3BucketTagging -TagSet @( @{ Key="Stage"; Value="Production" }, @{ Key="Department"; Value="Finance" } )
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucketTagging](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3BucketVersioning`
<a name="s3_PutBucketVersioning_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3BucketVersioning`。

**Tools for PowerShell V4**  
**範例 1： 命令會啟用給定 S3 儲存貯體的版本控制。**  

```
Write-S3BucketVersioning -BucketName 'amzn-s3-demo-bucket' -VersioningConfig_Status Enabled
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucketVersioning](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3BucketWebsite`
<a name="s3_PutBucketWebsite_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3BucketWebsite`。

**Tools for PowerShell V4**  
**範例 1：命令會啟用給定儲存貯體的網站託管，其中索引文件為 'index.html'，錯誤文件為 'error.html'。**  

```
Write-S3BucketWebsite -BucketName 'amzn-s3-demo-bucket' -WebsiteConfiguration_IndexDocumentSuffix 'index.html' -WebsiteConfiguration_ErrorDocument 'error.html'
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutBucketWebsite](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3LifecycleConfiguration`
<a name="s3_PutLifecycleConfiguration_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3LifecycleConfiguration`。

**Tools for PowerShell V4**  
**範例 1：此範例會寫入/取代 \$1NewRule 中提供的組態。此組態可確保限制具有給定字首和標籤值的範圍物件。**  

```
$NewRule = [Amazon.S3.Model.LifecycleRule] @{
		Expiration =  @{
			Days=  50
		}
		Id =  "Test-From-Write-cmdlet-1"
		Filter=  @{
			LifecycleFilterPredicate =  [Amazon.S3.Model.LifecycleAndOperator]@{
				Operands=  @(
					[Amazon.S3.Model.LifecyclePrefixPredicate] @{
						"Prefix" =  "py"
					},
					[Amazon.S3.Model.LifecycleTagPredicate] @{
						"Tag"=  @{
							"Key" =  "non-use"
							"Value" = "yes"
						}
					}
				)
			}
		}
		"Status"= 'Enabled'
		NoncurrentVersionExpiration = @{
			NoncurrentDays = 75
		}
	}
    
	Write-S3LifecycleConfiguration -BucketName amzn-s3-demo-bucket -Configuration_Rule $NewRule
```
**範例 2：此範例使用篩選條件設定多個規則。\$1ArchiveRule 會將要在 30 天內封存的物件設定為 Glacier，並將 120 天內封存的物件的設定為 DeepArchive。\$1ExpireRule 會針對具有 'py' 字首和 tag:key 'archieved' 設定為 'yes' 的物件，使其目前和先前版本在 150 天內到期。**  

```
$ExpireRule = [Amazon.S3.Model.LifecycleRule] @{
		Expiration =  @{
			Days=  150
		}
		Id =  "Remove-in-150-days"
		Filter=  @{
			LifecycleFilterPredicate =  [Amazon.S3.Model.LifecycleAndOperator]@{
				Operands=  @(
					[Amazon.S3.Model.LifecyclePrefixPredicate] @{
						"Prefix" =  "py"
					},
					[Amazon.S3.Model.LifecycleTagPredicate] @{
						"Tag"=  @{
							"Key" =  "archived"
							"Value" = "yes"
						}
					}
				)
			}
		}
		Status= 'Enabled'
		NoncurrentVersionExpiration = @{
			NoncurrentDays = 150
		}
	}

	$ArchiveRule = [Amazon.S3.Model.LifecycleRule] @{
		Expiration =  $null
		Id =  "Archive-to-Glacier-in-30-days"
		Filter=  @{
			LifecycleFilterPredicate =  [Amazon.S3.Model.LifecycleAndOperator]@{
				Operands= @(
					[Amazon.S3.Model.LifecyclePrefixPredicate] @{
						"Prefix" =  "py"
					},
					[Amazon.S3.Model.LifecycleTagPredicate] @{
						"Tag"=  @{
							"Key" =  "reviewed"
							"Value" = "yes"
						}
					}
				)
			}
		}
		Status = 'Enabled'
		NoncurrentVersionExpiration = @{
			NoncurrentDays = 75
		}
		Transitions = @(
			@{
				Days = 30
				"StorageClass"= 'Glacier'
			},
			@{
				Days = 120
				"StorageClass"= [Amazon.S3.S3StorageClass]::DeepArchive
			}
		)
	}

	Write-S3LifecycleConfiguration -BucketName amzn-s3-demo-bucket -Configuration_Rule $ExpireRule,$ArchiveRule
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutLifecycleConfiguration](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3Object`
<a name="s3_PutObject_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3Object`。

**Tools for PowerShell V4**  
**範例 1：此命令會將單一檔案 "local-sample.txt" 上傳至 Amazon S3，並在儲存貯體 "test-files" 中建立具索引鍵 "sample.txt" 的物件。**  

```
Write-S3Object -BucketName amzn-s3-demo-bucket -Key "sample.txt" -File .\local-sample.txt
```
**範例 2：此命令會將單一檔案 "sample.txt" 上傳至 Amazon S3，並在儲存貯體 "test-files" 中建立具索引鍵 "sample.txt" 的物件。如果未提供 -Key 參數，則會將檔案名稱用作 S3 物件索引鍵。**  

```
Write-S3Object -BucketName amzn-s3-demo-bucket -File .\sample.txt
```
**範例 3：此命令會將單一檔案 "local-sample.txt" 上傳至 Amazon S3，並在儲存貯體 "test-files" 中建立具索引鍵 "prefix/to/sample.txt" 的物件。**  

```
Write-S3Object -BucketName amzn-s3-demo-bucket -Key "prefix/to/sample.txt" -File .\local-sample.txt
```
**範例 4：此命令會將子目錄 "Scripts" 中的所有檔案上傳至儲存貯體 "test-files"，並將通用索引鍵字首 "SampleScripts" 套用至每個物件。每個上傳的檔案都會有一個索引鍵 "SampleScripts/filename"，其中 'filename' 會有所不同。**  

```
Write-S3Object -BucketName amzn-s3-demo-bucket -Folder .\Scripts -KeyPrefix SampleScripts\
```
**範例 5：此命令會將本機目錄 "Scripts" 中的所有 \$1.ps1 檔案上傳至儲存貯體 "test-files"，並將通用索引鍵字首 "SampleScripts" 套用至每個物件。每個上傳的檔案都會有一個索引鍵 "SampleScripts/filename.ps1"，其中 'filename' 會有所不同。**  

```
Write-S3Object -BucketName amzn-s3-demo-bucket -Folder .\Scripts -KeyPrefix SampleScripts\ -SearchPattern *.ps1
```
**範例 6：此命令會建立新的 S3 物件，其中包含索引鍵 'sample.txt' 的指定內容字串。**  

```
Write-S3Object -BucketName amzn-s3-demo-bucket -Key "sample.txt" -Content "object contents"
```
**範例 7：此命令會上傳指定的檔案 (檔案名稱用作索引鍵)，並將指定的標籤套用至新物件。**  

```
Write-S3Object -BucketName amzn-s3-demo-bucket -File "sample.txt" -TagSet @{Key="key1";Value="value1"},@{Key="key2";Value="value2"}
```
**範例 8：此命令會以遞迴方式上傳指定的資料夾，並將指定的標籤套用至所有新物件。**  

```
Write-S3Object -BucketName amzn-s3-demo-bucket -Folder . -KeyPrefix "TaggedFiles" -Recurse -TagSet @{Key="key1";Value="value1"},@{Key="key2";Value="value2"}
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutObject](https://docs.aws.amazon.com/powershell/v4/reference)。

### `Write-S3ObjectRetention`
<a name="s3_PutObjectRetention_powershell_4_topic"></a>

以下程式碼範例顯示如何使用 `Write-S3ObjectRetention`。

**Tools for PowerShell V4**  
**範例 1： 命令會針對給定 S3 儲存貯體中的 'testfile.txt' 物件啟用控管保留模式，直到「2019 年 12 月 31 日 00:00:00」為止。**  

```
Write-S3ObjectRetention -BucketName 'amzn-s3-demo-bucket' -Key 'testfile.txt' -Retention_Mode GOVERNANCE -Retention_RetainUntilDate "2019-12-31T00:00:00"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [PutObjectRetention](https://docs.aws.amazon.com/powershell/v4/reference)。