Skip to content

Instantly share code, notes, and snippets.

@iwozzy
Last active February 16, 2022 04:13
Show Gist options
  • Save iwozzy/3de30701673cde47cbeca3635944d36a to your computer and use it in GitHub Desktop.
Save iwozzy/3de30701673cde47cbeca3635944d36a to your computer and use it in GitHub Desktop.
Google Cloud Storage with Node.js
var Promise = require('bluebird');
var GoogleCloudStorage = Promise.promisifyAll(require('@google-cloud/storage'));
var storage = GoogleCloudStorage({
projectId: 'PROJECT_ID',
keyFilename: 'keyfile.json'
})
var BUCKET_NAME = 'my-bucket'
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/0.39.0/storage/bucket
var myBucket = storage.bucket(BUCKET_NAME)
// check if a file exists in bucket
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/0.39.0/storage/file?method=exists
var file = myBucket.file('myImage.png')
file.existsAsync()
.then(exists => {
if (exists) {
// file exists in bucket
}
})
.catch(err => {
return err
})
// upload file to bucket
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/0.39.0/storage/bucket?method=upload
let localFileLocation = './public/images/zebra.gif'
myBucket.uploadAsync(localFileLocation, { public: true })
.then(file => {
// file saved
})
// get public url for file
var getPublicThumbnailUrlForItem = file_name => {
return `https://storage.googleapis.com/${BUCKET_NAME}/${file_name}`
}
@fizaict
Copy link

fizaict commented Mar 8, 2019

This error occurs after running your code

TypeError: Cannot promisify an API that has normal methods with 'Async'-suffix

Please help me to resolve the issue

@hddananjaya
Copy link

GG!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment