node.jsgoogle-cloud-storagegcloud-node

Google Cloud Platform file to node server via gcloud


I have a bucket on Google Cloud Platform where part of my application adds small text files with unique names (no extension).

A second app needs to retrieve individual text files (only one at a time) for insertion into a template.

I cannot find the correct api call for this.

Configuration is as required:

var gcloud = require('gcloud');

var gcs = gcloud.storage({
    projectId: settings.bucketName,
    keyFilename: settings.bucketKeyfile 
});
var textBucket = gcs.bucket(settings.bucketTitle);

Saving to the bucket works well:

textBucket.upload(fileLocation, function(err, file) {
    if(err) {
        console.log("File not uploaded: " + err);
    } else {
        // console.log("File uploaded: " + file);
    }
});

The following seems logical but returns only metadata and not the actual file for use in the callback;

textBucket.get(fileName, function(err, file) {
    if(err) {
        console.log("File not retrieved: " + err);
    } else {
        callback(file);
    }
});

Probably no surprise this doesn't work since it's not actually in the official documentation but then again, neither is a simple asnyc function which returns a document you ask for.


Solution

  • The method get on a Bucket object is documented here: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.29.0/storage/bucket?method=get

    If you want to simply download the file into memory, try the method download on a File object: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.29.0/storage/file?method=download. You can also use createReadStream if using a stream workflow.

    If you have ideas for improving the docs, it would be great if you opened an issue on https://github.com/googlecloudplatform/gcloud-node so we can make it easier for the next person.