sObject - Python Examples
Publisher: Psychz Networks, March 21,2025- Prerequisites
- Creating a Connection
- Listing Buckets
- Creating Buckets
- Listing Bucket's Content
- Deleting Bucket
- Creating Object
- Uploading Object
- Downloading Object
- Deleting Object
- Changing Object's ACL
- Generating Object Download URL
- Conclusion
Integrating sObject with Python
sObject provides an S3-compatible API that allows you to interact with your object storage. This article walks you through setting up a connection and executing a variety of operations using Python and boto3.
Prerequisites
- Python installed on your system
- boto3 installed (install via pip install boto3) (
# pip install boto3
) - sObject endpoint, access key, and secret key
Creating a Connection
To start, import boto3 and create both a client and a resource object. Replace the placeholder values with sObject endpoint, access key, and secret key.
import boto3
from botocore.config import Config
# sObject endpoint URL (e.g., "http://sobject.psychz.net:8000")
endpoint_url = "http://sobject.psychz.net:port"
# Your sObject access credentials
access_key = "your-access-key"
secret_key = "your-secret-key"
# Create an S3 client for low-level operations
s3_client = boto3.client(
's3',
endpoint_url=endpoint_url,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key
config=Config(signature_version='s3'),
)
# Create an S3 resource for higher-level object-oriented operations
s3_resource = boto3.resource(
's3',
endpoint_url=endpoint_url,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key
)
Listing Owned Buckets
You can list all buckets available to your account using either the client or resource interface.
Using the client:
response = s3_client.list_buckets()
for bucket in response['Buckets']:
print(bucket['Name'])
Using the resource:
for bucket in s3_resource.buckets.all():
print(bucket.name)
Creating a Bucket
To create a new bucket, choose a unique bucket name. Note that bucket naming conventions must be followed.
bucket_name = "my-new-bucket"
# Using client
s3_client.create_bucket(Bucket=bucket_name)
# Alternatively, using resource:
# bucket = s3_resource.create_bucket(Bucket=bucket_name)
Listing a Bucket's Content
List all objects (files) stored within a specific bucket.
bucket = s3_resource.Bucket(bucket_name)
for obj in bucket.objects.all():
print(obj.key)
Deleting a Bucket
Before deleting a bucket, ensure it is empty. The following code snippet first deletes all objects in the bucket and then removes the bucket.
bucket = s3_resource.Bucket(bucket_name)
# Delete all objects in the bucket
for obj in bucket.objects.all():
obj.delete()
# Delete the bucket
s3_client.delete_bucket(Bucket=bucket_name)
Creating an Object
Creating an object can be as simple as using the put_object method to store data directly.
object_key = "sample.txt"
data = "This is a sample text file."
s3_client.put_object(Bucket=bucket_name, Key=object_key, Body=data)
Uploading an Object
To upload a local file to your bucket, use the upload_file method.
local_file = "path/to/your/local/file.txt"
object_key = "uploaded-file.txt"
s3_client.upload_file(local_file, bucket_name, object_key)
Downloading an Object
Download an object from your bucket to a local file using the download_file method.
download_path = "path/to/downloaded/file.txt"
s3_client.download_file(bucket_name, object_key, download_path)
Deleting an Object
Remove an object from your bucket with the delete_object method.
s3_client.delete_object(Bucket=bucket_name, Key=object_key)
Changing an Object's ACL
You can modify an object’s access control list (ACL). For example, to make an object publicly readable:
s3_client.put_object_acl(Bucket=bucket_name, Key=object_key, ACL='public-read')
Generating an Object Download URL
Signed URL (Temporary Access)
Generate a time-limited signed URL that allows secure access to a private object:
signed_url = s3_client.generate_presigned_url(
'get_object',
Params={'Bucket': bucket_name, 'Key': object_key},
ExpiresIn=3600 # URL expires in 1 hour
)
print("Signed URL:", signed_url)
Unsigned URL (For Public Objects)
If your object is publicly accessible, you can construct an unsigned URL manually:
unsigned_url = f"{endpoint_url}/{bucket_name}/{object_key}"
print("Unsigned URL:", unsigned_url)
Conclusion
By following these examples, you can easily integrate sObject storage into your Python applications. Adjust the code samples as needed to suit your environment and security policies. For more detailed information, refer to the boto3 documentation.