Last active
February 16, 2022 04:13
-
-
Save iwozzy/3de30701673cde47cbeca3635944d36a to your computer and use it in GitHub Desktop.
Google Cloud Storage with Node.js
This file contains hidden or 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
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}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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