我有一个存储为字符串的存储桶名称
s3_dest1 = "s3://fbg-hg/AGLUE/MYSQL/QUERY1/"
s3_dest2 = "s3://fbg-hg/AGLUE/MYSQL/QUERY2/"
s3_dest3 = "s3://fbg-hg/AGLUE/MYSQL/QUERY3/"
s3_dest4 = "s3://fbg-hg/AGLUE/MYSQL/QUERY4/"
s3_dest5 = "s3://fbg-hg/AGLUE/MYSQL/QUERY5/"
s3_dest6 = "s3://fbg-hg/AGLUE/MYSQL/QUERY6/"
我想从此s3存储桶下载文件,并以电子邮件结尾作为附件。 此文件夹中只有一个文件,但是要获取此文件,我们需要遍历该文件夹,因为我将不知道文件名。
这是我在做什么,但是这段代码给我错误。
AttributeError:'str'对象没有属性'objects'
这是我的python代码
my_list = [s3_dest1, s3_dest2,s3_dest3,s3_dest4,s3_dest5,s3_dest6]
for s3_dest in my_list:
s3=boto3.client('s3')
for s3_object in s3_dest.objects.all():
filename = os.path.split(s3_object.key)
print(filename)
我是python的新手
答案 0 :(得分:0)
如this答案和that答案中所述,有多种解决方法。在您的情况下,您将目标另存为字符串,但从未对其执行任何操作。您从未将其传递给boto3
。如果我尝试让您的代码正常工作,它将变成类似
# Use the bucket name, not a connection string
my_list = ['fbg-hg/AGLUE/MYSQL/QUERY1', ...]
attachments = []
s3=boto3.client('s3')
for bucket_name in my_list:
bucket = s3.Bucket(bucket_name)
for s3_object in bucket.objects.all():
filename = os.path.split(s3_object.key)
print(filename)
# and then when you want to add it as an attachment
bytes_buffer = io.BytesIO()
client.download_fileobj(Bucket=bucket_name,
Key=object_key,
Fileobj=bytes_buffer)
byte_value = bytes_buffer.getvalue()
attachments.append(byte_value.decode())