google-cloud-storage-standalone
v0.37.0
Published
Google Cloud Storage API Client Library for Node.js. Intended to be used as a standalone product.
Downloads
11
Maintainers
Readme
Google Cloud Node.js Client
Node.js client for Google Cloud Storage service. Intended for use with Meteor.
This client supports only Google Cloud Storage! If you need more APIs for the Google Cloud Platform, please check out the offical gcloud project
If you need support for other Google APIs, check out the Google Node.js API Client library.
Quick Start
$ npm install --save google-cloud-storage-standalone
Authentication
With google-cloud-storage-standalone
it's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Google Cloud services.
You need a Google Developers service account. To create a service account:
- Visit the Google Developers Console.
- Create a new project or click on an existing project.
- Navigate to APIs & auth > APIs section and turn on the following APIs (you may need to enable billing in order to use these services):
- Google Cloud Datastore API
- Google Cloud Storage
- Google Cloud Storage JSON API
- Navigate to APIs & auth > Credentials and then:
- If you want to use a new service account, click on Create new Client ID and select Service account. After the account is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests.
- If you want to generate a new key for an existing service account, click on Generate new JSON key and download the JSON key file.
// Authenticating on a global basis.
var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123'
var gcloud = require('gcloud')({
projectId: projectId,
keyFilename: '/path/to/keyfile.json'
});
// ...you're good to go! See the next section to get started using the APIs.
Google Cloud Storage
If you need more information for Google Cloud Storage, please refer to following docs
- Visit the Google Cloud Platform Docs
- Visit the Google Cloud Platform Docs for Google Storage
- Visit the Official Google Cloud Stoage Documentation
Preview
var fs = require('fs');
var gcloud = require('gcloud');
// Authenticating on a per-API-basis. You don't need to do this if you auth on a
// global basis (see Authentication section above).
var gcs = gcloud.storage({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
// Create a new bucket.
gcs.createBucket('my-new-bucket', function(err, bucket) {
if (!err) {
// "my-new-bucket" was successfully created.
}
});
// Reference an existing bucket.
var bucket = gcs.bucket('my-existing-bucket');
// Upload a local file to a new file to be created in your bucket.
bucket.upload('/photos/zoo/zebra.jpg', function(err, file) {
if (!err) {
// "zebra.jpg" is now in your bucket.
}
});
// Download a file from your bucket.
bucket.file('giraffe.jpg').download({
destination: '/photos/zoo/giraffe.jpg'
}, function(err) {});
// Streams are also supported for reading and writing files.
var remoteReadStream = bucket.file('giraffe.jpg').createReadStream();
var localWriteStream = fs.createWriteStream('/photos/zoo/giraffe.jpg');
remoteReadStream.pipe(localWriteStream);
var localReadStream = fs.createReadStream('/photos/zoo/zebra.jpg');
var remoteWriteStream = bucket.file('zebra.jpg').createWriteStream();
localReadStream.pipe(remoteWriteStream);
Contributing
Contributions to this library are always welcome and highly encouraged.
See CONTRIBUTING for more information on how to get started.
License
Apache 2.0 - See COPYING for more information.