Skip to content

add download() method #367

Description

@nicolabortignon

Hi,
I spent few hours looking into the code, but didn't find the solution.
What am i suppose to do in order to load the content of a file from a createReadStream into a variable ?
In the example there is only a read-to-pipe-to-write example.

I just want to do something like

var streamPointer = cloudstorage.createReadStream(myTextFile)
    streamPointer.on('complete', function(res) {
            myVar = res
        })

Activity

  1. added
    api: storageIssues related to the Cloud Storage API.
    type: questionRequest for information or clarification. Not an issue.
    on Feb 2, 2015
  2. added this to the Storage Stable milestone on Feb 2, 2015
  3. stephenplusplus commented on Feb 2, 2015

    @stephenplusplus
    Contributor

    Sorry, it appears our docs are lacking on showing how to simply download a file. Here's the current way to get a file's contents:

    var gcloud = require("gcloud")({ /* credentials. */ });
    var cloudstorage = gcloud.storage();
    var myTextFile = cloudstorage.file('your-file.txt');
    
    var fileContents = new Buffer('');
    
    myTextFile.createReadStream()
      .on('data', function(chunk) {
        fileContents = Buffer.concat([fileContents, chunk]);
      })
      .on('complete', function() {
        // `fileContents` is ready
      });

    @ryanseys thoughts on adding Bucket#download to make this easier?

  4. ryanseys commented on Feb 2, 2015

    @ryanseys
    Contributor

    @stephenplusplus That sounds like a good idea. I had an idea for a similar thing that would immediately write to a given filename as well for those wanting to just download a bunch of files. Perhaps we could support both of these options either as separate methods or as an option?

  5. stephenplusplus commented on Feb 2, 2015

    @stephenplusplus
    Contributor

    Something like:

    myBucket.download("my-file.txt", function (err, contents, file) {
      // contents === file's contents
      // file === myBucket.file("my-file.txt")
    })
    
    // and...
    
    myBucket.download("my-file.txt", { destination: "/Users/stephen/Desktop/local-copy.txt" }, function (err, contents, file) {
      // contents === file's contents
      // file === myBucket.file("my-file.txt")
    })
    
    // and we can support a File object as a source:
    myBucket.download(myBucket.file("my-file.txt"), //...

    ?

  6. nicolabortignon commented on Feb 2, 2015

    @nicolabortignon
    Author

    I would say both the .read and .download as wraps for the asynchronous readStream and Pipe, would add a really appreciated level of simplification.

    Also, just as overall idea, would be nice to align the interface of the CreateReadStream to the standard (fs) one.
    Thanks for the answer!

  7. changed the title [-]buffer of a createReadStream into a variable[/-] [+]add download() method[/+] on Feb 4, 2015
  8. ryanseys commented on Feb 4, 2015

    @ryanseys
    Contributor

    If we are downloading, wouldn't we just download from a Bucket#file instance? And if we just want to immediately write to a file without storing in memory, that'd be cool... I don't know if we should use two separate methods for that or if we can get away with just one like below:

    myFile.download('./local/path/image.jpg', function(err, contents) {
      // don't consume memory
      // contents = null
    });
    
    myFile.download(function(err, contents) {
      // consume memory
      // contents = contents of file
    });
  9. stephenplusplus commented on Feb 4, 2015

    @stephenplusplus
    Contributor

    PR sent: #381

  10. 21 remaining items

  11. added a commit that references this issue on Mar 27, 2026
  12. added a commit that references this issue on May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: storageIssues related to the Cloud Storage API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions