AWS SDK for Python (boto3) quickstart
boto3 is the AWS SDK for Python. It works with Filebase out of the box — set the endpoint and the region, and every operation behaves the same as it does against AWS S3.
Install
pip install boto3
Configure
filebase_client.py
import os
import boto3
from botocore.config import Config
s3 = boto3.client(
's3',
endpoint_url='https://s3.filebase.io',
region_name='auto',
aws_access_key_id=os.environ['FILEBASE_KEY'],
aws_secret_access_key=os.environ['FILEBASE_SECRET'],
config=Config(signature_version='s3v4'),
)
List buckets
for bucket in s3.list_buckets()['Buckets']:
print(bucket['Name'])
Create a bucket
s3.create_bucket(Bucket='quickstart-demo')
Upload an object
with open('hello.txt', 'rb') as f:
s3.put_object(
Bucket='quickstart-demo',
Key='hello.txt',
Body=f,
ContentType='text/plain',
)
For large files, use the high-level transfer API:
s3.upload_file(
Filename='./large-file.bin',
Bucket='quickstart-demo',
Key='large-file.bin',
)
upload_file automatically uses multipart upload for files over 8 MB.
List objects
response = s3.list_objects_v2(Bucket='quickstart-demo', MaxKeys=100)
for obj in response.get('Contents', []):
print(obj['Key'], obj['Size'])
Download an object
s3.download_file(
Bucket='quickstart-demo',
Key='hello.txt',
Filename='downloaded.txt',
)
Or stream the body:
response = s3.get_object(Bucket='quickstart-demo', Key='hello.txt')
print(response['Body'].read().decode())
Generate a pre-signed URL
url = s3.generate_presigned_url(
'get_object',
Params={'Bucket': 'quickstart-demo', 'Key': 'hello.txt'},
ExpiresIn=3600,
)
print(url)
Delete an object
s3.delete_object(Bucket='quickstart-demo', Key='hello.txt')
Using the resource API
If you prefer the higher-level resource API:
import boto3
from botocore.config import Config
resource = boto3.resource(
's3',
endpoint_url='https://s3.filebase.io',
region_name='auto',
aws_access_key_id=os.environ['FILEBASE_KEY'],
aws_secret_access_key=os.environ['FILEBASE_SECRET'],
config=Config(signature_version='s3v4'),
)
bucket = resource.Bucket('quickstart-demo')
for obj in bucket.objects.all():
print(obj.key, obj.size)