jone2
jone2

Reputation: 191

how to get the download url for image resized with node js in google cloud functions?

I am new to node js and google cloud functions. I am able to resize an image to generate a thumbnail. What I can't figure out is how to get the download url of the newly generated thumbnail. This is the code

   exports.generateThumbnail =  functions.storage.object('{pushId}/ProductImages').onFinalize((object) => {
 // [END generateThumbnailTrigger]
 // [START eventAttributes]
 const fileBucket = object.bucket; // The Storage bucket that contains the file.
 const filePath = object.name; // File path in the bucket.
 const contentType = object.contentType; // File content type.
 const resourceState = object.resourceState; // The resourceState is  'exists' or 'not_exists' (for file/folder deletions).
 const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.

console.log("this is the object ",object);
  // [END eventAttributes]

// [START stopConditions]

 if (!contentType.startsWith('image/')) {
  console.log('This is not an image.');
  return null;
  }


 const fileName = path.basename(filePath);

 if (fileName.startsWith('thumb_')) {

 return null;
 }
  // [END stopConditions]

  // [START thumbnailGeneration]
 // Download file from bucket.
  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = path.join(os.tmpdir(), fileName);
 // const tempFilePath1 = path.join(os.tmpdir(), fileName+"1");

 // var storageRef = firebase.storage.ref("folderName/file.jpg");
    const storageRef = event.data.ref.parent;
 // return storageRef.getDownloadURL().then(function(url) {

 // });

const metadata = {
 contentType: contentType,
};
return bucket.file(filePath).download({
destination: tempFilePath, 

 }).then(() => {
 console.log('Image downloaded locally to', tempFilePath);
// Generate a thumbnail using ImageMagick.
 return spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath] );
 }).then(() => {


const thumbFileName = `thumb_${fileName}`;
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);


// Uploading the thumbnail.
return bucket.upload(tempFilePath, {
destination: thumbFilePath,
metadata: metadata,
})
// Once the thumbnail has been uploaded delete the local file to free up disk space.
}).then(() => fs.unlinkSync(tempFilePath));
});

In javascript, the downloadurl for an image uploaded to firebase storage can be obtained with this code

   storageRef.put(file, metadata).then(function(snapshot) {

            var url = snapshot.downloadURL;
    })

How can I get the download url for the resized image in node js?

Upvotes: 0

Views: 1160

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317712

You need to use getSignedUrl() to generate a public URL. There is an example in the functions-samples repo in the generate-thumbnail example. Abbreviated:

const thumbFile = bucket.file(thumbFilePath);
const config = {
  action: 'read',
  expires: '03-01-2500',
};
thumbFile.getSignedUrl(config);  // returns a promise with results

You will have to pay attention to the README and initialize the admin SDK with a service account in order to use this method. Default init for Cloud Functions will not work.

Upvotes: 4

Related Questions