简体   繁体   中英

AWS Lambda Python S3 Read File Error

Trying to read a file from a bucket in s3. The bucket has a trigger wired to a python lambda function. This then runs when a new file is put in the bucket. I keep getting an error.

This the code:

Download the file from S3 to the local filesystem

try:
    s3.meta.client.download_file(bucket, key, localFilename)
except Exception as e:
    print(e)
    print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
    raise e

I get this error

'ClientMeta' object has no attribute 'client'

I am thinking it could be the IAM but the Lambda function has the AWSLambdaFullAccess Role which is pretty much an admin in S3.

Any help would be much appreciated

This error is caused by creating a boto3 client instead of resource .

Example (this will reproduce your error):

s3 = boto3.client('s3')
s3.meta.client.download_file(bucket, key, localFilename)

You have two solutions:

1) Change to use the " high-level abstracted " s3 resource:

s3 = boto3.resource('s3')
s3.meta.client.download_file(bucket, key, localFilename)

2) Directly use the " low-level " s3 client download_file()

s3 = boto3.client('s3')
s3.download_file(bucket, key, localFilename)

Working with Boto3 Low-Level Clients

I think may be refer incorrectly object type for variable s3 . s3.meta.client may be something you use when s3 is ResourceMeta object, but here I think s3 is Client object.

So you can just write

try: s3.download_file(bucket, key, localFilename) except Exception as e: ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM