Skip to content

puffer-git/aws-lambda-file-upload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS Lambda File Upload

A lightweight, serverless AWS Lambda function for uploading files directly to Amazon S3 with CloudFront signed URL generation. This project provides a simple and efficient solution for handling file uploads in serverless architectures.

🎯 Motivation

File uploads are a common requirement in modern web applications, but implementing a secure, scalable solution can be complex. This project was created to:

  • Simplify file uploads: Provide a ready-to-use Lambda function that handles file uploads without the need for complex server infrastructure
  • Leverage serverless architecture: Take advantage of AWS Lambda's scalability and cost-effectiveness for handling file uploads
  • Generate secure URLs: Automatically create CloudFront signed URLs for secure file access
  • Reduce boilerplate: Save development time by providing a production-ready solution that can be easily integrated into existing projects

Whether you're building a new application or need to add file upload functionality to an existing one, this project offers a straightforward, maintainable solution that follows AWS best practices.

✨ Features

  • 📤 Direct S3 Upload: Upload files directly to Amazon S3 buckets
  • 🔒 Secure Access: Generate CloudFront signed URLs for secure file access
  • 🚀 Serverless: Built on AWS Lambda for automatic scaling and cost efficiency
  • 📝 Multipart Support: Handles multipart/form-data file uploads
  • Lightweight: Minimal dependencies and fast execution
  • 🔧 Configurable: Easy configuration through environment variables

📋 Prerequisites

Before using this Lambda function, ensure you have:

  • An AWS account with appropriate permissions
  • An S3 bucket configured for file storage
  • A CloudFront distribution (optional, for signed URLs)
  • AWS Lambda runtime environment (Node.js 18.x or later recommended)
  • AWS credentials with permissions for:
    • S3: PutObject, PutObjectAcl
    • CloudFront: Access to generate signed URLs (if using CloudFront)

🚀 Installation

  1. Clone this repository:
git clone https://github.com/yourusername/aws-lambda-file-upload.git
cd aws-lambda-file-upload
  1. Install dependencies:
npm install

⚙️ Configuration

Configure the Lambda function using environment variables:

Variable Description Required
REACT_APP_ACCESS_KEY_ID AWS Access Key ID Yes
REACT_APP_SECRET_ACCESS_KEY AWS Secret Access Key Yes
REACT_APP_S3_BUCKET_NAME Name of the S3 bucket Yes
REACT_APP_CLOUDFRONT_ACCESS_KEY_ID CloudFront Key Pair ID Yes (if using CloudFront)
REACT_APP_CLOUDFRONT_PRIVATE_KEY_STRING CloudFront Private Key (base64 encoded) Yes (if using CloudFront)
REACT_APP_CLOUD_FRONT_ORIGIN_PATH CloudFront distribution URL Yes (if using CloudFront)

Setting Environment Variables in AWS Lambda

  1. Go to your Lambda function in the AWS Console
  2. Navigate to ConfigurationEnvironment variables
  3. Add the required variables listed above

📖 Usage

Lambda Function Handler

The function expects an event with the following structure:

{
  headers: {
    'content-type': 'multipart/form-data; boundary=...'
  },
  body: 'base64-encoded-file-data'
}

Example Request

// Example using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);

const response = await fetch('your-api-gateway-endpoint', {
  method: 'POST',
  body: formData
});

const result = await response.json();
console.log('File URL:', result);

Response

The function returns a CloudFront signed URL:

"https://your-cloudfront-domain.com/1234567890"

🔧 Deployment

Using AWS CLI

  1. Package your function:
zip -r function.zip index.js node_modules/
  1. Deploy to Lambda:
aws lambda update-function-code \
  --function-name your-function-name \
  --zip-file fileb://function.zip

Using Serverless Framework

# serverless.yml
service: aws-lambda-file-upload

provider:
  name: aws
  runtime: nodejs18.x
  environment:
    REACT_APP_ACCESS_KEY_ID: ${env:AWS_ACCESS_KEY_ID}
    REACT_APP_SECRET_ACCESS_KEY: ${env:AWS_SECRET_ACCESS_KEY}
    REACT_APP_S3_BUCKET_NAME: ${env:S3_BUCKET_NAME}
    # ... other environment variables

functions:
  upload:
    handler: index.handler
    events:
      - http:
          path: upload
          method: post

📝 API Reference

handler(event)

Main Lambda handler function that processes file uploads.

Parameters:

  • event (Object): Lambda event object containing:
    • headers (Object): Request headers with content-type
    • body (String): Base64-encoded file data

Returns:

  • Promise<String>: CloudFront signed URL of the uploaded file

Throws:

  • Error if S3 upload fails
  • Error if file extraction fails

🤝 Contributing

Contributions are welcome! This project is open source and we encourage community participation.

How to Contribute

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow existing code style and conventions
  • Add comments for complex logic
  • Update documentation for new features
  • Ensure backward compatibility when possible
  • Test your changes thoroughly

📄 License

This project is open source and available under the MIT License.

👤 Author

Developer

🙏 Acknowledgments

📚 Additional Resources


⭐ If you find this project helpful, please consider giving it a star!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published