1

I want to write a function that create a CSV file to save it in the Cloud Storage then return the download url to the frontend. After reading for 1 day there is no similar question as I want to:

exports.createCSV = functions.https.onRequest((req, res) => {
     return cors(req, res, async () => {
         let csv = ''
      const { location } = req.body.report
      const {
        postcode, streetAddress, city, state,
      } = location.address

      const header = []
      header.push('First Name')
      header.push('Last Name')
      header.push('Postcode')
      header.push('Street Address')
      header.push('City')
      header.push('State')
      header.push('Start')
      header.push('End')
      header.push('Status')
      csv += header.join(';')
      csv += '\n'

      await Promise.all(
          report.shifts.map(async shift => {
            const row = []

            row.push(firstName)
            row.push(lastName)
            row.push(postcode)
            row.push(streetAddress)
            row.push(city)
            row.push(state)
            row.push(shift.startStr)
            row.push(shift.endStr)
            row.push(shift.status)
            csv += row.join(';')
            csv += '\n'
            return null
          }),
        )
     })
})

But then how to save it in the Cloud Storage and get the download url is the problem to me at this point :(

I use the function as Frank below suggest:

await bucket.upload(tempLocalFile, {
destination: filePath,
metadata: {
  metadata: {
    ...metadata,
    firebaseStorageDownloadTokens: uuidv4(),
  },
},
})
const file = bucket.file(fileName);
file.getSignedUrl({
  action: "download",
  expires: "03-09-2491",
}).then((signedUrls) => {
  console.log("URL URL URL: ", signedUrls);
});

It still not working as Error return like

Error: The action is not provided or invalid. at File.getSignedUrl

Sy Quoc
  • 29
  • 1
  • 6
  • Are you asking [how to upload a file to Cloud Storage from Cloud Functions](https://www.google.com/search?q=how+to+upload+a+file+to+Cloud+Storage+from+Cloud+Functions+site:stackoverflow.com)? Or [how to get a download URL in Cloud Functions](https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-firebase)? – Frank van Puffelen Sep 24 '21 at 14:26
  • But it's not working. Seems like firebase cloud and google-cloud are 2 different things – Sy Quoc Sep 24 '21 at 16:02
  • The Firebase Admin SDK provides a very thin wrapper around the GCP Node SDK for Cloud Storage, mostly initializing the `bucket` with the right values. Your `file` should just be a `File` object, as shown here: https://googleapis.dev/nodejs/storage/latest/File.html#getSignedUrl. Also note that the question I linked has a second, highly upvoted answer that shows how to get a proper download URL: https://stackoverflow.com/a/43764656 – Frank van Puffelen Sep 24 '21 at 19:00
  • Thank you. The URL: https://firebasestorage.googleapis.com/v0/b/[BUCKET_NAME]/o/[FILE_PATH]?alt=media&token=[THE_TOKEN_YOU_CREATED] seems like correct than anything else as: const res = await bucket.upload(......) res.mediaLink() // not working res.getDownloadURL() // not working res.getSignedUrl() // not working – Sy Quoc Sep 25 '21 at 06:28
  • The answer I linked doesn't call `getDownloadURL` anywhere, as that method doesn't exist in the server-side SDKs. – Frank van Puffelen Sep 25 '21 at 15:32

0 Answers0