

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

# 使用具有 S3 物件鎖定保留控管模式的 S3 批次操作
<a name="batch-ops-governance-mode"></a>

下列範例以先前建立信任政策的範例，以及設定 S3 Batch Operations 和 S3 物件鎖定組態許可的範例為基礎。此範例示範如何對多個物件套用 S3 物件鎖定保留控管，並將`retain until date`設定為 2025 年 1 月 30 日。它會建立使用資訊清單儲存貯體的批次操作任務，並在報告儲存貯體中報告結果。

若要使用下列範例，請以您自己的資訊取代 {{`user input placeholders`}}。

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

下列 AWS 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-bucket}}T",
  "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 Batch Operations 和 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 的 AWS 開發套件
<a name="batch-ops-examples-java-object-lock-governance"></a>

下列適用於 Java 的 AWS SDK 範例示範如何將 S3 物件鎖定保留控管與 `retain until date` 設定為 2025 年 1 月 30 日跨多個物件套用，包括將物件鎖定保留控管套用至具有保留截止日期的多個物件，以及略過跨多個物件的保留控管。

如需如何搭配適用於 Java 的 AWS SDK 使用批次操作與 S3 物件鎖定保留控管模式的範例，請參閱《*Amazon S3 API 參考*》中的[搭配 AWS SDK 或 CLI 使用 CreateJob](https://docs.aws.amazon.com/AmazonS3/latest/API/s3-control_example_s3-control_CreateJob_section.html)。