0

Is it possible to create a JSON file from Cloud Function and save it to Cloud Storage? Answer: Yes if so How do we achieve that? Answer: Using dataStream.pipe see below an example

dataStream.pipe example:

  var myJSON = JSON.stringify(Row);

  const stream = require('stream'),
    dataStream = new stream.PassThrough(),
    gcFile     = bucket.file("Leaderboard/users.json");

  dataStream.push(myJSON);
  dataStream.push(null);

  await new Promise((resolve, reject) => {
      dataStream.pipe(gcFile.createWriteStream({
        resumable  : false,
        validation : false,
        metadata   : {'Cache-Control': 'public, max-age=31536000'}
      }))
      .on('error', (error) => { 
        reject(error) 
      })
      .on('finish', () => { 
        resolve(true)
      })
  }) 

Thanks,

  • Your question is very broad, but your error message is very specific. I suggest editing your question to focus on the error message rather than broad issues like "is it possible". You will want to examine what specifically you're getting for the `storage` variable. – Doug Stevenson Aug 01 '20 at 18:39
  • I have edited my question. in other words, what I want is to create a JSON file from my array on Cloud function and add it to the storage. From there on unity, i will get that JSON files. – Sebastien Lachapelle Aug 01 '20 at 19:40
  • I suggest removing your first question altogether and just focus on the code at hand. "Is it possible" type questions are too broad and unspecific, as the answer is technically only "yes" or "no". It's definitely possible to do what you're suggesting. – Doug Stevenson Aug 01 '20 at 20:00
  • I changed my Question again :D – Sebastien Lachapelle Aug 01 '20 at 20:34
  • I think you're missing the point of my comments. You've now made the question so broad that it's no longer on-topic for Stack Overflow. You were better off showing the code that isn't working the way you expect, and explaining what it was supposed to, showing clearly where any errors are. As I said, it **is** possible to do what you're saying, and you certainly had code started to make that work. The issue is figuring out what the error message means, which Frank is helping you with in his answer. – Doug Stevenson Aug 01 '20 at 20:39
  • broad? Doug, I just want to know if it's possible to create a JSON file from Cloud Function and save it to Cloud Storage? and if you tell me Yes Sebastien it is possible, then I want to know how to do it. I put back the code to show you how I try and fail to make a JSON file and save it to Cloud storage. Forget the error. – Sebastien Lachapelle Aug 01 '20 at 20:52
  • Why do you want to store JSON data in Cloud **Storage**? That's not what [Cloud Storage](https://firebase.google.com/docs/storage) is really for - you would be better off storing JSON data in [Firestore](https://firebase.google.com/docs/firestore). Can you clarify what you're asking and what the use case is? – Jay Aug 02 '20 at 13:44

1 Answers1

0

When you access admin.storage(), you are not using Firebase's regular JavaScript SDK for Storage, but actually the Node.js SDK for Cloud Storage. So the API is different, and it doesn't have a ref() function.

For some examples of how to upload a file using the Node.js SDK, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Ok now that I have my bucket link, how do I upload a JSON file? – Sebastien Lachapelle Aug 01 '20 at 19:52
  • You call `upload` on the bucket, as shown in the first two links in my answer, and many more if you search for https://www.google.com/search?q=upload+a+file+to+cloud+storage+using+the+Node.js+SDK – Frank van Puffelen Aug 01 '20 at 20:10
  • Yeah, but how do we upload something that I did not store in my C:/ drive or elsewhere? the guy says `bucket.upload('Local file to upload, e.g. ./local/path/to/file.txt')` but I don't want to upload a local file. What I want is: Cloud function to create a JSON file then save it to the Storage. Sorry for my incompetence but I really need your help. – Sebastien Lachapelle Aug 01 '20 at 20:32
  • You can either write the data to the local disk first and use `upload`, or write it straight from memory as for example shown here: https://stackoverflow.com/questions/44945376/how-to-upload-an-in-memory-file-data-to-google-cloud-storage-using-nodejs and https://stackoverflow.com/questions/57927063/node-js-cloud-function-stream-csv-data-directly-to-google-cloud-storage-file (with an answer from Doug) – Frank van Puffelen Aug 01 '20 at 21:02