Skip to main content

CloudSpark is a powerful Python package designed to simplify the management of AWS S3 and Lambda services. Whether you're working on the frontend or backend, CloudSpark provides an intuitive interface to generate presigned URLs and handle file uploads seamlessly.

Project description

CloudSpark

cloudspark is a Python package that provides a convenient way to manage AWS S3 buckets and generate presigned URLs using boto3. It supports bucket creation, CORS management, policy setting, and presigned URL generation.

Features

  • S3 Management: Effortlessly manage your S3 buckets, objects, and file uploads with built-in methods.

  • Presigned URL Generation: Generate secure presigned URLs for your S3 objects, enabling users to upload files directly from the frontend without exposing your credentials.

  • Seamless Integration: Designed to work smoothly with both frontend and backend applications, making file uploads and cloud function management more accessible.

This repository contains several components. For more details on each component, refer to the respective README files.

Installation

You can install cloudspark via pip. Make sure you have boto3 installed as well.

pip install cloudspark 

Usage

Importing the Library

from cloudspark import S3Connection

Initializing the Connection

Create an instance of S3Connection by providing your AWS credentials and region name

s3_conn = S3Connection(access_key='YOUR_ACCESS_KEY',

                       secret_access_key='YOUR_SECRET_ACCESS_KEY',

                       region_name='YOUR_REGION_NAME')

Connecting to a Bucket

Establish a connection to an S3 bucket:

s3_bucketclient = s3_conn.connect(bucket_name='your-bucket-name')

returns an S3 client instance.

Returns the current S3 client instance

s3_bucketclient = s3_conn.get_instance()

returns an S3 client instance.

Creating a bucket

Creates an S3 bucket with the provided name.

s3_client = s3_conn.connect()

s3_bucketclient = s3_conn.create_s3bucket(bucket_name='new-bucket-name')

Set Bucket CORS

Set the CORS configuration for the connected bucket

s3_conn.set_bucket_cors()



# CORSRules: A list of dictionaries containing CORS rules.

# If None, a default CORS rule allowing all origins and methods is applied.

cors_rules = [

    {

        'AllowedHeaders': ['*'],

        'AllowedMethods': ['GET', 'POST'],

        'AllowedOrigins': ['*'],

        'ExposeHeaders': [],

        'MaxAgeSeconds': 3000

    }

]



s3_conn.set_bucket_cors(CORSRules=cors_rules)

Get Bucket CORS

Retrieve the CORS configuration for the connected bucket

cors_config = s3_conn.get_bucket_cors()

Delete Bucket CORS

Delete the CORS configuration from the connected bucket

s3_conn.delete_bucket_cors()

Set Bucket Policy

Set or update the bucket policy for the connected bucket

s3_conn.set_bucket_policy()



# A JSON string or dictionary representing the bucket policy. 

# If None, a default public read policy is applied.



policy = {

    "Version": "2012-10-17",

    "Statement": [

        {

            "Sid": "PublicReadGetObject",

            "Effect": "Allow",

            "Principal": "*",

            "Action": "s3:GetObject",

            "Resource": f"arn:aws:s3:::{bucket_name}/*"

        }

    ]

}

s3_conn.set_bucket_policy(bucket_policy=policy)

Retrieves Bucket Policy

Retrieves the bucket policy for the connected S3 bucket

policy = s3_conn.get_bucket_policy()

Delete Bucket Policy

Delete the bucket policy from the connected bucket

s3_conn.delete_bucket_policy()

List User Policies

List inline policies for an IAM user

policies = s3_conn.list_user_policies(UserName='user-name')

Block or Allow Public Access

Block or allow public access to the bucket

s3_conn.public_access(block=True)  # Block public access

s3_conn.public_access(block=False) # Allow public access

Generate Presigned Create URL

Generate a presigned URL for creating an object

responce = s3_conn.presigned_create_url(

    object_name='object_name',

    params={'key': 'value'},

    fields={'field': 'value'},

    conditions=[{'condition': 'value'}],

    expiration=3600

)

object_name : The name of the object to be created in the S3 bucket.

params: (Optional) Additional request parameters to include in the presigned URL.

