Boto3 is the library for interacting with AWS via Python.
https://boto3.amazonaws.com/v1/documentation/api/latest/index.html
Need to first install:
pip3 install boto3
Lots of examples are out there but they are not yours unless you get it to work for you.
So to get started you need an AWS account, and credentials be setup in your local machine.
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
So I have a couple files on S3 in a bucket, and I can retrieve them.
Sometimes boto3 is better than other languages because sometimes other languages such as Java may be more clumsy.
import boto3
from botocore.exceptions import ClientError
def getContentInBucket(s3_client, bucketName):
result = s3_client.list_objects(Bucket = bucketName, Prefix='')
for o in result.get('Contents'):
print("fileName: "+o.get('Key'))
data = s3_client.get_object(Bucket=bucketName, Key=o.get('Key'))
contents = data['Body'].read()
print(contents.decode("utf-8"))
# To get list of buckets present in AWS using S3 client
def get_buckets_client(s3_client):
try:
response = s3_client.list_buckets()
for bucket in response['Buckets']:
bucketName = bucket["Name"]
print("bucketName: "+bucketName)
getContentInBucket(s3_client, bucketName)
except ClientError:
print("Couldn't get buckets.")
raise
session = boto3.session.Session()
# User can pass customized access key, secret_key and token as well
# credentials are set in my .aws/credentials file
# See https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
s3_client = session.client('s3')
get_buckets_client(s3_client);