AWS SDK - Python

Learn how to use the AWS SDK for Python with Filebase.

What is AWS SDK - Python?

AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - Python (boto3).

Prerequisites:

This guide was written using Ubuntu 20.04 and tested using the command line interface. If you are using an Integrated Development Environment (IDE), the installation of dependencies and modules will vary.

List all Buckets

The following code example creates a new Filebase bucket. Replace the following values in the code to match your configuration:

  • filebase-access-key: Your Filebase Access Key

  • filebase-secret-key: Your Filebase Secret Key

import boto3

s3 = boto3.client('s3',
	endpoint_url='https://s3.filebase.com',
	aws_access_key_id="filebase-access-key",
	aws_secret_access_key="filebase-secret-key")

response = s3.list_buckets()

print('Existing buckets:')
for bucket in response['Buckets']:
    print(f'  {bucket["Name"]}')

Create a Bucket

The following code example creates a new Filebase bucket. Replace the following values in the code to match your configuration:

  • filebase-access-key: Your Filebase Access Key

  • filebase-secret-key: Your Filebase Secret Key

  • new-filebase-bucket: The Intended New Bucket Name

Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes.

Buckets created through this method will be automatically created on the IPFS network.

import boto3

s3 = boto3.client('s3',
	endpoint_url='https://s3.filebase.com',
	aws_access_key_id="filebase-access-key",
	aws_secret_access_key="filebase-secret-key")

bucket_name = "new-filebase-bucket"

s3.create_bucket(Bucket=bucket_name)

Upload an Object

The following code example uploads an object to the specified bucket. Replace the following values in the code to match your configuration:

  • filebase-access-key: Your Filebase Access Key

  • filebase-secret-key: Your Filebase Secret Key

  • filebase-bucket-name: The Filebase Bucket Name

  • object-name: The desired object name once uploaded

import boto3

s3 = boto3.client('s3',
	endpoint_url='https://s3.filebase.com',
	aws_access_key_id="filebase-access-key",
	aws_secret_access_key="filebase-secret-key")
	
data = open('image.png', 'rb')

bucket_name = "filebase-bucket-name"
key_name = "image.png"
s3.put_object(Body=data, Bucket=bucket_name, Key=key_name)

Download an Object

The following code example downloads an object from the specified bucket. Replace the following values in the code to match your configuration:

  • filebase-access-key: Your Filebase Access Key

  • filebase-secret-key: Your Filebase Secret Key

  • filebase-bucket-name: The Filebase Bucket Name

  • object-name: The object to be downloaded

import boto3

s3 = boto3.client('s3',
	endpoint_url='https://s3.filebase.com',
	aws_access_key_id="filebase-access-key",
	aws_secret_access_key="filebase-secret-key")

bucket_name = "filebase-bucket-name"
key_name = "object-name"

s3.get_object(Bucket=bucket_name, Key=key_name)

Delete an Object

The following code example deletes an object from the specified bucket. Replace the following values in the code to match your configuration:

  • filebase-access-key: Your Filebase Access Key

  • filebase-secret-key: Your Filebase Secret Key

  • filebase-bucket-name: The Filebase Bucket Name

  • object-name: The object to be downloaded

import boto3

s3 = boto3.client('s3',
	endpoint_url='https://s3.filebase.com',
	aws_access_key_id="filebase-access-key",
	aws_secret_access_key="filebase-secret-key")

bucket_name = "filebase-bucket-name"

key_name = "object-name"

s3.delete_object(Bucket=bucket_name, Key=key_name)

For more information on the AWS SDK for Python, check out the documentation here.

Last updated