Working with S3 Buckets

The Amazon Simple Storage Service (S3) was one of the first offerings by Aws, and it has become perhaps their most well know service. Cdev makes it easy to quickly create and integrate S3 Buckets into your projects.


Create a S3 Bucket

    
from cdev.resources.simple.object_store import Bucket myBucket = Bucket("demo_bucket")

You can use the cdev run bucket commands to push and pull objects to the bucket from your computer.

cdev run bucket.cp <local.json> bucket://<bucket-info>
Using the Aws cli

You can also get the deployed name of your bucket with the cdev output command and then use the aws cli tool to transfer files.

cdev output <component_name>.bucket.demo_bucket.bucket_name
aws s3 cp <local_file> s3://<your-bucket-name>

Creating Multiple Buckets

    
from cdev.resources.simple.object_store import Bucket myBucket = Bucket("demo_bucket") myBucket2 = Bucket("demo_bucket2", nonce="123")

Note that a Bucket does not currently have any available settings and configuration, which means we must provide a nonce value to distinguish between the two resources. To learn more about how the nonce value works within the larger Cdev framework please read our deep dive on our architecture.


Use a Serverless function to read objects from a S3 Bucket

    
import os import boto3 from cdev.resources.simple.object_store import Bucket from cdev.resources.simple.xlambda import simple_function_annotation myBucket = Bucket("demo_bucket") s3_client = boto3.client('s3') @simple_function_annotation("read_function", environment={"BUCKET_NAME": myBucket.output.bucket_name}, permissions=[myBucket.available_permissions.LIST_BUCKET]) def read_object(event, context): bucket_name = os.environ.get('BUCKET_NAME') print( s3_client.list_objects( Bucket=bucket_name ).get('Contents') )

The Bucket object provides the permissions and output to configure a Serverless function to be able to integrate and read objects. For more in depth information on how to configure Serverless Functions, read our documentation on the available settings.


Use a Serverless function to write objects to a S3 Bucket

    
import os import boto3 from cdev.resources.simple.object_store import Bucket from cdev.resources.simple.xlambda import simple_function_annotation myBucket = Bucket("demo_bucket") s3_client = boto3.client('s3') @simple_function_annotation("read_function", environment={"BUCKET_NAME": myBucket.output.bucket_name}, permissions=[myBucket.available_permissions.READ_AND_WRITE_BUCKET]) def read_object(event, context): bucket_name = os.environ.get('BUCKET_NAME') print( s3_client.put_object(Body='Text Test', Bucket=bucket_name, Key="filename.txt") )

The Bucket object provides the permissions and output to configure a Serverless function to be able to integrate and write objects. For more in depth information on how to configure Serverless Functions, read our documentation on the available settings.


Use a Serverless function to handle events from a S3 Bucket

    
import os import boto3 from cdev.resources.simple.object_store import Bucket, Bucket_Event_Type from cdev.resources.simple.xlambda import simple_function_annotation myBucket = Bucket("demo_bucket") myBucket_event = myBucket.create_event_trigger(Bucket_Event_Type.Object_Created) s3_client = boto3.client('s3') @simple_function_annotation("read_function", events=[myBucket_event], environment={"BUCKET_NAME": myBucket.output.bucket_name}, permissions=[myBucket.available_permissions.LIST_BUCKET]) def read_object(event, context): bucket_name = os.environ.get('BUCKET_NAME') result = s3_client.list_objects(Bucket=bucket_name).get('Contents') print(result) return {"message": str(result)}

For more in depth information on how to configure Serverless Functions, read our documentation on the available settings.