复制并重命名S3存储桶中的文件Lambda

huangapple go评论112阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月7日 03:10:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定