Fireship · @fireship
Image Resize Cloud Function
Uploaded August 9, 2018 · Length 8:42
Summary
Build a cloud function with Firebase that resizes an image into multiple thumbnails after upload, optimizing performance and avoiding infinite loops.
- firebase cloud functions
- image resizing
- thumbnail generation
- cloud storage
- node js
- typescript
- sharp library
- google cloud storage
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:06
today we're going to build a cloud
- 0:08
function that's capable of resizing an
- 0:10
image into multiple thumbnails after its
- 0:13
initially uploaded this is one of the
- 0:14
most commonly needed features for a
- 0:16
cloud storage bucket but doing it
- 0:18
requires some pretty advanced no js'
- 0:20
code in today's video we'll build this
- 0:22
cloud function from scratch and do
- 0:23
everything we can to optimize
- 0:25
performance for the fastest possible
- 0:26
execution if you're new the channel like
- 0:29
and subscribe and if you want to get
- 0:30
really good at cloud functions consider
- 0:32
enrolling in my cloud functions master
- 0:34
course which was just released yesterday
- 0:36
the of course is free if you're already
- 0:38
a pro member but if not I'll go ahead
- 0:40
and leave a discount code in the video
- 0:41
description as well to get started I am
- 0:44
assuming that you have the firebase
- 0:45
command line tools installed then we can
- 0:47
run firebase and net functions in any
- 0:49
existing project I'm going to select
- 0:52
typescript here but you could also
- 0:53
select JavaScript but I would recommend
- 0:55
that you use the new node js8 runtime
- 0:57
because we'll be using a lot of
- 0:59
async/await syntax here and that's not
- 1:02
available in a default node 6 runtime
- 1:04
once that's done we can see the end of
- 1:06
the functions directory and then we have
- 1:08
a couple of dependencies to install here
- 1:10
the cloud functions environment by
- 1:12
default doesn't have tools set up for
- 1:15
you to download files from a storage
- 1:17
bucket but we can install the Google
- 1:19
Cloud Storage nodejs client to take care
- 1:21
of that for us when a storage cloud
- 1:24
function is triggered it doesn't give
- 1:26
you access directly to the file but
- 1:28
instead it just gives you some
- 1:29
information about the file that was
- 1:30
uploaded so we'll need the Google Cloud
- 1:32
Storage library to actually download it
- 1:35
and resize the image and then re-upload
- 1:37
those back to the bucket we're also
- 1:40
going to install a library called sharp
- 1:42
because it's four to five times faster
- 1:44
than the image magic library that's
- 1:45
installed in the cloud functions
- 1:47
environment and if you've ever used
- 1:49
image magic in a cloud function before
- 1:51
you'll know that it's pretty cumbersome
- 1:52
sharp is just way more
- 1:53
developer-friendly it's promise based
- 1:55
and we'll be able to do all of our
- 1:57
resizing with a single line of code
- 1:59
the last dependency I'll install is node


