Search code examples
pythonamazon-s3boto3

An exception occurred : 's3.ServiceResource' object has no attribute 'head_object'


I am checking if an object exist in S3 bucket. Following is the code snippet that I am using. obj is the filename.

    s3 = boto3.resource('s3')
    try:
        s3.head_object(Bucket=bucket_name, Key=obj)
    except ClientError as e:
        return False

But it is throwing me exception :

An exception occurred in python_code : 's3.ServiceResource' object has no attribute 'head_object'

Reference I used for this API - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.head_object

Could anyone help me fix this?


Solution

  • Try:

    s3 = boto3.client('s3')

    instead of

    s3 = boto3.resource('s3')