Skip to content

Instantly share code, notes, and snippets.

@Karolass
Last active February 21, 2017 16:10
Show Gist options
  • Save Karolass/7842d773d976c061e3addcf9ef7f8fb4 to your computer and use it in GitHub Desktop.
Save Karolass/7842d773d976c061e3addcf9ef7f8fb4 to your computer and use it in GitHub Desktop.
Upload to Google Cloud Storage
npm install --save google-cloud
# or
# npm install --save @google-cloud/storage
const gcloud = require('google-cloud')
const gcs = gcloud.storage({
projectId: '<YOUR_PROJECT_ID>',
keyFilename: '/path/to/key.json'
})
// or
/*
const gcs = require('@google-cloud/storage')({
projectId: '<YOUR_PROJECT_ID>',
keyFilename: '/path/to/key.json'
})
*/
const fireBaseBucket = '<YOUR_FILEBASE_PROJECT_ID>.appspot.com'
const bucket = gcs.bucket(fireBaseBucket)
function upload(filePath, fireBaseFolder, fireBaseFileName) {
return new Promise(function(resolve, reject) {
const option = {
destination: `${fireBaseFolder}/${fireBaseFileName}`,
public: true
}
bucket.upload(filePath, option, function(err) {
if (err) {
reject(err)
} else {
resolve(`http://storage.googleapis.com/${fireBaseBucket}/${fireBaseFolder}/${fireBaseFileName}`)
}
})
})
}
module.exports = {
upload: upload
}
const gcs = require('./gcs.js')
gcs.upload('/path/to/image.png', '<STORAGE_FOLDER>', '<FILE_NAME>')
.then(function(fireBaseURL) {
console.log(fireBaseURL)
})
.catch(function(err) {
console.log(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment