

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

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

# 使用 Tools for PowerShell V5 的 CloudFront 範例
<a name="powershell_5_cloudfront_code_examples"></a>

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

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

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

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

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

### `Get-CFCloudFrontOriginAccessIdentity`
<a name="cloudfront_GetCloudFrontOriginAccessIdentity_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：此範例傳回由 -Id 參數指定的特定 Amazon CloudFront 原始存取身分。雖然不需要 -Id 參數，但如果沒有指定該參數，就不會傳回任何結果。**  

```
Get-CFCloudFrontOriginAccessIdentity -Id E3XXXXXXXXXXRT
```
**輸出：**  

```
      CloudFrontOriginAccessIdentityConfig    Id                                      S3CanonicalUserId
      ------------------------------------    --                                      -----------------
      Amazon.CloudFront.Model.CloudFrontOr... E3XXXXXXXXXXRT                          4b6e...
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [GetCloudFrontOriginAccessIdentity](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-CFCloudFrontOriginAccessIdentityConfig`
<a name="cloudfront_GetCloudFrontOriginAccessIdentityConfig_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：此範例傳回 -Id 參數所指定之單一 Amazon CloudFront 原始存取身分的組態資訊。如果未指定 -Id 參數，則會發生錯誤。**  

```
Get-CFCloudFrontOriginAccessIdentityConfig -Id E3XXXXXXXXXXRT
```
**輸出：**  

```
      CallerReference                                             Comment
      ---------------                                             -------
      mycallerreference: 2/1/2011 1:16:32 PM                      Caller reference: 2/1/2011 1:16:32 PM
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [GetCloudFrontOriginAccessIdentityConfig](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-CFCloudFrontOriginAccessIdentityList`
<a name="cloudfront_ListCloudFrontOriginAccessIdentities_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：此範例傳回 Amazon CloudFront 原始存取身分的清單。由於 -MaxItem 參數指定值 2，因此結果包含兩個身分。**  

```
Get-CFCloudFrontOriginAccessIdentityList -MaxItem 2
```
**輸出：**  

```
IsTruncated : True
Items       : {E326XXXXXXXXXT, E1YWXXXXXXX9B}
Marker      :
MaxItems    : 2
NextMarker  : E1YXXXXXXXXX9B
Quantity    : 2
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [ListCloudFrontOriginAccessIdentities](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-CFDistribution`
<a name="cloudfront_GetDistribution_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：擷取特定分佈的資訊。**  

```
Get-CFDistribution -Id EXAMPLE0000ID
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [GetDistribution](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-CFDistributionConfig`
<a name="cloudfront_GetDistributionConfig_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：擷取特定分佈的組態。**  

```
Get-CFDistributionConfig -Id EXAMPLE0000ID
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [GetDistributionConfig](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-CFDistributionList`
<a name="cloudfront_ListDistributions_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：傳回分佈。**  

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

### `New-CFDistribution`
<a name="cloudfront_CreateDistribution_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：建立以日誌記錄和快取設定的基本 CloudFront 分佈。**  

```
$origin = New-Object Amazon.CloudFront.Model.Origin
$origin.DomainName = "amzn-s3-demo-bucket.s3.amazonaws.com"
$origin.Id = "UniqueOrigin1"
$origin.S3OriginConfig = New-Object Amazon.CloudFront.Model.S3OriginConfig
$origin.S3OriginConfig.OriginAccessIdentity = ""
New-CFDistribution `
      -DistributionConfig_Enabled $true `
      -DistributionConfig_Comment "Test distribution" `
      -Origins_Item $origin `
      -Origins_Quantity 1 `
      -Logging_Enabled $true `
      -Logging_IncludeCookie $true `
      -Logging_Bucket amzn-s3-demo-logging-bucket.s3.amazonaws.com `
      -Logging_Prefix "help/" `
      -DistributionConfig_CallerReference Client1 `
      -DistributionConfig_DefaultRootObject index.html `
      -DefaultCacheBehavior_TargetOriginId $origin.Id `
      -ForwardedValues_QueryString $true `
      -Cookies_Forward all `
      -WhitelistedNames_Quantity 0 `
      -TrustedSigners_Enabled $false `
      -TrustedSigners_Quantity 0 `
      -DefaultCacheBehavior_ViewerProtocolPolicy allow-all `
      -DefaultCacheBehavior_MinTTL 1000 `
      -DistributionConfig_PriceClass "PriceClass_All" `
      -CacheBehaviors_Quantity 0 `
      -Aliases_Quantity 0
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateDistribution](https://docs.aws.amazon.com/powershell/v5/reference)。

### `New-CFInvalidation`
<a name="cloudfront_CreateInvalidation_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：此範例會在 ID 為 EXAMPLENSTXAXE 的分佈上建立新的失效證明。CallerReference 是使用者選擇的唯一 ID；在此情況下，會使用代表 2019 年 5 月 15 日上午 9:00 的時間戳記。\$1Paths 變數存放了使用者不想要當作分佈快取一部分的映像和媒體檔案的三個路徑。-Paths\$1Quantity 參數值是 -Paths\$1Item 參數中指定的路徑總數。**  

```
$Paths = "/images/*.gif", "/images/image1.jpg", "/videos/*.mp4"
New-CFInvalidation -DistributionId "EXAMPLENSTXAXE" -InvalidationBatch_CallerReference 20190515090000 -Paths_Item $Paths -Paths_Quantity 3
```
**輸出：**  

```
Invalidation                         Location                                                                                          
------------                         --------                                                                                          
Amazon.CloudFront.Model.Invalidation https://cloudfront.amazonaws.com/2018-11-05/distribution/EXAMPLENSTXAXE/invalidation/EXAMPLE8NOK9H
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateInvalidation](https://docs.aws.amazon.com/powershell/v5/reference)。

### `New-CFSignedCookie`
<a name="cloudfront_New-CFSignedCookie_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：使用標準政策為指定的資源建立已簽署的 Cookie。Cookie 有效期為一年。**  

```
$params = @{
	"ResourceUri"="http://xyz.cloudfront.net/image1.jpeg"
	"KeyPairId"="AKIAIOSFODNN7EXAMPLE"
	"PrivateKeyFile"="C:\pk-AKIAIOSFODNN7EXAMPLE.pem"
	"ExpiresOn"=(Get-Date).AddYears(1)
}
New-CFSignedCookie @params
```
**輸出：**  

```
Expires
-------
[CloudFront-Expires, 1472227284]
```
**範例 2：使用自訂政策為指定的資源建立已簽署的 Cookie。Cookie 將在 24 小時內生效，並在一週後到期。**  

```
$start = (Get-Date).AddHours(24)
$params = @{
	"ResourceUri"="http://xyz.cloudfront.net/content/*.jpeg"
	"KeyPairId"="AKIAIOSFODNN7EXAMPLE"
	"PrivateKeyFile"="C:\pk-AKIAIOSFODNN7EXAMPLE.pem"
	"ExpiresOn"=$start.AddDays(7)
    "ActiveFrom"=$start
}

New-CFSignedCookie @params
```
**輸出：**  

```
Policy
------
[CloudFront-Policy, eyJTd...wIjo...
```
**範例 3：使用自訂政策為指定的資源建立已簽署的 Cookie。Cookie 將在 24 小時內生效，並在一週後到期。資源的存取僅限於指定的 ip 範圍。**  

```
$start = (Get-Date).AddHours(24)
$params = @{
	"ResourceUri"="http://xyz.cloudfront.net/content/*.jpeg"
	"KeyPairId"="AKIAIOSFODNN7EXAMPLE"
	"PrivateKeyFile"="C:\pk-AKIAIOSFODNN7EXAMPLE.pem"
	"ExpiresOn"=$start.AddDays(7)
    "ActiveFrom"=$start
	"IpRange"="192.0.2.0/24"
}

New-CFSignedCookie @params
```
**輸出：**  

```
Policy                                                                                                                                         ------                                                                                                                                         [CloudFront-Policy, eyJTd...wIjo...
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [New-CFSignedCookie](https://docs.aws.amazon.com/powershell/v5/reference)。

### `New-CFSignedUrl`
<a name="cloudfront_New-CFSignedUrl_powershell_5_topic"></a>

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

**Tools for PowerShell V5**  
**範例 1：使用標準政策建立指定資源的已簽署 URL。URL 的有效期為一小時。包含已簽署 URL 的 System.Uri 物件會發送到管道。**  

```
$params = @{
	"ResourceUri"="https://cdn.example.com/index.html"
	"KeyPairId"="AKIAIOSFODNN7EXAMPLE"
	"PrivateKeyFile"="C:\pk-AKIAIOSFODNN7EXAMPLE.pem"
	"ExpiresOn"=(Get-Date).AddHours(1)
}
New-CFSignedUrl @params
```
**範例 2：使用自訂政策建立指定資源的已簽署 URL。url 將在 24 小時內生效，並在一週後到期。**  

```
$start = (Get-Date).AddHours(24)
$params = @{
	"ResourceUri"="https://cdn.example.com/index.html"
	"KeyPairId"="AKIAIOSFODNN7EXAMPLE"
	"PrivateKeyFile"="C:\pk-AKIAIOSFODNN7EXAMPLE.pem"
	"ExpiresOn"=(Get-Date).AddDays(7)
    "ActiveFrom"=$start
}
New-CFSignedUrl @params
```
**範例 3：使用自訂政策建立指定資源的已簽署 URL。url 將在 24 小時內生效，並在一週後到期。資源的存取僅限於指定的 ip 範圍。**  

```
$start = (Get-Date).AddHours(24)
$params = @{
	"ResourceUri"="https://cdn.example.com/index.html"
	"KeyPairId"="AKIAIOSFODNN7EXAMPLE"
	"PrivateKeyFile"="C:\pk-AKIAIOSFODNN7EXAMPLE.pem"
	"ExpiresOn"=(Get-Date).AddDays(7)
    "ActiveFrom"=$start
    "IpRange"="192.0.2.0/24"	
}
New-CFSignedUrl @params
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [New-CFSignedUrl](https://docs.aws.amazon.com/powershell/v5/reference)。