

のバージョン 5 (V5) AWS Tools for PowerShell がリリースされました。

重要な変更とアプリケーションの移行については、[「移行トピック](https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html)」を参照してください。

 [https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html](https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html)

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Tools for PowerShell V5 を使用した CloudFront の例
<a name="powershell_cloudfront_code_examples"></a>

次のコード例は、CloudFront で AWS Tools for PowerShell V5 を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

*アクション*はより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。

**Topics**
+ [アクション](#actions)

## アクション
<a name="actions"></a>

### `Get-CFCloudFrontOriginAccessIdentity`
<a name="cloudfront_GetCloudFrontOriginAccessIdentity_powershell_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 コマンドレットリファレンス (V5)* の「[GetCloudFrontOriginAccessIdentity](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

### `Get-CFCloudFrontOriginAccessIdentityConfig`
<a name="cloudfront_GetCloudFrontOriginAccessIdentityConfig_powershell_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 コマンドレットリファレンス (V5)* の「[GetCloudFrontOriginAccessIdentityConfig](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`Get-CFCloudFrontOriginAccessIdentityList` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: この例では、Amazon CloudFront オリジンアクセスアイデンティティのリストを返します。-MaxItem パラメータは値として 2 を指定しているため、結果には 2 つのアイデンティティが含まれます。**  

```
Get-CFCloudFrontOriginAccessIdentityList -MaxItem 2
```
**出力:**  

```
IsTruncated : True
Items       : {E326XXXXXXXXXT, E1YWXXXXXXX9B}
Marker      :
MaxItems    : 2
NextMarker  : E1YXXXXXXXXX9B
Quantity    : 2
```
+  API の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V5)* の「[ListCloudFrontOriginAccessIdentities](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`Get-CFDistribution` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: 特定のディストリビューションに関する情報を取得します。**  

```
Get-CFDistribution -Id EXAMPLE0000ID
```
+  API の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V5)* の「[GetDistribution](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`Get-CFDistributionConfig` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: 特定のディストリビューションの設定を取得します。**  

```
Get-CFDistributionConfig -Id EXAMPLE0000ID
```
+  API の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V5)* の「[GetDistributionConfig](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`Get-CFDistributionList` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: ディストリビューションを返します。**  

```
Get-CFDistributionList
```
+  API の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V5)* の「[ListDistributions](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

### `New-CFDistribution`
<a name="cloudfront_CreateDistribution_powershell_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 コマンドレットリファレンス (V5)* の「[CreateDistribution](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`New-CFInvalidation` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: この例では、ID が EXAMPLENSTXAXE であるディストリビューションで、新しいキャッシュ削除を作成します。CallerReference はユーザーが選択した固有の ID です。この例では、2019 年 5 月 15 日の午前 9 時を表すタイムスタンプを使用しています。\$1Paths 変数には、ユーザーがディストリビューションのキャッシュに含めたくない画像ファイルやメディアファイルへの 3 つのパスが格納されます。-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 コマンドレットリファレンス (V5)* の「[CreateInvalidation](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`New-CFSignedCookie` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: 既定ポリシーを使用して、指定されたリソースに署名付き Cookie を作成します。Cookie は 1 年間有効です。**  

```
$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 時間後に有効になり、1 週間後に期限切れになります。**  

```
$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 時間後に有効になり、1 週間後に期限切れになります。リソースへのアクセスは、指定の 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 コマンドレットリファレンス (V5)* の「[New-CFSignedCookie](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`New-CFSignedUrl` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: 既定ポリシーを使用して、指定リソースへの署名付き URL を作成します。URL は 1 時間有効です。署名付き 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 時間後に有効になり、1 週間後に期限切れになります。**  

```
$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 時間後に有効になり、1 週間後に期限切れになります。リソースへのアクセスは、指定の 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 コマンドレットリファレンス (V5)* の「[New-CFSignedUrl](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。