我是 python 新手。我有一個事件觸發的 AWS Lambda 函式,它將檔案從 S3 存盤桶復制到另一個 S3 存盤桶。我要復制檔案的目標 S3 路徑是:“dest_bucket/folder1/test”。當我嘗試運行它時,它給了我這個錯誤:
無效的存盤桶名稱“dest_bucket/folder1/test”:存盤桶名稱必須匹配正則運算式“^[a-zA-Z0-9.-_]{1,255}$”或者是匹配正則運算式“^arn:(aws) 的 ARN . :(s3|s3-object-lambda):[az-0-9] :[0-9]{12}:accesspoint[/:][a-zA-Z0-9-.]{1,63} $|^arn:(aws).*:s3-outposts:[az-0-9] :[0-9]{12}:outpost[/:][a-zA-Z0-9-]{1 ,63}[/:]接入點[/:][a-zA-Z0-9-]{1,63}$"
源存盤桶沒有任何檔案夾結構。目標存盤桶具有檔案夾結構,需要將檔案復制到“dest_bucket/folder1/test”下。錯誤發生在 lambda 函式中:“destination_bucket_name = 'dest_bucket/folder1/test”。因為,如果我只寫沒有斜杠的目標存盤桶名稱,它就可以作業!知道我應該怎么寫嗎?
import json
import boto3
import os
import uuid
def lambda_handler(event, context):
try:
client = boto3.client('sts')
response = client.assume_role(RoleArn='arn:aws:iam::xxx:role/xxx_lambda_role',RoleSessionName="cross_acct_lambda")
session = boto3.Session(aws_access_key_id=response['Credentials']['AccessKeyId'],aws_secret_access_key=response['Credentials']['SecretAccessKey'],aws_session_token=response['Credentials']['SessionToken'])
print(session)
print("successfully assumed role")
s3_client = boto3.client("s3", aws_access_key_id=response['Credentials']['AccessKeyId'],aws_secret_access_key=response['Credentials']['SecretAccessKey'],aws_session_token=response['Credentials']['SessionToken'])
#s3_client = boto3.client("s3")
#base = read from parameter store
#table_partion = read from file
destination_bucket_name = 'dest_bucket/folder1/test'
# event contains all information about uploaded object
print("Event :", event)
# Bucket Name where file was uploaded
source_bucket_name = event['Records'][0]['s3']['bucket']['name']
print(source_bucket_name)
# Filename of object (with path)
file_key_name = event['Records'][0]['s3']['object']['key']
#file_key_name = 'empty_test.txt'
print(file_key_name)
# Copy Source Object
copy_source_object = {'Bucket': source_bucket_name, 'Key': file_key_name}
print(copy_source_object)
# S3 copy object operation
s3_client.copy_object(CopySource=copy_source_object, Bucket=destination_bucket_name, Key=file_key_name)
return {
'statusCode': 200,
'body': json.dumps('S3 events Lambda!')
}
except Exception as e:
print(e)
raise e
uj5u.com熱心網友回復:
從檔案:
存盤桶名稱的長度可以在 3 到 63 個字符之間,并且只能包含小寫字符、數字、句點和短劃線。
存盤桶名稱中的每個標簽都必須以小寫字母或數字開頭。
存盤桶名稱不能包含下劃線、以破折號結尾、具有連續句點或在句點旁邊使用破折號。
存盤桶名稱不能格式化為 IP 地址 (198.51.100.24)。
https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-s3-bucket-naming-requirements.html
uj5u.com熱心網友回復:
確保您只使用存盤桶名稱作為存盤桶名稱:) 至于“路徑”,它在 S3 中確實是假的 - 唯一真實的是物件鍵。斜線只是該名稱中的字符,它們沒有特殊含義。
uj5u.com熱心網友回復:
您可以使用:
destination_bucket = 'dest_bucket'
destination_path = 'folder1/test/'
for record in event['Records']:
source_bucket = record['s3']['bucket']['name']
source_key = record['s3']['object']['key']
copy_source_object = {'Bucket': source_bucket, 'Key': source_key}
destination_key = destination_path source_key
s3_client.copy_object(CopySource=copy_source_object, Bucket=destination_bucket, Key=destination_key)
這將遍歷事件中的所有傳入記錄。
然后它將創建一個名稱folder1/test/ 源物件的名稱(鍵)的物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/430416.html
