Last active
February 21, 2017 16:10
-
-
Save Karolass/7842d773d976c061e3addcf9ef7f8fb4 to your computer and use it in GitHub Desktop.
Upload to Google Cloud Storage
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
npm install --save google-cloud | |
# or | |
# npm install --save @google-cloud/storage |
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
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 | |
} |
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
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