I want to list the objects of a bucket. I used the following code in boto and it worked fine
from boto.s3.connection import S3Connection
from boto.s3.connection import OrdinaryCallingFormat
from boto.s3.key import Key
destination = S3Connection(aws_access_key_id=aws_access_key_id, aws_secret_access_key='lokesh', is_secure=False, port=9090, host=host, calling_format=OrdinaryCallingFormat())
destination_bucket = destination.get_bucket(bucket_name)
destination_all_keys = destination_bucket.get_all_keys()
print destination_all_keys
for key in destination_all_keys:
print key
However if I am using boto3, then I am getting error
botocore.exceptions.ClientError: An error occurred (MethodNotAllowed) when calling the ListObjects operation: The specified method is not allowed against this resource.
below is the boto3 code that gives error
import boto3
destination = boto3.client('s3',
endpoint_url=endpoint,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
use_ssl=False,
config=timeout_config
)
destination.list_objects(Bucket=bucket_name)['Contents']
The correct way to do this is in boto3 is using boto3 session .
import boto3
sess = boto3.session(
endpoint_url=endpoint,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
use_ssl=False,
config=timeout_config)
s3 = sess.client("s3")
s3.list_objects(Bucket=bucket_name)
In addition, passing API key is a bad design. If you deploy application into any AWS services that use IAM roles, the accesskey lines will give you headache.
Just setup your local .aws credential file and you can use simple setup such as this.
import boto3
sess = boto3.session(
endpoint_url=endpoint,
use_ssl=False,
config=timeout_config)
s3 = sess.client("s3")
s3.list_objects(Bucket=bucket_name)