0

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.

Zachary Newman
  • 20,014
  • 4
  • 39
  • 37
Squidinker
  • 169
  • 2
  • 14

1 Answers1

1

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.

Stephen
  • 5,710
  • 1
  • 24
  • 32
  • Steven, thanks for this - I actually just came back to write up my solution which is essentially what you put; calling download on a File object. Additionally, the files are JSON but came back with backslashes added - JSON.parse() was needed. Documentation is probably fine; I'm just not much of a programmer... – Squidinker Mar 16 '16 at 09:49