英文:
Copy and Rename files in S3 bucket Lambda
问题
I am writing a lambda function to test the tables in Athena and I have gotten that part to work. I am trying to rename the ResultConfiguration outputlocation file name that the function creates which cannot be renamed unless it's copied and deleted. I am getting Invalid Bucket Name everytime it tries to copy and I'm not sure why. This is the python code I have after doing some research:
def copy_delete_output_results(query_id, table_name):
oldCsvFileLocation = output + query_id + '.csv'
newOutputLocation = output + table_name
newFileName = table_name + '.csv'
s3.Object(newOutputLocation, newFileName).copy_from(CopySource=oldCsvFileLocation)
s3.Object(output, oldCsvFileLocation).delete()
Error Message:
Invalid bucket name "s3://ab-binaries-bucket-devtest/test/testQueryResults/dsm_abc_dns": Bucket name must match the regex "^[a-zA-Z0-9.-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).:(s3|s3-object-lambda):[a-z-0-9]:[0-9]{12}:accesspoint[/:][a-zA-Z0-9-.]{1,63}$|^arn:(aws).*:s3-outposts:[a-z-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9-]{1,63}$".
英文:
I am writing a lambda function to test the tables in Athena and I have gotten that part to work. I am trying to rename the ResultConfiguration outputlocation file name that the function creates which cannot be renamed unless it's copied and deleted. I am getting Invalid Bucket Name everytime it tries to copy and I'm not sure why. This is the python code I have after doing some research:
def copy_delete_output_results(query_id, table_name):
oldCsvFileLocation = output + query_id + '.csv'
newOutputLocation = output + table_name
newFileName = table_name + '.csv'
s3.Object(newOutputLocation,newFileName).copy_from(CopySource=oldCsvFileLocation)
s3.Object(output,oldCsvFileLocation).delete()
oldCsvFileLocation = s3://ab-binaries-bucket-devtest/test/testQueryResults/1d8ab6ce-eda0-435f-93c3-2baa6fec8abd.csv
newOutputLocation = s3://ab-binaries-bucket-devtest/test/testQueryResults/dsm_abc_dns
newFileName = dsm_abc_dns.csv
Error Message:
Invalid bucket name "s3://ab-binaries-bucket-devtest/test/testQueryResults/dsm_abc_dns": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:(s3|s3-object-lambda):[a-z\-0-9]*:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-.]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\-]{1,63}$"
答案1
得分: 4
s3.Object有两个参数,第一个是桶名称(字符串),第二个是文件键(字符串)。
例如:
import boto3
s3 = boto3.resource('s3')
源桶名称 = 'ab-binaries-bucket-devtest'
源文件键 = 'test/testQueryResults/1d8ab6ce-eda0-435f-93c3-2baa6fec8abd.csv'
目标桶名称 = 'ab-binaries-bucket-devtest'
目标文件键 = 'test/testQueryResults/dsm_abc_dns/dsm_abc_dns.csv'
# 复制文件
s3.Object(目标桶名称, 目标文件键).copy_from(
CopySource={'Bucket': 源桶名称, 'Key': 源文件键}
)
# 删除原始文件
s3.Object(源桶名称, 源文件键).delete()
英文:
s3.Object has two parameteres, the first one is the bucket name (string), and the second one is the file key (string).
For example:
import boto3
s3 = boto3.resource('s3')
source_bucket_name = 'ab-binaries-bucket-devtest'
source_file_key = 'test/testQueryResults/1d8ab6ce-eda0-435f-93c3-2baa6fec8abd.csv'
destination_bucket_name = 'ab-binaries-bucket-devtest'
destination_file_key = 'test/testQueryResults/dsm_abc_dns/dsm_abc_dns.csv'
# Copy the file
s3.Object(destination_bucket_name, destination_file_key).copy_from(
CopySource={'Bucket': source_bucket_name, 'Key': source_file_key}
)
# Delete the original file
s3.Object(source_bucket_name, source_file_key).delete()
So, the error you got is because you are passing the full s3 path in the first param to the s3.Object instead of passing the bucket-name
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论