Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have access to GraphicsMagicks or ImageMagicks in a Google Cloud Function?

I want to make a Google Cloud Function that will correctly set the content type of uploaded files. I know how to do this with GraphicsMagick or ImageMagick, but I'm not sure if Google Cloud Function has those native libraries. How do I find out if they have them or failing that how do I get them installed?

like image 703
krainboltgreene Avatar asked Mar 27 '17 00:03

krainboltgreene


People also ask

How do I give permission to Cloud Functions?

You can set access control using roles at the project level. Grant a role to a project member or service account to determine the level of access to your Google Cloud project and its resources. By default, all Cloud projects come with a single user: the original project creator.

How Google cloud functions work?

With Cloud Functions, you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your Cloud Function is triggered when an event being watched is fired. Your code executes in a fully managed environment.

Are Google cloud functions secure?

Google Cloud Functions provide a mechanism to secure each of your endpoint to permitted users only, namely remove allUsers and allAuthenticatedUsers in the IAM. For service to service communication you can use service account to generate token when you want to create a request to the Google Cloud Functions endpoint.


1 Answers

Google Cloud Functions run in a container that has ImageMagick installed. Somehow the Firebase documentation seems to have best documentation for it. From there:

Cloud Functions provides an image-processing program called ImageMagick that can perform manipulations on graphical image files. The following is an example of how to create a thumbnail image for an uploaded image file:

    // Download file from bucket.
    const bucket = gcs.bucket(fileBucket);
    const tempFilePath = `/tmp/${fileName}`;
    return bucket.file(filePath).download({
      destination: tempFilePath
    }).then(() => {
      console.log('Image downloaded locally to', tempFilePath);
      // Generate a thumbnail using ImageMagick.
      return exec(`convert "${tempFilePath}" -thumbnail '200x200>' "${tempFilePath}"`).then(() => {
        console.log('Thumbnail created at', tempFilePath);
        // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
        const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, `$1thumb_$2`);
        // Uploading the thumbnail.
        return bucket.upload(tempFilePath, {
          destination: thumbFilePath
        });
      });
    });

This code executes the ImageMagick command line program convert to create a 200x200 thumbnail for the image saved in a temporary directory, then uploads it back to Cloud Storage.

Also see the Firebase functions-sample repo for an example of how to use it: https://github.com/firebase/functions-samples/tree/master/generate-thumbnail

like image 123
Frank van Puffelen Avatar answered Oct 18 '22 22:10

Frank van Puffelen