Home > Net >  AttributeError: 'S3' object has no attribute 'Bucket'
AttributeError: 'S3' object has no attribute 'Bucket'

Time:12-10

What I'm doing wrong?

class S3:
    def __init__(self, b: str, r: str = ""):
        self._bucket = b
        self._remote_dir = r
        self._s3 = client("s3")


def get_bootcamp_dumps(self, file_name):
try:
    my_bucket = self._s3.Bucket(self._bucket)
    dumps_list = []

    for object in my_bucket.objects.all():
        dumps_list.append(object.key)
    filtered_list = [i for i in dumps_list if i.startswith(file_name)]
    return filtered_list
except ClientError as error:
    print(error)

and this is how I'm calling it

s3 = S3(b='my_bucket_name')

dump_list=s3.get_bootcamp_dumps('key_word')

and I'm getting this error

AttributeError: 'S3' object has no attribute 'Bucket'

CodePudding user response:

boto3 has two different ways to access Amazon S3. It appears that you are mixing usage between the two of them.

Client Method

Using a client maps 1:1 with an AWS API call. For example:

import boto3

s3_client = boto3.client('s3')

objects = s3_client.list_objects('bucket-name')

for object in objects['Contents']:
  print(object['Key'])

Resource Method

boto3 also provides more convenient 'resource' methods, which are more Pythonic. For example:

import boto3

s3_resource = boto3.resource('s3')

for object in s3_resource.Bucket('bucket-name').objects.all():
  print(object.key)
  • Related