AWS Boto3

Aws credentials must be configured, Quickstart.

pip install boto3
import boto3
s3 = boto3.client('s3')

S3

  • boto3.client("s3") for low level

  • boto3.resource('s3') for higher level stuff

def all_objects(filter="mp4", bucket_name="interdimensional-news"):
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(bucket_name)
    all_urls = []
    for obj in bucket.objects.all():
        if obj.key.endswith(filter):
           all_urls.append(construct_url(bucket_name, obj.key))

    return all_urls

def get_object(object_key="space_whales.mp3", file_name="test33.mp3", bucket_name="interdimensional-news"):
    client = boto3.client('s3')

    resp = client.get_object(Bucket=bucket_name, Key=object_key)
    all_binary = resp["Body"].read()

    with open(file_name, "wb") as f:
        f.write(all_binary)

def put_content(body, content_type="video/mp4", object_key="default/test3.mp4", bucket_name="interdimensional-news"):
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(bucket_name)
    bucket.put_object(Key=object_key, Body=body, ContentType=content_type)
    print(f"Successfully uploaded {object_key} to S3")

def construct_url(bucket_name, object_key):
    url = "https://%s.s3.amazonaws.com/%s" % (bucket_name, object_key)
    return url

List

Create

Upload

Presigned URL

Allow clients to access the resource

Download

Last updated