2

I am retriving a file from my bucket.

I get the file and want to read it's contents but I do not want to download it to my local project i just want to read the contents, take the data and do other operations with it.

my code:

export const fileManager = async () => {
  try {
    const source = 'upload/';

    const options = { prefix: source, delimiter: '/' };

    const remoteFile = st.bucket(bName).file('myData.csv');

    const readFileData;

    remoteFile
      .createReadStream()
      .on('error', err => {
        console.log('error');
      })
      .on('response', response => {
        readFileData = response;
        console.log('success');
        // Server connected and responded with the specified status and headers.
      })
      .on('end', () => {
        console.log('end');
        // The file is fully downloaded.
      });

console.log("data", readFileData)

  } catch (error) {
    console.log('Error Is', error);
  }
};

readFileData is undefined.

Is this possible? Every example I find envolves me downloading the file.

CodingLittle
  • 1,761
  • 2
  • 17
  • 44
  • Your puzzle is purely a logic typo ... in your code you have define readFileData as const and then later on you try and assign to it. That should fail. Change your variable to not be const and try again. – Kolban Dec 10 '19 at 22:08

1 Answers1

2

createReadStream is asynchronous and returns immediately. You have to use the callbacks to find out when the download to memory is complete. Right now, your code is always going to print "data undefined" because it's trying to print the response before it's available.

createReadStream is definitely the right way to go, but you will have to understand how node streams work in order to process the results correctly. There is a whole section in the linked documentation for reading streams, which is what you want to do here. The way you deal with the stream is not specific to Cloud Storage - it's the same for all node streams.

You might be helped by the answers to these questions that deal with reading node streams:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Great answer! Just to reconnect to something you said about it being async. I have also tried using the await to wait for the process to finish but im never inside of either success, error nor end, I tried the download methods and then i got the data but it is not the way I want to go. – CodingLittle Dec 10 '19 at 22:25
  • It's asynchronous, but it doesn't return a promise, so you can't `await` it, nor can you `then` or `catch` on it it. It's required to use the callbacks to determine progress, completion, and error cases. – Doug Stevenson Dec 10 '19 at 22:28
  • Delted my last comment since I was reading the articles you linked to and did not come to the answers you linke to. I added a new callback that listened for data and there I did get the buffer. My readFileData is still undefined and when logging it I can clearly see that it is undefined since the console.log(readFileData) occurs before the above process is finished. I suspect the answer to that lies somewhere in these links. A massive thank you and if you have the time would appreciate a small pointer to my final problem. – CodingLittle Dec 10 '19 at 22:47