S3 客户端没有名为 Bucket 的属性

S3 Client has no attribute called Bucket

我正在尝试从我的 s3 存储桶下载文件夹。我想使用 s3.client 因为我在代码中进一步使用了客户端方法,但我无法使用客户端方法访问存储桶。当我使用“s3Client.Bucket(bucketName)”时,我收到一条错误消息,指出它没有属性 Bucket。当我使用“s3Client.get_object(Bucket=bucketName, Key= ?)”时,首先它说需要密钥,应该是什么密钥,它是我要下载的文件夹吗?请让我知道我在这里做错了什么。谢谢。

awsParams = {
    "bucket_name": "asgard-icr-model",
    "region_name": "ap-south-1"
}

def get_s3_client():

    s3Client = boto3.client('s3')
    return s3Client

def download_from_s3(srcDir, dstDir):

    try:

        bucketName = awsParams['bucket_name'] #s3 bucket name

        s3Client = get_s3_client()
        bucket = s3Client.Bucket(bucketName) # I get error saying - client has no attribute Bucket.
        bucket = s3Client.get_object(Bucket=bucketName, Key= ?) # If I use this line instead of previous, what should be my key here? 

所以现在如果我做这个改变,我应该用什么来代替 s3.resource 中的 list_objects_v2() 因为没有这个名字的属性?

def get_s3_object():

    s3Obj = boto3.resource("s3",region_name=awsParams['region_name'])
    
    return s3Obj

def download_from_s3(srcDir, dstDir):

    try:

        bucketName = awsParams['bucket_name'] #s3 bucket name
        
        # s3Client = get_s3_client()
        # bucket = s3Client.Bucket(bucketName)
        # bucket = s3Client.get_object(Bucket=bucketName, Key=)
        s3Obj = get_s3_object()
        bucket = s3Obj.Bucket(bucketName)


        keys = []
        dirs = []
        next_token = ''

        base_kwargs = {
            'Bucket':bucket,
            'srcDir':srcDir,
        }
        
        while next_token is not None:
            kwargs = base_kwargs.copy()
            if next_token != '':
                kwargs.update({'ContinuationToken': next_token})
            **results = s3Client.list_objects_v2(**kwargs)**
            contents = results.get('Contents')
            for i in contents:
                k = i.get('Key')
                if k[-1] != '/':
                    keys.append(k)
                else:
                    dirs.append(k)
            next_token = results.get('NextContinuationToken')
        for d in dirs:
            dest_pathname = os.path.join(local, d)
            if not os.path.exists(os.path.dirname(dest_pathname)):
                os.makedirs(os.path.dirname(dest_pathname))
        for k in keys:
            dest_pathname = os.path.join(local, k)
            if not os.path.exists(os.path.dirname(dest_pathname)):
                os.makedirs(os.path.dirname(dest_pathname))
            s3Client.download_file(bucket, k, dest_pathname)
            
    except Exception as e:
        raise

你应该是using一个resource,而不是client:

s3Resource = boto3.resource('s3')
return s3Resource

到此结束

bucket = s3Resource.Bucket(bucketName)

使用 client 时,您可以获得对象列表:

s3_client = boto3.client('s3')

results = s3_client.list_objects_v2(Bucket=...)

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

当使用 resource 时,您可以使用:

s3_resource = boto3.resource('s3')

bucket = s3_resource.Bucket('Bucketname')

for object in bucket.objects.all():
    print(object.key)