Skip to content
The Chat Archive
minimax-m1 LMArena Human Preference 140K

Generate an app in amplify that takes a picture sense to s3 bucket which then triggeres…

  1. User

    Generate an app in amplify that takes a picture sense to s3 bucket which then triggeres a lambda if a file is created

  2. 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

    1. Go to AWS Console > S3 > Your Bucket
    2. Properties > Event notifications > Create event notification
    3. Configure:
      • Event name: "ImageUploadTrigger"
      • Event types: "PUT" (Object creation)
      • Prefix: "images/"
      • Select your Lambda function as the destination

    5. Deploy and Test

    amplify push
    

    6. Required Dependencies (package.json)

    {
      "dependencies": {
        "expo-image-picker": "~14.0.0",
        "aws-amplify": "^5.0.0",
        "react-native": "0.71.0"
      }
    }
    

    Key Features:

    1. Camera capture using Expo's ImagePicker
    2. Secure S3 upload with Amplify Storage
    3. Automatic Lambda trigger on file creation
    4. Event metadata logging in CloudWatch
    5. Responsive UI with image preview

    Architecture Diagram:

    Mobile App → S3 Upload → Event Trigger → Lambda Execution
         ↑           ↓
         └─ Camera ─┘
    

    To test:

    1. Open the app and take a picture
    2. Check S3 bucket for uploaded image
    3. Verify Lambda execution in CloudWatch Logs

    This implementation provides a complete serverless image processing pipeline with mobile capture capabilities.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.