

的版本 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)

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# CloudFront 使用适用于 PowerShell V5 的工具的示例
<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`。

**适用于 PowerShell V5 的工具**  
**示例 1：此示例返回由-Id 参数指定的特定亚马逊 CloudFront 源访问身份。尽管不需要 -Id 参数，但如果您不指定该参数，则不会返回任何结果。**  

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

```
      CloudFrontOriginAccessIdentityConfig    Id                                      S3CanonicalUserId
      ------------------------------------    --                                      -----------------
      Amazon.CloudFront.Model.CloudFrontOr... E3XXXXXXXXXXRT                          4b6e...
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [GetCloudFrontOriginAccessIdentity](https://docs.aws.amazon.com/powershell/v5/reference)中的。

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

以下代码示例演示了如何使用 `Get-CFCloudFrontOriginAccessIdentityConfig`。

**适用于 PowerShell V5 的工具**  
**示例 1：此示例返回有关单个 Amazon CloudFront 源访问身份的配置信息，该身份由-Id 参数指定。如果未指定 -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 参考 (V* 5) [GetCloudFrontOriginAccessIdentityConfig](https://docs.aws.amazon.com/powershell/v5/reference)中的。

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

以下代码示例演示了如何使用 `Get-CFCloudFrontOriginAccessIdentityList`。

**适用于 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 参考 (V* 5) [ListCloudFrontOriginAccessIdentities](https://docs.aws.amazon.com/powershell/v5/reference)中的。

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

以下代码示例演示了如何使用 `Get-CFDistribution`。

**适用于 PowerShell V5 的工具**  
**示例 1：检索有关特定分配的信息。**  

```
Get-CFDistribution -Id EXAMPLE0000ID
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [GetDistribution](https://docs.aws.amazon.com/powershell/v5/reference)中的。

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

以下代码示例演示了如何使用 `Get-CFDistributionConfig`。

**适用于 PowerShell V5 的工具**  
**示例 1：检索有关特定分配的配置。**  

```
Get-CFDistributionConfig -Id EXAMPLE0000ID
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [GetDistributionConfig](https://docs.aws.amazon.com/powershell/v5/reference)中的。

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

以下代码示例演示了如何使用 `Get-CFDistributionList`。

**适用于 PowerShell V5 的工具**  
**示例 1：返回分配。**  

```
Get-CFDistributionList
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [ListDistributions](https://docs.aws.amazon.com/powershell/v5/reference)中的。

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

以下代码示例演示了如何使用 `New-CFDistribution`。

**适用于 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 参考 (V* 5) [CreateDistribution](https://docs.aws.amazon.com/powershell/v5/reference)中的。

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

以下代码示例演示了如何使用 `New-CFInvalidation`。

**适用于 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 参考 (V* 5) [CreateInvalidation](https://docs.aws.amazon.com/powershell/v5/reference)中的。

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

以下代码示例演示了如何使用 `New-CFSignedCookie`。

**适用于 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) 中的 “新增-C CFSigned* [ookie](https://docs.aws.amazon.com/powershell/v5/reference)”。

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

以下代码示例演示了如何使用 `New-CFSignedUrl`。

**适用于 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 小时内开始生效，并将在一周后过期。**  

```
$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 参考文档 (V* 5) 中的[新建CFSigned网址](https://docs.aws.amazon.com/powershell/v5/reference)。