Python

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 boto3.

Prerequisites

Before running the code, ensure you have the following:

  1. Create an object storage bucket on the Nebula Block platform, then navigate to the corresponding page to obtain the storage access credentials.1.1

  2. Python installed.

  3. boto3 and python-dotenv libraries installed. You can install them using pip:

    pip install boto3 python-dotenv
  4. Create a .env file with your Nebula Block credentials:

    NEBULA_ACCESS_KEY=YOUR_ACCESS_KEY  #Use the Access Key from the Details page.
    NEBULA_SECRET_KEY=YOUR_SECRET_KEY  #Use the Secret Key from the Details page.
    NEBULA_ENDPOINT=YOUR_ENDPOINT_URL  #Use the Hostname from the Details page.
    NEBULA_REGION=YOUR_REGION          #Optional, default None.
    NEBULA_BUCKET=YOUR_BUCKET_NAME

Python Code

Common Usage Examples

Create/Delete Bucket Demo

Upload/Download File Demo

Explanation

Steps in the Code

  1. 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.

  2. 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.

  3. Test Connection: Tests if the client can successfully list the buckets to verify the connection.

  4. Create Bucket: The script attempts to create the specified bucket. If the bucket already exists, it will proceed without error.

  5. Upload a File: Uploads a test file from the local system to the specified bucket in Nebula Block storage.

  6. Download a File: Downloads the uploaded test file from Nebula Block storage to the local system to verify successful upload.

  7. List Objects: Lists all objects in the specified bucket to confirm the file was uploaded.

  8. Presigned URL Generation: Generates a temporary URL for downloading the uploaded object without needing further authentication.

  9. Cleanup: Deletes the local temporary test files after completing the demonstration.

Run the Code

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.

Last updated