Skip to content

Instantly share code, notes, and snippets.

@astrotars
Created January 22, 2019 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astrotars/4eb23c5a8e205272e3b382730be617fe to your computer and use it in GitHub Desktop.
Save astrotars/4eb23c5a8e205272e3b382730be617fe to your computer and use it in GitHub Desktop.
import uuid from 'uuid/v4';
import mime from 'mime-types';
import { Storage } from '@google-cloud/storage';
import config from '../config';
exports.create = async (req, res, next) => {
const type = mime.lookup(req.file.originalname);
const storage = new Storage({
projectId: config.google.projectId,
keyFilename: './google.json',
});
const bucket = storage.bucket(config.google.bucket);
const blob = bucket.file(`${uuid()}.${mime.extensions[type][0]}`);
const stream = blob.createWriteStream({
resumable: true,
contentType: type,
predefinedAcl: 'publicRead',
});
stream.on('error', err => {
next(err);
});
stream.on('finish', () => {
res.status(200).json({
data: {
url: `https://storage.googleapis.com/${bucket.name}/${blob.name}`,
},
});
});
stream.end(req.file.buffer);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment