What is the best design of a system that uploads a file to S3, than resizes it with…
- User
What is the best design of a system that uploads a file to S3, than resizes it with lambda but using the same key as uploaded, without triggering the resize lambda function twice or more?
- Assistant
To prevent triggering the resize Lambda function multiple times, you can use a unique identifier in the object key and check if it exists before triggering the resize function.
Here's one possible design of a system that uploads a file to S3 and resizes it with a Lambda function:
The user uploads a file to S3, which triggers a S3 "Put" event. The S3 event invokes a Lambda function (let's call it the "controller" function) that is responsible for processing the uploaded file. The controller function generates a unique identifier (e.g., a timestamp) and appends it to the original file key. The controller function checks if an object with the new key exists in S3. If the object does not exist, the controller function triggers the resize Lambda function (let's call it the "resizer" function) with the new key as input. The resizer function resizes the file and stores it in S3 under the new key. If the object already exists, the controller function terminates and does not trigger the resizer function. This design ensures that the resizer function is triggered only once per unique file, and that the resized file is stored under a unique key.
- User
Show me how with AWS SAM CLI commands or as a bash script
- Assistant
Here's an example AWS SAM CLI command to create a SAM template for this setup: sam init --runtime python3.8 cd sam-app/ sam build sam package --output-template-file packaged.yaml sam deploy --template-file packaged.yaml --stack-name s3-resize-stack --capabilities CAPABILITY_IAM
Here's the Sam template:
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: S3Bucket: Type: AWS::S3::Bucket DeletionPolicy: Retain Properties: BucketName: my-resize-bucket NotificationConfiguration: LambdaConfigurations: - Event: s3:ObjectCreated:* Function: !GetAtt LambdaResizeFunction.Arn
LambdaResizeFunction: Type: AWS::Serverless::Function Properties: CodeUri: resize_function.py Handler: index.handler Runtime: python3.8 Environment: Variables: S3_BUCKET: !Ref S3Bucket Policies: - S3CrudPolicy: BucketName: !Ref S3Bucket
And here's an example Python script for the Lambda function that resizes the uploaded file:
import boto3
def handler(event, context): s3 = boto3.client('s3') bucket_name = os.environ['S3_BUCKET']
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)