fields : (Optional) Pre-filled form fields to include in the presigned URL.

conditions: (Optional) Conditions to include in the presigned URL.

expiration : (Optional) Time in seconds for which the presigned URL should remain valid. Default is 3600 seconds (1 hour).

Output
{'url': 'https://{{bucketname}}.s3.amazonaws.com/', 'fields': {'key': '{{object_name}}.mp4', 'x-amz-algorithm': 'AWS4-HMAC-SHA256', 'x-amz-credential': 'ASIASXFYXRCWS2V7P3US/20240731/ap-south-1/s3/aws4_request', 'x-amz-date': '20240731T145524Z', 'x-amz-security-token': 'IQoJb3JpZ2luX2VjEHcaCmFwLXNvdXRoLTEiRzBFAiBNpppLXjpopHVEyPJuNu+zgxIo4u9bWLP5ILcL75CPCQIhAOOHx0OACSBdLzc41Vn2NUzKTvfQHfflqcBuT10tb7RCKocDCGAQABoMMTg3MjE4N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SStjNWZGdEUxZU9NZTFHQjFScHM5NTl1TWFPVG42bzJ6TitHcXFlS28zYllGNUtxb1NFUVQ1aEdnVmdMak0ra1pYaDhvMU80OTU4aW84V0haVkRYSm5peWlwNXlNWFJJTGFjcnZscDRkMlg0cFNmQ1hCT2FFcE1XdEQ4In1dfQ==', 'x-amz-signature': '878f7196136da54e345460f49b636ef5ea1ca54fd5615f86e25f77ff1a20d755'}}

Generate Presigned Delete URL

Generate a presigned URL for Delete an object

responce = s3_conn.presigned_delete_url(object_name='object_name', expiration=3600)
Output
"https://{{bucket_name}}.s3.amazonaws.com/hdfwf.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIASXFYXRCWTXZSH6N3%2F20240812%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20240812T151744Z&X-Amz-Expires=4600&X-Amz-SignedHeaders=host&X-Amz-Signature=a680ee638e9cc27f2ec21cae36ad807cffb2fc74e1e91fa55709f25f324d1e22"

Uploads a file to the connected S3 bucket.

responce = s3_conn.upload_object(file=file, key_name="object_name")



# example:

with open('file_name',"rb") as file_obj:

    s3_conn.upload_object(file=file_obj, key_name="object_name")

file: Bytes of the file to upload

key_name: S3 object name (e.g., 'folder/filename.txt').

Retrieves an object from the connected S3 bucket.

return: The object metadata

key_object = s3_conn.get_object(key_name="object_name")

Deletes an object from the connected S3 bucket

key_object = s3_conn.delete_object(key_name="object_name")

Lists objects in the connected S3 bucket.

key_object = s3_conn.get_objects()
key_object = s3_conn.get_objects(only_objects=True)

only_objects: If True, returns a list of object metadata (excluding keys).

key_object = s3_conn.get_objects(only_keys=True)

only_keys: If True, returns a list of object keys.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudspark-1.0.11.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudspark-1.0.11-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file cloudspark-1.0.11.tar.gz.

File metadata

  • Download URL: cloudspark-1.0.11.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for cloudspark-1.0.11.tar.gz
Algorithm Hash digest
SHA256 b3d94653fe62ea7eb01541cbea0d40ad77fcea3f8ca95b2030038e6969bad6a9
MD5 6e95a569c9f9834f0b37b701c928de27
BLAKE2b-256 08a992f2cf5ce2f390b360085d56432dece9328ae30ec8fcfbd439d72318d4e9

See more details on using hashes here.

File details

Details for the file cloudspark-1.0.11-py3-none-any.whl.

File metadata

  • Download URL: cloudspark-1.0.11-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for cloudspark-1.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 ec38d98a23df9b5674bbc94af4cede7f91874654c8332f752748cf0e63a0c288
MD5 22c7e973c3af614a8a100d4083f94ca1
BLAKE2b-256 0da5ed3cd73fdcd757ab290b3ab54e8eb7704023e5c66eb6388ff89795674c1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page