0

I would like to implement the following process on cloud functions (TypeScript).

  1. Get the image by URL
  2. Save image to Cloud Storage
  3. Get URL of the image saved in the firestore

I am trying to move it with the following code, there is 2 problem.

  • It takes a long time to save the data (about 5 minutes)
  • URL is not issued to the saved image

Code:

const bucket = admin.storage().bucket();
const url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';

request(
  { method: 'GET', url: url, encoding: null },
  async function (error, response, body) {
    if (!error && response.statusCode === 200) {
      const file = bucket.file('test/test.png');

      const metadata = {
        contentType: 'image/png'
      };
      try {
        await file.save(body, metadata);
      } catch (err) {
        console.log(err);
      }
    }
  }
);

It would be greatly appreciated if you could explain the details.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Please edit the question to show the complete code of the function. What you have now lacks a function definition. – Doug Stevenson Jun 04 '20 at 02:54
  • I think you mean to say "Cloud Storage" instead of "Firestore". – Doug Stevenson Jun 04 '20 at 02:55
  • Does this answer your question? [Get Download URL from file uploaded with Cloud Functions for Firebase](https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-firebase) – Happy-Monad Jun 04 '20 at 14:22

1 Answers1

1

You should probably understand that when the URL is set to the saved image it defaults to a different one that Firebase issues it. You should probably look at this documentation to see how to get that URL: https://firebase.google.com/docs/storage/web/download-files. A way you could get the link (that you issued for it) onto the file, you should add it to the metadata like this:

const bucket = admin.storage().bucket();
const url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';

request(
  { method: 'GET', url: url, encoding: null },
  async function (error, response, body) {
    if (!error && response.statusCode === 200) {
      const file = bucket.file('test/test.png');

      const metadata = {
        contentType: 'image/png',
        url // <- add url here
      };
      try {
        await file.save(body, metadata);
      } catch (err) {
        console.log(err);
      }
    }
  }
);

Also the reason it may be taking so long is because getting the image and then adding the image to Firebase.

tillsanders
  • 1,656
  • 1
  • 17
  • 29