📔
ServerlessDocs - Serverless File Service
  • Introduction
  • Why Serverless!!
  • Prerequisites
  • User Management
    • Authentication
    • Amazon Cognito
    • Create the User Pool
    • Create an App Client
    • Integrate the app with Cognito User Pool
    • Create the Identity Pool
    • Validate the setup
    • Troubleshooting
  • Manage Documents with AWS S3
    • Authorization
    • Update Cognito Identity Pool Auth Role
    • S3 Documents Bucket Folder Structure
    • Validate the Access Permissions
    • Public and Private files access
    • Upload Files to S3
    • Download Files
    • Delete Files
    • Share Files
  • More Security Configurations
    • Cognito user emails
    • Password policies
    • Allow only Organization and Whitelisted users to Signup
    • Allow admin users to upload Public files
    • Notify Admin users when a new user signup
    • Enable Multi-factor Authentication (MFA)
  • User Operations
    • Password Reset / Forgot Password
    • Resend Verification Code
  • Serverless APIs
    • APIs
    • Secure APIs using API Gateway Authorizer
    • Access Cognito values in Lambda function
    • Authorize APIs with OAuth 2 Scope
    • Fine-grained Access with AssumeRole
    • Notify Admin users on large file uploads
    • Generate a Month-To-Date Usage report
  • Source Code and Setup
    • Source Code
    • IAM Policies
    • S3 Bucket Policies
    • Suggestions / Feedback
    • More References
  • Deployment
    • AWS SAM
    • Serverless Framework
  • Contributors
    • Team
Powered by GitBook
On this page
  1. Manage Documents with AWS S3

Upload Files to S3

PreviousPublic and Private files accessNextDownload Files

Last updated 5 years ago

Was this helpful?

CtrlK

Was this helpful?

Now each user gets her own folder inside the S3 bucket, we can now upload files from our application front-end and validate the access.

function getUserId(){
    const key = `aws.cognito.identity-id.${identityPoolId}`;
    return cognitoUser.pool.storage[key];
}

function uploadFile(){
    var files = document.getElementById("file-upload").files;
    if (!files.length) {
        return alert("Please choose a file to upload.");
    }
    var file = files[0];
    var fileName = file.name;    
    var fileKey = `users/${getUserId()}/${fileName}`;

    var upload = new AWS.S3.ManagedUpload({
        params: {
            Bucket: S3DocBucket,
            Key: fileKey,
            Body: file        
        }
    });

    upload.promise()
    .then((data) => {
        // file uploaded
    },(err) => {
        // error uploading the file
    });
}

AWS S3 is an Object Storage service. Unlike the regular file system, S3 stores the data in a different way, and you can consider it's a Key/Value store as well. In the above example, we are uploading a file with Key=users/<userId>/filename and Value=<file data>.