Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get buffer from File's download callback #676

Closed
munkhorgil opened this issue Apr 25, 2019 · 4 comments
Closed

Can't get buffer from File's download callback #676

munkhorgil opened this issue Apr 25, 2019 · 4 comments
Assignees
Labels
api: storage Issues related to the googleapis/nodejs-storage API. type: question Request for information or clarification. Not an issue.

Comments

@munkhorgil
Copy link

Environment details

  • OS: macOS
  • Node.js version: 8.10.0
  • npm version: 5.6.0
  • @google-cloud/storage version: ^2.5.0

I am trying to get file's buffer from google cloud storage, unfortunately there is nothing about buffer in documentation. However I found file api's download return buffer callback according to signatures of download function as follows.

    export declare type DownloadCallback = (err: RequestError | null, contents: Buffer) => void;  
    download(options: DownloadOptions, callback: DownloadCallback): void; 

callback supposed to return buffer as contents but it returns undefined and here is my code.

    return new Promise((resolve, reject) => {
      file.download(
        {
          destination: key
        },
        (err, response) => {
          if (err) {
            reject(err);
          }

          return resolve(response); // undefined
        }
      )
    });

And here is my workaround which is not efficient.

    const file = bucket.file(key);
    return rawBody(file.createReadStream());

How can I get a buffer from download function?

@munkhorgil munkhorgil changed the title Can't get buffer from File's download Can't get buffer from File's download callback Apr 25, 2019
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Apr 26, 2019
@jkwlui jkwlui added type: question Request for information or clarification. Not an issue. and removed triage me I really want to be triaged. labels Apr 26, 2019
@jkwlui
Copy link
Member

jkwlui commented Apr 29, 2019

If you're only interested in getting a buffer, then you can skip saving the file to your local file system if you omit the destination field in the options passed to File.download():

file.download({
  // don't set destination here
  }).then((data) => {
    const contents = data[0];  // contents is the file as Buffer
  });

Hope this answers your question. Please let us know if you are still running into issues.

@jkwlui jkwlui closed this as completed Apr 29, 2019
@munkhorgil
Copy link
Author

munkhorgil commented Apr 30, 2019

@jkwlui Thank you, it works.

@google-cloud-label-sync google-cloud-label-sync bot added the api: storage Issues related to the googleapis/nodejs-storage API. label Jan 31, 2020
@muescha
Copy link

muescha commented Feb 26, 2020

@jkwlui could you add this code snippet also to the docs? they are not in the examples.

@jayarjo
Copy link

jayarjo commented Jun 5, 2022

@jkwlui why 0 index is Buffer? Not intuitive at all. Could have been a buffer prop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/nodejs-storage API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

5 participants