

# 将 `GetBucketReplication` 与 Amazon SDK 或 CLI 配合使用
<a name="s3_example_s3_GetBucketReplication_section"></a>

以下代码示例演示如何使用 `GetBucketReplication`。

------
#### [ CLI ]

**Amazon CLI**  
以下命令检索名为 `amzn-s3-demo-bucket` 的存储桶的复制配置：  

```
aws s3api get-bucket-replication --bucket {{amzn-s3-demo-bucket}}
```
输出：  

```
{
    "ReplicationConfiguration": {
        "Rules": [
            {
                "Status": "Enabled",
                "Prefix": "",
                "Destination": {
                    "Bucket": "arn:aws:s3:::amzn-s3-demo-bucket-backup",
                    "StorageClass": "STANDARD"
                },
                "ID": "ZmUwNzE4ZmQ4tMjVhOS00MTlkLOGI4NDkzZTIWJjNTUtYTA1"
            }
        ],
        "Role": "arn:aws:iam::123456789012:role/s3-replication-role"
    }
}
```
+  有关 API 详细信息，请参阅《Amazon CLI 命令参考》**中的 [GetBucketReplication](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/get-bucket-replication.html)。

------
#### [ Java ]

**适用于 Java 的 SDK 2.x**  
 查看 GitHub，了解更多信息。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/s3#code-examples)中查找完整示例，了解如何进行设置和运行。

```
    /**
     * Retrieves the replication details for the specified S3 bucket.
     *
     * @param s3Client           the S3 client used to interact with the S3 service
     * @param sourceBucketName   the name of the S3 bucket to retrieve the replication details for
     *
     * @throws S3Exception if there is an error retrieving the replication details
     */
    public static void getReplicationDetails(S3Client s3Client, String sourceBucketName) {
        GetBucketReplicationRequest getRequest = GetBucketReplicationRequest.builder()
            .bucket(sourceBucketName)
            .build();

        try {
            ReplicationConfiguration replicationConfig = s3Client.getBucketReplication(getRequest).replicationConfiguration();
            ReplicationRule rule = replicationConfig.rules().get(0);
            System.out.println("Retrieved destination bucket: " + rule.destination().bucket());
            System.out.println("Retrieved priority: " + rule.priority());
            System.out.println("Retrieved source-bucket replication rule status: " + rule.status());

        } catch (S3Exception e) {
            System.err.println("Failed to retrieve replication details: " + e.awsErrorDetails().errorMessage());
        }
    }
```
+  有关 API 详细信息，请参阅《Amazon SDK for Java 2.x API Reference》**中的 [GetBucketReplication](https://docs.amazonaws.cn/goto/SdkForJavaV2/s3-2006-03-01/GetBucketReplication)。

------
#### [ PowerShell ]

**适用于 PowerShell V4 的工具**  
**示例 1：返回在名为“amzn-s3-demo-bucket”的存储桶上设置的复制配置信息。**  

```
Get-S3BucketReplication -BucketName amzn-s3-demo-bucket
```
+  有关 API 详细信息，请参阅《Amazon Tools for PowerShell Cmdlet Reference (V4)》**中的 [GetBucketReplication](https://docs.amazonaws.cn/powershell/v4/reference)。

**适用于 PowerShell V5 的工具**  
**示例 1：返回在名为“amzn-s3-demo-bucket”的存储桶上设置的复制配置信息。**  

```
Get-S3BucketReplication -BucketName amzn-s3-demo-bucket
```
+  有关 API 详细信息，请参阅《Amazon Tools for PowerShell Cmdlet Reference (V5)》**中的 [GetBucketReplication](https://docs.amazonaws.cn/powershell/v5/reference)。

------

有关 Amazon SDK 开发人员指南和代码示例的完整列表，请参阅 [使用 Amazon SDK 通过 Amazon S3 进行开发](sdk-general-information-section.md)。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。