本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 S3 Batch Operations 關閉 S3 物件鎖定法務保存
下列範例以先前建立信任原則的範例,以及設定 S3 批次操作和 S3 物件鎖定組態許可的範例為基礎。此範例示範如何使用 Batch Operations 停用物件的物件鎖定法務保存。
此範例會先更新角色以授予 s3:PutObjectLegalHold
許可、建立會關閉 (移除) 從資訊清單中識別之物件的法務保存的批次操作任務,然後報告該任務。
若要使用下列範例,請以您自己的資訊取代
。user input
placeholders
下列 AWS CLI 範例示範如何使用批次操作來關閉多個物件的 S3 物件鎖定法務保存。
範例 — 更新角色以授予 s3:PutObjectLegalHold
許可
export AWS_PROFILE='
aws-user
' read -d ''legal_hold_permissions
<<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:PutObjectLegalHold" ], "Resource": [ "arn:aws:s3:::amzn-s3-demo-manifest-bucket
/*" ] } ] EOF aws iam put-role-policy --role-namebatch_operations-objectlock
--policy-namelegal-hold-permissions
--policy-document "${legal_hold_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 { "S3PutObjectLegalHold": { "LegalHold": { "Status":"OFF" } } } EOF read -d ''MANIFEST
<<EOF { "Spec": { "Format": "S3BatchOperations_CSV_20180820", "Fields": [ "Bucket", "Key" ] }, "Location": { "ObjectArn": "arn:aws:s3:::", "ETag": "
amzn-s3-demo-manifest-bucket
/legalhold-object-manifest.csvYour-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/legalhold-objects-batch_operations
", "ReportScope": "AllTasks" } EOF aws \ s3control create-job \ --account-id "${ACCOUNT_ID
}" \ --manifest "${MANIFEST
//$'\n'}" \ --operation "${OPERATION
//$'\n'/}" \ --report "${REPORT
//$'\n'}" \ --priority10
\ --role-arn "${ROLE_ARN
}" \ --client-request-token "$(uuidgen)" \ --region "${AWS_DEFAULT_REGION
}" \ --description "Turn off legal hold
";
下列 適用於 Java 的 AWS SDK 範例示範如何使用批次操作來關閉多個物件的 S3 物件鎖定法務保存。
範例 — 更新角色以授予 s3:PutObjectLegalHold
許可
public void allowPutObjectLegalHold() { final String roleName = "
batch_operations-object-lock
"; final String legalHoldPermissions = "{" + " \"Version\": \"2012-10-17\"," + " \"Statement\": [" + " {" + " \"Effect\": \"Allow\"," + " \"Action\": [" + " \"s3:PutObjectLegalHold\"" + " ]," + " \"Resource\": [" + " \"arn:aws:s3:::amzn-s3-demo-manifest-bucket
/*\"" + " ]" + " }" + " ]" + "}"; final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient(); final PutRolePolicyRequest putRolePolicyRequest = new PutRolePolicyRequest() .withPolicyDocument(legalHoldPermissions) .withPolicyName("legal-hold-permissions
") .withRoleName(roleName); final PutRolePolicyResult putRolePolicyResult = iam.putRolePolicy(putRolePolicyRequest); }
範例 — 關閉法務保存
如果您想要關閉法務保存,請使用以下範例。
public String createLegalHoldOffJob(final AWSS3ControlClient awss3ControlClient) { final String manifestObjectArn = "arn:aws:s3:::
"; final String manifestObjectVersionId = "
amzn-s3-demo-manifest-bucket
/legalhold-object-manifest.csv15ad5ba069e6bbc465c77bf83d541385
"; final JobManifestLocation manifestLocation = new JobManifestLocation() .withObjectArn(manifestObjectArn) .withETag(manifestObjectVersionId); final JobManifestSpec manifestSpec = new JobManifestSpec() .withFormat(JobManifestFormat.S3BatchOperations_CSV_20180820) .withFields("Bucket", "Key"); final JobManifest manifestToPublicApi = new JobManifest() .withLocation(manifestLocation) .withSpec(manifestSpec); final String jobReportBucketArn = "arn:aws:s3:::amzn-s3-demo-completion-report-bucket
"; final String jobReportPrefix = "reports/legalhold-objects-batch_operations
"; final JobReport jobReport = new JobReport() .withEnabled(true) .withReportScope(JobReportScope.AllTasks) .withBucket(jobReportBucketArn) .withPrefix(jobReportPrefix) .withFormat(JobReportFormat.Report_CSV_20180820); final JobOperation jobOperation = new JobOperation() .withS3PutObjectLegalHold(new S3SetObjectLegalHoldOperation() .withLegalHold(new S3ObjectLockLegalHold() .withStatus(S3ObjectLockLegalHoldStatus.OFF))); final String roleArn = "arn:aws:iam::123456789012
:role/batch_operations-object-lock
"; final Boolean requiresConfirmation = true; final int priority =10
; final CreateJobRequest request = new CreateJobRequest() .withAccountId("123456789012
") .withDescription("Turn off legal hold
") .withManifest(manifestToPublicApi) .withOperation(jobOperation) .withPriority(priority) .withRoleArn(roleArn) .withReport(jobReport) .withConfirmationRequired(requiresConfirmation); final CreateJobResult result = awss3ControlClient.createJob(request); return result.getJobId(); }