minimax-m1 LMArena Human Preference 140K
Generate an app in amplify that takes a picture sense to s3 bucket which then triggeres…
- User
Generate an app in amplify that takes a picture sense to s3 bucket which then triggeres a lambda if a file is created
- Assistant
Here's a step-by-step implementation of an AWS Amplify app that captures images and triggers a Lambda function when files are uploaded to S3:
1. Set Up Amplify Project
npm install -g @aws-amplify/cli amplify init amplify add storage # Select Amazon S3 # Configure as needed (default settings work) amplify add function # Select Lambda function # Name it "imageProcessor" # Select "Create a new Lambda function"2. React Native Component (Camera Capture)
import { useState } from 'react'; import { View, Button, Image, Alert } from 'react-native'; import * as ImagePicker from 'expo-image-picker'; import { Storage } from 'aws-amplify'; const CameraCapture = () => { const [image, setImage] = useState(null); const takePicture = async () => { const permission = await ImagePicker.requestCameraPermissionsAsync(); if (!permission.granted) { Alert.alert('Permission required'); return; } const result = await ImagePicker.launchCameraAsync({ allowsEditing: true, aspect: [4, 3], quality: 1, }); if (!result.canceled) { setImage(result.assets[0].uri); await uploadToS3(result.assets[0].uri); } }; const uploadToS3 = async (uri) => { try { const response = await fetch(uri); const blob = await response.blob(); await Storage.put(`images/${Date.now()}.jpg`, blob, { contentType: 'image/jpeg', }); Alert.alert('Upload successful!'); } catch (error) { console.error('Upload error:', error); } }; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Button title="Take Picture" onPress={takePicture} /> {image && <Image source={{ uri: image }} style={{ width: 200, height: 200 }} />} </View> ); }; export default CameraCapture;3. Lambda Function Code (
amplify/backend/function/imageProcessor/src/index.js)exports.handler = async (event) => { console.log('Event:', JSON.stringify(event, null, 2)); // Process the S3 event here const s3Record = event.Records[0].s3; const bucket = s3Record.bucket.name; const key = decodeURIComponent(s3Record.object.key.replace(/\+/g, ' ')); console.log(`New file uploaded to s3://${bucket}/${key}`); // Add your image processing logic here return { statusCode: 200, body: JSON.stringify({ message: 'Processing complete' }), }; };4. Configure S3 Event Trigger
- Go to AWS Console > S3 > Your Bucket
- Properties > Event notifications > Create event notification
- Configure:
- Event name: "ImageUploadTrigger"
- Event types: "PUT" (Object creation)
- Prefix: "images/"
- Select your Lambda function as the destination
5. Deploy and Test
amplify push6. Required Dependencies (
package.json){ "dependencies": { "expo-image-picker": "~14.0.0", "aws-amplify": "^5.0.0", "react-native": "0.71.0" } }Key Features:
- Camera capture using Expo's ImagePicker
- Secure S3 upload with Amplify Storage
- Automatic Lambda trigger on file creation
- Event metadata logging in CloudWatch
- Responsive UI with image preview
Architecture Diagram:
Mobile App → S3 Upload → Event Trigger → Lambda Execution ↑ ↓ └─ Camera ─┘To test:
- Open the app and take a picture
- Check S3 bucket for uploaded image
- Verify Lambda execution in CloudWatch Logs
This implementation provides a complete serverless image processing pipeline with mobile capture capabilities.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.