storing user files with express, stormpath, and amazon s3
TRANSCRIPT
Storing User Files with Express,
Stormpath, and Amazon
S3
@rdegges
I’m Randall Degges
Developer Evangelist at Stormpath
Python / Node / Go Hacker
Agenda
- Stormpath Intro (5 minutes)
- Main Talk (30 minutes)- Q/A (15 minutes)
Intro to Stormpath
API Service Website Mobile App
We Do a Lot- Libraries for many languages- AD / LDAP- Single Sign On (SAML)- OAuth2- Social Login- Multi-Tenancy- Groups and Roles- Email Workflows
What’s the problem?
www.awesome.io
avatar.png
receipt.pdf
invoice.pdf
Stormpath!???
How do people usually store user files?
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
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.
Your Webserver(s)
Webserver
avatar.png
avatar.png
avatar.png
avatar.png
avatar.png
avatar.png
avatar.png
avatar.png
LOW
DI
SK
SPAC
E!!OMG!
Backups?
!
Amazon S3
- Reliability.- Durability.- Availability.- Cost.- Speed.- Security ACLs.
How S3 Works
avatar.png
webserver
S3 Bucket
File URL
Custom Data (for user)
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" } } }}
Cool, Right?
So… Let’s Build Something!
$ npm install express$ npm install express-stormpath$ npm install express-stormpath-s3
"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!
/register
/login
app.use(stormpathS3({ awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID, awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, awsBucket: 'express-stormpath-s3'}));
This sets up the S3 client and adds simple file methods onto req.user. Required.
Setup Done!
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.
Upload Files (cont)
req.user.uploadFile("./avatar.png", "public-read", (err) => { if (err) throw err; console.log("Successfully uploaded file!");});
Desired ACL for file.
Delete Files
req.user.deleteFile("avatar.png", (err) => { if (err) throw err; console.log("Successfully deleted file!");});
(demo)
Resources- https://github.com/rdegges/express-stor
mpath-s3
- https://aws.amazon.com/s3/
- https://stormpath.com/
- https://github.com/rdegges/express-stormpath-s3-webinar
- https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html
You’re awesome.
@rdegges