Python
Last updated
Last updated
This example demonstrates how to connect to Nebula Block (an S3-compatible service) and perform basic operations like listing buckets, uploading, downloading, and generating a presigned URL using the AWS SDK for Python .
Before running the code, ensure you have the following:
Create an object storage bucket on the platform, then navigate to the corresponding page to obtain the storage access credentials.
Python installed.
boto3 and python-dotenv libraries installed. You can install them using pip:
Create a .env
file with your Nebula Block credentials:
Credentials:
The program uses boto3.client()
to authenticate with the S3-compatible service using the provided access key and secret key. Ensure to replace YOUR_ACCESS_KEY
and YOUR_SECRET_KEY
with your actual credentials.
Client Configuration:
The boto3.client()
is configured with the endpoint and region of the S3-compatible service. The signature_version='s3v4'
ensures the use of S3 v4 signature for secure requests.
Test Connection: Tests if the client can successfully list the buckets to verify the connection.
Create Bucket: The script attempts to create the specified bucket. If the bucket already exists, it will proceed without error.
Upload a File: Uploads a test file from the local system to the specified bucket in Nebula Block storage.
Download a File: Downloads the uploaded test file from Nebula Block storage to the local system to verify successful upload.
List Objects: Lists all objects in the specified bucket to confirm the file was uploaded.
Presigned URL Generation: Generates a temporary URL for downloading the uploaded object without needing further authentication.
Cleanup: Deletes the local temporary test files after completing the demonstration.
To run the program, simply execute the script as a standard Python application:
Make sure to have the boto3
and python-dotenv
libraries installed and your .env
file properly configured with your Nebula Block credentials.
Note: Ensure that you keep your access keys secure. Do not hardcode them in production code. Always use environment variables or secret managers.