如何使用 Amazon S3 Batch Operations 複製大於 5 GB 的物件?
我想將大於 5 GB 的 Amazon Simple Storage Service (Amazon S3) 物件從一個儲存貯體複製到另一個儲存貯體。
簡短描述
若要將大於 5 GB 的 Amazon S3 物件從一個儲存貯體複製到另一個儲存貯體,請將 S3 Batch Operations 與 AWS Lambda 函數搭配使用。如需詳細資訊,請參閱使用 Amazon S3 Batch Operations 複製大於 5 GB 的物件。
**注意:**您也可以使用分段上傳 API 複製大於 5 GB 的 Amazon S3 物件。如需詳細資訊,請參閱使用分段上傳複製物件。
解決方法
在開始之前,請確定您已建立要將物件複製到的目的地 S3 儲存貯體。
建立 Lambda 函數
請完成下列步驟:
-
開啟 Lambda 主控台。
-
選擇建立函數。
-
選擇 Author from scratch (從頭開始撰寫)。
-
在 Function name (函數名稱) 中,輸入您的函數名稱,例如 S3BatchCopy。
-
選擇 Runtime (執行時期) 下拉式清單,然後選擇 Python 3.9。
-
在 Architecture (架構) 中,選擇 x86\ _64,然後選擇 Create function (建立函數)。
-
在內建程式碼編輯器中,輸入 Lambda 函數程式碼。
Lambda 函數程式碼範例:import boto3 import os from urllib import parse from botocore.client import Config from boto3.s3.transfer import TransferConfig target_bucket = os.environ['destination_bucket'] new_prefix = os.environ['destination_bucket_prefix'] metadata_copy = os.environ['copy_metadata'] tagging_copy = os.environ['copy_tagging'] storage_class = os.environ['copy_storage_class'] # Added storage class s3Client = boto3.client('s3', config=Config(retries={'max_attempts': 3})) def lambda_handler(event, context): task = event['tasks'][0] s3Key = parse.unquote_plus(task['s3Key'], encoding='utf-8') s3VersionId = task['s3VersionId'] s3Bucket = task['s3BucketArn'].split(':')[-1] try: copy_source = {'Bucket': s3Bucket, 'Key': s3Key} if s3VersionId: copy_source['VersionId'] = s3VersionId newKey = f"{new_prefix}/{s3Key}" if new_prefix else s3Key myargs = { 'ACL': 'bucket-owner-full-control', 'StorageClass': storage_class # Added storage class to copy arguments } # Add metadata if enabled if metadata_copy == 'Enable': get_metadata = s3Client.head_object(Bucket=s3Bucket, Key=s3Key, VersionId=s3VersionId if s3VersionId else None) for key in ['CacheControl', 'ContentDisposition', 'ContentEncoding', 'ContentLanguage', 'Metadata', 'WebsiteRedirectLocation', 'Expires']: if value := get_metadata.get(key): myargs[key] = value # Add tagging if enabled if tagging_copy == 'Enable': get_obj_tag = s3Client.get_object_tagging(Bucket=s3Bucket, Key=s3Key, VersionId=s3VersionId if s3VersionId else None) if tag_set := get_obj_tag.get('TagSet'): myargs['Tagging'] = "&".join([f"{parse.quote_plus(d['Key'])}={parse.quote_plus(d['Value'])}" for d in tag_set]) response = s3Client.copy(copy_source, target_bucket, newKey, ExtraArgs=myargs) result = {'resultCode': 'Succeeded', 'resultString': str(response)} except Exception as e: result = {'resultCode': 'PermanentFailure', 'resultString': str(e)} return { 'invocationSchemaVersion': event['invocationSchemaVersion'], 'treatMissingKeysAs': 'PermanentFailure', 'invocationId': event['invocationId'], 'results': [{'taskId': task['taskId'], **result}] } -
選擇 Deploy (部署)。
為 Lambda 函數建立 IAM 角色
請完成下列步驟:
-
開啟 AWS Identity and Access Management (IAM) console (AWS Identity and Access Management (IAM) 主控台)。
-
在導覽窗格中,選擇 Roles (角色),然後選擇 Create role (建立角色)。
-
在 Trusted entity type (受信任實體的類型) 中,選擇 AWS service (AWS 服務)。
-
選擇 Use case (使用案例) 下拉式清單,然後選擇 Lambda。
-
選擇 Next (下一步)。
-
選擇 AWSLambdaBasicExecutionRole 政策,然後選擇 Next (下一步)。
-
在 Role name (角色名稱) 中輸入名稱,例如 LambdaS3BatchRole。
-
附加以下政策:
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "s3:GetObject", "s3:GetObjectAcl", "s3:GetObjectTagging", "s3:GetObjectVersion", "s3:GetObjectVersionAcl", "s3:GetObjectVersionTagging", "s3:ListBucket*" ], "Resource": "*", "Effect": "Allow" }, { "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:PutObjectTagging", "s3:PutObjectLegalHold", "s3:PutObjectRetention", "s3:GetBucketObjectLockConfiguration", "s3:ListBucket*", "s3:GetBucketLocation" ], "Resource": [ "arn:aws:s3:::amzn-s3-demo-destination-bucket", "arn:aws:s3:::amzn-s3-demo-destination-bucket/*" ], "Effect": "Allow" } ] } -
選擇 Create role (建立角色)。
**注意:**如果您使用客戶自管的 AWS Key Management Service (AWS KMS) 金鑰來加密儲存貯體,則 IAM 角色必須授予額外的權限。如需詳細資訊,請參閱我的 Amazon S3 儲存貯體使用自訂 AWS KMS key 進行預設加密。如何允許使用者從儲存貯體下載及上傳資料?
建立 Batch Operations 權限政策
請完成下列步驟:
-
開啟 IAM console (IAM 主控台)。
-
在導覽窗格中,選擇 Roles (角色),然後選擇 Create role (建立角色)。
-
在 Trusted entity type (受信任實體的類型) 中,選擇 AWS 服務。
-
選擇 Use case (使用案例) 下拉式清單,然後選擇 S3。
-
選擇 S3 Batch Operations。
-
選擇 Next (下一步)。
-
在 Role name (角色名稱) 中輸入名稱,例如 S3BatchOperationsRole。
-
附加以下政策:
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "s3:GetObject", "s3:GetObjectVersion", "s3:GetBucketLocation" ], "Resource": [ "arn:aws:s3:::amzn-s3-demo-manifest-bucket", "arn:aws:s3:::amzn-s3-demo-manifest-bucket/" ], "Effect": "Allow" }, { "Action": [ "s3:PutObject", "s3:GetBucketLocation" ], "Resource": [ "arn:aws:s3:::amzn-s3-demo-completion-report-bucket", "arn:aws:s3:::amzn-s3-demo-completion-report-bucket/" ], "Effect": "Allow" }, { "Action": [ "lambda:InvokeFunction" ], "Resource": "arn_of_lambda_function_created_in_step1", "Effect": "Allow" } ] } -
選擇 Create role (建立角色)。
建立 Batch Operations 作業
請完成下列步驟:
- 開啟 Amazon S3 console (Amazon S3 主控台)。
- 在導覽窗格中,選擇 Batch Operations,然後選擇 Create job (建立作業)。
- 在 Manifest format (資訊清單格式) 中,選取 S3 inventory report (S3 庫存清單報告) 或 CSV。
- 在 Manifest object (資訊清單物件) 中,選擇 Browse S3 (瀏覽 S3)。
- 選取要從中複製物件的來源儲存貯體。
- 選取來源儲存貯體中的物件,然後選擇 Choose path (選擇路徑)。
- 選擇 Next (下一步)。
- 在 Choose operation (選擇作業) 中,選擇 Copy (複製)。
- 在 Copy destination (複製目的地) 中,選擇 Browse S3 (瀏覽 S3)。
- 選取要將物件複製到的目的地儲存貯體,然後選擇 Choose path (選擇路徑)。
- 選取I acknowledge that existing objects with the same name will be overwritten (我確認將覆寫具有相同名稱的現有物件),然後選擇 Next (下一步)。
- 在 Permissions (權限) 中,選擇 IAM 角色 下拉式清單,然後選取您的 S3BatchOperationsRole IAM 角色。
- 選擇 Next (下一步),然後選擇 Create job (建立作業)。
跨帳戶存取權
如果目的地 S3 儲存貯體位於另一個 AWS 帳戶中,則您必須將資源型政策附加到該儲存貯體。
資源型政策範例:
{ "Version": "2012-10-17", "Id": "Policy1541018284691", "Statement": [ { "Sid": "Allow Cross Account Copy", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::SourceAccountNumber:role/LambdaS3BatchRole" }, "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:PutObjectTagging" ], "Resource": "arn:aws:s3:::DESTINATION_BUCKET/*" } ] }
如需更多資訊,請參閱如何從其他 AWS 帳戶複製 Amazon S3 物件?
相關資訊
如何對 Amazon S3 Batch Operations 問題進行疑難排解?
- 語言
- 中文 (繁體)

相關內容
已提問 2 年前
已提問 1 年前