Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Storing User Files with Express, Stormpath, and Amazon S3

Stormpath
October 06, 2016

Storing User Files with Express, Stormpath, and Amazon S3

Sign up for Stormpath: https://api.stormpath.com/register
Watch the presentation: https://youtu.be/dxKbcBDvvrI
Read the blog post: https://stormpath.com/blog/securely-storing-files-node-s3

Join Stormpath Developer Evangelist, Randall Degges, to learn how to store user files using Amazon S3. He’ll cover everything you need to know to properly handle user files in your web applications.

Randall will cover:
- What is the problem we're trying to solve?
- How files are typically stored
- What you need to know about Amazon S3
- How to build a basic Express application with user authentication
- How to securely store files in S3 using express-stormpath-s3
- Q/A Session

Stormpath

October 06, 2016
Tweet

More Decks by Stormpath

Other Decks in Programming

Transcript

  1. We Do a Lot - Libraries for many languages -

    AD / LDAP - Single Sign On (SAML) - OAuth2 - Social Login - Multi-Tenancy - Groups and Roles - Email Workflows
  2. Database Columns CREATE TABLE IF NOT EXISTS users ( id

    UUID DEFAULT uuid_generate_v4(), email TEXT PRIMARY KEY, password TEXT NOT NULL, avatar TEXT ); avatar.png Base64 encode
  3. No!!! - Makes DB queries for each image view. -

    Slows down DB. - DBs aren’t good at this sort of IO (heavy disk reading). - Slow performance for end users.
  4. User CustomData { "href": "https://api.stormpath.com/v1/accounts/gbMUL3uP8rFLZUMAw2XhI", "email": "[email protected]", "givenName": "Randall", "surname":

    "Degges", "customData": { "href": "https://api.stormpath.com/v1/accounts/gbMUL3uP8rFLZUMAw2XhI/customData", "s3": { "fluent.jpg": { "href": "https://s3.amazonaws.com/express-stormpath- s3/gbMUL3uP8rFLZUMAw2XhI/fluent.jpg", "lastModified": "2016-10-05T23:20:53.508Z" }, "wallhaven-204175.jpg": { "href": "https://s3.amazonaws.com/express-stormpath-s3/gbMUL3uP8rFLZUMAw2XhI/wallhaven- 204175.jpg", "lastModified": "2016-10-06T01:08:58.898Z" } } } }
  5. So… Let’s Build Something! $ npm install express $ npm

    install express-stormpath $ npm install express-stormpath-s3
  6. "use strict"; const express = require("express"); const stormpath = require("express-stormpath");

    const stormpathS3 = require("express-stormpath-s3"); let app = express(); app.use(stormpath.init(app)); app.use(stormpath.getUser); // Other middleware here // Routes here app.listen(3000); The Magic!
  7. Upload Files req.user.uploadFile("./avatar.png", (err) => { if (err) throw err;

    console.log("Successfully uploaded file!"); }); Path to local file you want to upload.
  8. Upload Files (cont) req.user.uploadFile("./avatar.png", "public-read", (err) => { if (err)

    throw err; console.log("Successfully uploaded file!"); }); Desired ACL for file.