├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── s3UploaderFunction ├── app.js ├── package.json └── testHarness.js └── template.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/osx,node,linux,windows 3 | 4 | ### Linux ### 5 | *~ 6 | 7 | # temporary files which can be created if a process still has a handle open of a deleted file 8 | .fuse_hidden* 9 | 10 | # KDE directory preferences 11 | .directory 12 | 13 | # Linux trash folder which might appear on any partition or disk 14 | .Trash-* 15 | 16 | # .nfs files are created when an open file is removed but is still being accessed 17 | .nfs* 18 | 19 | ### Node ### 20 | # Logs 21 | logs 22 | *.log 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # Runtime data 28 | pids 29 | *.pid 30 | *.seed 31 | *.pid.lock 32 | 33 | # Directory for instrumented libs generated by jscoverage/JSCover 34 | lib-cov 35 | 36 | # Coverage directory used by tools like istanbul 37 | coverage 38 | 39 | # nyc test coverage 40 | .nyc_output 41 | 42 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 43 | .grunt 44 | 45 | # Bower dependency directory (https://bower.io/) 46 | bower_components 47 | 48 | # node-waf configuration 49 | .lock-wscript 50 | 51 | # Compiled binary addons (http://nodejs.org/api/addons.html) 52 | build/Release 53 | 54 | # Dependency directories 55 | node_modules/ 56 | jspm_packages/ 57 | 58 | # Typescript v1 declaration files 59 | typings/ 60 | 61 | # Optional npm cache directory 62 | .npm 63 | 64 | # Optional eslint cache 65 | .eslintcache 66 | 67 | # Optional REPL history 68 | .node_repl_history 69 | 70 | # Output of 'npm pack' 71 | *.tgz 72 | 73 | # Yarn Integrity file 74 | .yarn-integrity 75 | 76 | # dotenv environment variables file 77 | .env 78 | 79 | 80 | ### OSX ### 81 | *.DS_Store 82 | .AppleDouble 83 | .LSOverride 84 | 85 | # Icon must end with two \r 86 | Icon 87 | 88 | # Thumbnails 89 | ._* 90 | 91 | # Files that might appear in the root of a volume 92 | .DocumentRevisions-V100 93 | .fseventsd 94 | .Spotlight-V100 95 | .TemporaryItems 96 | .Trashes 97 | .VolumeIcon.icns 98 | .com.apple.timemachine.donotpresent 99 | 100 | # Directories potentially created on remote AFP share 101 | .AppleDB 102 | .AppleDesktop 103 | Network Trash Folder 104 | Temporary Items 105 | .apdisk 106 | 107 | ### Windows ### 108 | # Windows thumbnail cache files 109 | Thumbs.db 110 | ehthumbs.db 111 | ehthumbs_vista.db 112 | 113 | # Folder config file 114 | Desktop.ini 115 | 116 | # Recycle Bin used on file shares 117 | $RECYCLE.BIN/ 118 | 119 | # Windows Installer files 120 | *.cab 121 | *.msi 122 | *.msm 123 | *.msp 124 | 125 | # Windows shortcuts 126 | *.lnk 127 | 128 | 129 | # End of https://www.gitignore.io/api/osx,node,linux,windows -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *master* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | 61 | We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes. 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 11 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 12 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless S3 Uploader 2 | 3 | The Serverless S3 Uploader allows you to upload JPG files to Amazon S3 buckets from your web applications using pre-signed URLs. 4 | 5 | Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS pricing page](https://aws.amazon.com/pricing/) for details. 6 | 7 | ```bash 8 | . 9 | ├── README.MD <-- This instructions file 10 | ├── s3UploaderFunction <-- Source code for the main lambda function 11 | │ └── app.js <-- Main Lambda handler 12 | │ └── testHarness.js <-- For testing code locally 13 | │ └── package.json <-- NodeJS dependencies and scripts 14 | ├── template.yaml <-- SAM template 15 | ``` 16 | 17 | See this [YouTube video walkthrough](https://www.youtube.com/watch?v=mw_-0iCVpUc&list=PLJo-rJlep0EAY0nMNBv0MZ487l1tOFAjh&index=7) of how uploading to S3 works with presigned URLs. 18 | 19 | ## Requirements 20 | 21 | If using outside of the AWS Serverless Application Repository: 22 | 23 | * AWS CLI already configured with Administrator permission 24 | * [NodeJS 12.x installed](https://nodejs.org/en/download/) 25 | 26 | ## Installation Instructions 27 | 28 | 1. [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and login. 29 | 1. Go to the app's page on the [Serverless Application Repository](https://serverlessrepo.aws.amazon.com/applications/) and click "Deploy" 30 | 31 | ## Using this Application 32 | 33 | * This application creates an API Gateway endpoint where your application can request a pre-signed URL to upload JPG objects to an S3 bucket. Once the API returns the URL, your application can PUT the object data to this URL to complete the upload. 34 | * This application is for educational purposes and does not provide any throttling on the API Gateway endpoint. For production usage, you should [apply throttling to your API resources](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-throttling.html). 35 | * You can modify this application to upload other types of object. 36 | 37 | ## How it works 38 | 39 | * Deploy this serverless application and take a note of the API endpoint. 40 | * Invoke the API to receive a pre-signed URL for uploading a JPG file. Use this pre-signed URL to complete the upload. 41 | * For a live example of this, see [this Fiddle](https://jsfiddle.net/jbeswick/Lq3vkdx2/). View the browser console to see logs of how the Fiddle is interacting with the API Gateway and presigned URL. 42 | 43 | ============================================== 44 | 45 | Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 46 | 47 | SPDX-License-Identifier: MIT-0 48 | -------------------------------------------------------------------------------- /s3UploaderFunction/app.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 4 | software and associated documentation files (the "Software"), to deal in the Software 5 | without restriction, including without limitation the rights to use, copy, modify, 6 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 7 | permit persons to whom the Software is furnished to do so. 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 9 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 10 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 11 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 12 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 13 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | */ 15 | 16 | 'use strict' 17 | 18 | const AWS = require('aws-sdk') 19 | AWS.config.update({ region: process.env.AWS_REGION || 'us-east-1' }) 20 | const s3 = new AWS.S3() 21 | 22 | // Main Lambda entry point 23 | exports.handler = async (event) => { 24 | const result = await getUploadURL() 25 | console.log('Result: ', result) 26 | return result 27 | } 28 | 29 | const getUploadURL = async function() { 30 | const actionId = parseInt(Math.random()*10000000) 31 | 32 | const s3Params = { 33 | Bucket: process.env.UploadBucket, 34 | Key: `${actionId}.jpg`, 35 | ContentType: 'image/jpeg' // Update to match whichever content type you need to upload 36 | //ACL: 'public-read' // Enable this setting to make the object publicly readable - only works if the bucket can support public objects 37 | } 38 | 39 | console.log('getUploadURL: ', s3Params) 40 | return new Promise((resolve, reject) => { 41 | // Get signed URL 42 | resolve({ 43 | "statusCode": 200, 44 | "isBase64Encoded": false, 45 | "headers": { 46 | "Access-Control-Allow-Origin": "*" 47 | }, 48 | "body": JSON.stringify({ 49 | "uploadURL": s3.getSignedUrl('putObject', s3Params), 50 | "photoFilename": `${actionId}.jpg` 51 | }) 52 | }) 53 | }) 54 | } 55 | -------------------------------------------------------------------------------- /s3UploaderFunction/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "form-handler", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "James Beswick, Amazon Web Services", 11 | "license": "MIT", 12 | "dependencies": { 13 | "aws-sdk": "^2.714.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /s3UploaderFunction/testHarness.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 4 | software and associated documentation files (the "Software"), to deal in the Software 5 | without restriction, including without limitation the rights to use, copy, modify, 6 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 7 | permit persons to whom the Software is furnished to do so. 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 9 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 10 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 11 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 12 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 13 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | */ 15 | 16 | // For local testing 17 | 18 | const { handler } = require('./app') 19 | 20 | const main = async () => { 21 | await handler() 22 | } 23 | 24 | main() 25 | 26 | -------------------------------------------------------------------------------- /template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: Serverless S3 Uploader - upload files to S3 buckets from your web applications using pre-signed URLs. 4 | 5 | # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst 6 | Globals: 7 | Api: 8 | # Allows any site to call these APIs (can restrict by replacing asterisk with site name) 9 | # Automatically adds AllowMethods with a list of methods for this API 10 | EndpointConfiguration: EDGE 11 | Cors: 12 | AllowMethods: "'OPTIONS,GET'" 13 | AllowHeaders: "'Content-Type'" 14 | AllowOrigin: "'*'" 15 | Function: 16 | Timeout: 5 17 | 18 | Resources: 19 | S3Bucket: 20 | Type: AWS::S3::Bucket 21 | Properties: 22 | CorsConfiguration: 23 | CorsRules: 24 | - AllowedHeaders: 25 | - "*" 26 | AllowedMethods: 27 | - GET 28 | - PUT 29 | - POST 30 | - DELETE 31 | - HEAD 32 | AllowedOrigins: 33 | - "*" 34 | 35 | S3UploaderFunction: 36 | # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction 37 | Type: AWS::Serverless::Function 38 | Properties: 39 | CodeUri: s3UploaderFunction/ 40 | Handler: app.handler 41 | Runtime: nodejs12.x 42 | MemorySize: 128 43 | Environment: 44 | Variables: 45 | UploadBucket: !Ref S3Bucket 46 | Policies: 47 | - S3CrudPolicy: 48 | BucketName: !Ref S3Bucket 49 | Events: 50 | HttpPost: 51 | Type: Api 52 | Properties: 53 | Path: '/' 54 | Method: get 55 | 56 | Outputs: 57 | S3UploaderFunction: 58 | Description: "Lambda Function ARN" 59 | Value: !GetAtt S3UploaderFunction.Arn 60 | S3UploaderFunctionIamRole: 61 | Description: "Implicit IAM Role created for function" 62 | Value: !GetAtt S3UploaderFunctionRole.Arn 63 | S3BucketName: 64 | Description: S3 bucket 65 | Value: !Ref S3Bucket --------------------------------------------------------------------------------