0

I have a bucket name stored as string

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/"

I want to download the files from this s3 bucket and end an email as an attachment. There will be only one file in this folder but to get this file we need to iterate over the folder because i will not be knowing the name of the file.

This is what i am doing but this code gives me error.

AttributeError: 'str' object has no attribute 'objects'

Here is my python code

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)

I am new to python

Atharv Thakur
  • 671
  • 3
  • 21
  • 39

1 Answers1

0

As is described in this answer and that answer, there are multiple ways to solve this. In your case, you saved the destination as a string, but never did anything with it. You never passed it to boto3. If I try to get your code to work, it becomes something like

# 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())
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
  • it says AttributeError: 'S3' object has no attribute 'Bucket' – Atharv Thakur Jul 25 '20 at 10:24
  • The answer I referred to probably used an old version of `boto3`. In any case, you can use `s3.list_objects()` from (their docs)[https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.list_objects] instead – Ruben Helsloot Jul 25 '20 at 16:02