Working with S3 Buckets


Create a S3 Bucket

    
from cdev.aws.s3 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

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.

    
from cdev.aws.s3 import Bucket myBucket = Bucket("demo_bucket") myBucket2 = Bucket("demo_bucket2", nonce="123")

Use a Serverless function to read objects from a S3 Bucket

The Bucket object provides the permissions and output to configure a Serverless function to be able to integrate and read objects.

    
import os import boto3 from cdev.aws.s3 import Bucket from cdev.aws.lambda_function import ServerlessFunction myBucket = Bucket("demo_bucket") s3_client = boto3.client('s3') @ServerlessFunction("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') )

Use a Serverless function to write objects to a S3 Bucket

The Bucket object provides the permissions and output to configure a Serverless function to be able to integrate and write objects.

    
import os import boto3 from cdev.aws.s3 import Bucket from cdev.aws.lambda_function import ServerlessFunction myBucket = Bucket("demo_bucket") s3_client = boto3.client('s3') @ServerlessFunction("write_function", environment={"BUCKET_NAME": myBucket.output.bucket_name}, permissions=[myBucket.available_permissions.READ_AND_WRITE_BUCKET]) def write_object(event, context): bucket_name = os.environ.get('BUCKET_NAME') print( s3_client.put_object(Body='Text Test', Bucket=bucket_name, Key="filename.txt") )

Use a Serverless function to handle events from a S3 Bucket

The Bucket object provides the permissions and output to configure a Serverless function to be triggered from events from a S3 Bucket.

    
import os import boto3 from cdev.aws.s3 import Bucket, Bucket_Event_Type from cdev.aws.lambda_function import ServerlessFunction myBucket = Bucket("demo_bucket") myBucket_event = myBucket.create_event_trigger(Bucket_Event_Type.Object_Created) s3_client = boto3.client('s3') @ServerlessFunction("handle_event_function", events=[myBucket_event], environment={"BUCKET_NAME": myBucket.output.bucket_name}, permissions=[myBucket.available_permissions.LIST_BUCKET]) def handle_event(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)}