Cloud Storage
Simple wrapper for uploading and deleting files from Google Cloud Storage.
Thanks to @bsphere for https://github.com/bsphere/node-gcs
Installation
npm install cloud-storage
Getting Started
-
Sign into the Google Cloud Console site: https://console.developers.google.com
-
Go to your project and under APIs & auth click on Credentials
-
Create an Oauth Service Account for your project if you don't already have one
-
Under the Service Account section copy your email address (that is your accessId
)
-
If you do not have a private key, click Generate new key
to generate one (this will download a .p12 file to your computer)
-
Convert the key to a .pem file
openssl pkcs12 -in path/to/key.p12 -nodes -nocerts > path/to/key.pem
-
If prompted for a password enter notasecret
-
Store this .pem file somewhere secret (the path to this file is your privateKey
)
Usage
Creating a cloud storage object
var CloudStorage = require('cloud-storage');
var storage = new CloudStorage({
accessId: '1234-abcd@developer.gserviceaccount.com',
privateKey: '/path/to/private/key.pem'
});
Copying a file to cloud storage
storage.copy('/path/to/something.jpg', 'gs://some-bucket/something.jpg', function(err, url) {
console.log(url);
});
Deleting a file from cloud storage
storage.remove('gs://some-bucket/something.jpg', function(err, success) {
console.log(success);
});
Custom options and metadata
var options = {
headers: {
'Cache-Control': 'public, max-age=7200, no-transform',
'X-Goog-Acl': 'bucket-owner-full-control'
},
metadata: {
'width': 100,
'height': 100
},
removeAfterCopy: true,
forceExtension: true
};
storage.copy('http://someurl.com/path/to/file.jpg', 'gs://some-bucket/images/file', options, function(err, url) {
});
Get a url for a file
var url = storage.getUrl('gs://some-bucket/images/file.jpg')
var options = {
expiration: 100,
download: true
};
var signedUrl = storage.getSignedUrl('gs://some-bucket/images/file.jpg', options)