Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save latusikl/d88e9db5cdaff5703f1dfa58ea9c07b9 to your computer and use it in GitHub Desktop.
Save latusikl/d88e9db5cdaff5703f1dfa58ea9c07b9 to your computer and use it in GitHub Desktop.
Perform Foundational Infrastructure Tasks in Google Cloud: Challenge Lab

Task 1: Create a bucket

gsutil mb -p qwiklabs-gcp-04-ffed8baf5001 -l us-east1 gs://<YOUR_BUCKET_NAME>

Task 2: Create a Pub/Sub topic

gcloud pubsub topic create kraken-topic

Task 3: Create the thumbnail Cloud Function

  • Execute following commands
mkdir gcf
cd gcf
touch index.js
  • Using your favourite editor paste following content to file index.js

/* globals exports, require */
//jshint strict: false
//jshint esversion: 6
"use strict";
const crc32 = require("fast-crc32c");
const gcs = require("@google-cloud/storage")();
const PubSub = require("@google-cloud/pubsub");
const imagemagick = require("imagemagick-stream");

exports.thumbnail = (event, context) => {
  const fileName = event.name;
  const bucketName = event.bucket;
  const size = "64x64"
  const bucket = gcs.bucket(bucketName);
  const topicName = "kraken-topic";
  const pubsub = new PubSub();
  if ( fileName.search("64x64_thumbnail") == -1 ){
    // doesn't have a thumbnail, get the filename extension
    var filename_split = fileName.split('.');
    var filename_ext = filename_split[filename_split.length - 1];
    var filename_without_ext = fileName.substring(0, fileName.length - filename_ext.length );
    if (filename_ext.toLowerCase() == 'png' || filename_ext.toLowerCase() == 'jpg'){
      // only support png and jpg at this point
      console.log(`Processing Original: gs://${bucketName}/${fileName}`);
      const gcsObject = bucket.file(fileName);
      let newFilename = filename_without_ext + size + '_thumbnail.' + filename_ext;
      let gcsNewObject = bucket.file(newFilename);
      let srcStream = gcsObject.createReadStream();
      let dstStream = gcsNewObject.createWriteStream();
      let resize = imagemagick().resize(size).quality(90);
      srcStream.pipe(resize).pipe(dstStream);
      return new Promise((resolve, reject) => {
        dstStream
          .on("error", (err) => {
            console.log(`Error: ${err}`);
            reject(err);
          })
          .on("finish", () => {
            console.log(`Success: ${fileName} → ${newFilename}`);
              // set the content-type
              gcsNewObject.setMetadata(
              {
                contentType: 'image/'+ filename_ext.toLowerCase()
              }, function(err, apiResponse) {});
              pubsub
                .topic(topicName)
                .publisher()
                .publish(Buffer.from(newFilename))
                .then(messageId => {
                  console.log(`Message ${messageId} published.`);
                })
                .catch(err => {
                  console.error('ERROR:', err);
                });

          });
      });
    }
    else {
      console.log(`gs://${bucketName}/${fileName} is not an image I can handle`);
    }
  }
  else {
    console.log(`gs://${bucketName}/${fileName} already has a thumbnail`);
  }
};

  • Create package.json file
touch package.json
  • Using your favourite editor paste content to package.json
{
  "name": "thumbnails",
  "version": "1.0.0",
  "description": "Create Thumbnail of uploaded image",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "@google-cloud/storage": "1.5.1",
    "@google-cloud/pubsub": "^0.18.0",
    "fast-crc32c": "1.0.4",
    "imagemagick-stream": "4.1.1"
  },
  "devDependencies": {},
  "engines": {
    "node": ">=4.3.2"
  }
}
  • Execute following command in cloud shell.
gcloud functions deploy thumbnail-gcf \
--trigger-bucket=<YOUR_BUCKET_NAME> \
--runtime=nodejs10 \
--region=us-east1 \
--entry-point=thumbnail
  • Check if function was deployed.
gcloud functions describe thumbnail-gcf
  • Verify if cloud function works.
cd ~
wget --output-document=map.jpg https://storage.googleapis.com/cloud-training/gsp315/map.jpg
gsutil cp map.jpg gs://<YOUR_BUCKET_NAME>/
  • Check if thumbnail was created.
gsutil ls gs://<YOUR_BUCKET_NAME>/

Task 4: Remove the previous cloud engineer.

  • Using user inferface (Console):
  1. Navigation menu > IAM & Admin > IAM.
  2. Click the pencil icon next to username (listed as Username2 in Challenge Lab).
  3. Click on the bin to remove Viever permissions.
  4. Save changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment