

# 将 S3 分批操作与 S3 对象锁定保留监管模式结合使用
<a name="batch-ops-governance-mode"></a>

以下示例基于上述创建信任策略以及设置 S3 分批操作和 S3 对象锁定配置权限的示例而构建。本示例说明如何跨多个对象应用 S3 对象锁定保留监管，`retain until date` 为 2025 年 1 月 30 日。它会创建一个分批操作任务，该任务使用清单存储桶，并在报告存储桶中报告结果。

要使用以下示例，请将 *`user input placeholders`* 替换为您自己的信息。

## 使用 Amazon CLI
<a name="batch-ops-cli-object-lock-governance-example"></a>

以下 Amazon CLI 示例说明如何使用批量操作跨多个对象应用 S3 对象锁定保留监管模式。

**Example — 跨多个对象应用 S3 对象锁定保留监管，“保留到期日”为 2025 年 1 月 30 日**  

```
export AWS_PROFILE='aws-user'
export AWS_DEFAULT_REGION='us-west-2'
export ACCOUNT_ID=123456789012
export ROLE_ARN='arn:aws:iam::123456789012:role/batch_operations-objectlock'

read -d '' OPERATION <<EOF
{
  "S3PutObjectRetention": {
    "Retention": {
      "RetainUntilDate":"2025-01-30T00:00:00",
      "Mode":"GOVERNANCE"
    }
  }
}
EOF

read -d '' MANIFEST <<EOF
{
  "Spec": {
    "Format": "S3BatchOperations_CSV_20180820",
    "Fields": [
      "Bucket",
      "Key"
    ]
  },
  "Location": {
    "ObjectArn": "arn:aws:s3:::amzn-s3-demo-manifest-bucket/governance-objects-manifest.csv",
    "ETag": "Your-manifest-ETag"
  }
}
EOF

read -d '' REPORT <<EOF
{
  "Bucket": "arn:aws:s3:::amzn-s3-demo-completion-report-bucketT",
  "Format": "Report_CSV_20180820",
  "Enabled": true,
  "Prefix": "reports/governance-objects",
  "ReportScope": "AllTasks"
}
EOF

aws \
    s3control create-job \
    --account-id "${ACCOUNT_ID}" \
    --manifest "${MANIFEST//$'\n'}" \
    --operation "${OPERATION//$'\n'/}" \
    --report "${REPORT//$'\n'}" \
    --priority 10 \
    --role-arn "${ROLE_ARN}" \
    --client-request-token "$(uuidgen)" \
    --region "${AWS_DEFAULT_REGION}" \
    --description "Put governance retention";
```

**Example — 跨多个对象绕过保留监管**  
以下示例基于上述创建信任策略以及设置 S3 分批操作和 S3 对象锁定配置权限的示例而构建。它说明如何绕过跨多个对象的保留监管，并创建一个分批操作任务，该任务使用清单存储桶并在报告存储桶中报告结果。  

```
export AWS_PROFILE='aws-user'

read -d '' bypass_governance_permissions <<EOF
{
    "Version": "2012-10-17"		 	 	 ,		 	 	 TCX5-2025-waiver;,
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:BypassGovernanceRetention"
            ],
            "Resource": [
                "arn:aws:s3:::amzn-s3-demo-manifest-bucket/*"
            ]
        }
    ]
}
EOF

aws iam put-role-policy --role-name batch-operations-objectlock --policy-name bypass-governance-permissions --policy-document "${bypass_governance_permissions}"

export AWS_PROFILE='aws-user'
export AWS_DEFAULT_REGION='us-west-2'
export ACCOUNT_ID=123456789012
export ROLE_ARN='arn:aws:iam::123456789012:role/batch_operations-objectlock'

read -d '' OPERATION <<EOF
{
  "S3PutObjectRetention": {
    "BypassGovernanceRetention": true,
    "Retention": {
    }
  }
}
EOF

read -d '' MANIFEST <<EOF
{
  "Spec": {
    "Format": "S3BatchOperations_CSV_20180820",
    "Fields": [
      "Bucket",
      "Key"
    ]
  },
  "Location": {
    "ObjectArn": "arn:aws:s3:::amzn-s3-demo-manifest-bucket/governance-objects-manifest.csv",
    "ETag": "Your-manifest-ETag"
  }
}
EOF

read -d '' REPORT <<EOF
{
  "Bucket": "arn:aws:s3:::amzn-s3-demo-completion-report-bucket",
  "Format": "Report_CSV_20180820",
  "Enabled": true,
  "Prefix": "reports/batch_operations-governance",
  "ReportScope": "AllTasks"
}
EOF

aws \
    s3control create-job \
    --account-id "${ACCOUNT_ID}" \
    --manifest "${MANIFEST//$'\n'}" \
    --operation "${OPERATION//$'\n'/}" \
    --report "${REPORT//$'\n'}" \
    --priority 10 \
    --role-arn "${ROLE_ARN}" \
    --client-request-token "$(uuidgen)" \
    --region "${AWS_DEFAULT_REGION}" \
    --description "Remove governance retention";
```

## 使用适用于 Java 的 Amazon 开发工具包
<a name="batch-ops-examples-java-object-lock-governance"></a>

以下适用于 Java 的 Amazon SDK 示例说明如何跨多个对象应用 S3 对象锁定保留治理（`retain until date` 设置为 2025 年 1 月 30 日），包括跨多个对象应用具有保留截止日期的对象锁定保留治理，以及绕过跨多个对象的保留治理。

有关如何通过适用于 Java 的 Amazon SDK 将批量操作与 S3 对象锁定保留治理模式结合使用的示例，请参阅《Amazon S3 API Reference》**中的 [Use CreateJob with an Amazon SDK or CLI](https://docs.amazonaws.cn/AmazonS3/latest/API/s3-control_example_s3-control_CreateJob_section.html)。