Created
January 22, 2019 02:47
-
-
Save astrotars/4eb23c5a8e205272e3b382730be617fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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