@imageproc/cloud-functions
v0.0.15
Published
@imageproc/cloud-functions
Downloads
5
Readme
@imageproc/cloud-functions
Process images on Google Cloud Storage when images are created.
How to use?
- Prepare your Google Cloud Functions directory.
functions
|- package.json
|- index.js
|- .gcloudignore
...
- Install @imageproc/cloud-functions
npm install @imageproc/cloud-functions
- Handle stroage trigger
You can use handleStorageObjectCreated(params: InitParams)
to initialize trigger. The followings are the property of InitParams:
- sourceBucket?: string
- Source bucket of images
- This is optional property. If not set, use trigger bucket as the source bucket.
- destBucket?: string
- Destination bucket of generated images
- This is optional property. If not set, use trigger bucket as the source bucket.
- ignoreNamePattern?: RegExp
- Pattern of object name to be ignored.
- If sourceBucket and destBucket is same, this property is REQUIRED to avoid infinite loop processing.
- destNamePrefix?: string
- Prefix of destination object name
- destNameTransform?:
(sourceName: string) => void
- Function to transfrom object name
- If this property is set, destNamePrefix property will be ignored.
- operation: Operation
- Type of image processing
- name:
resizeAspectFit
- params: ResizeAspectFitParams
- name:
resizeCrop
- params: ResizeCropParams
- name:
convertFormat
- params: ConvertFormatParams
Examples:
- Using event target bucket for both source and destination.
const imageproc = require("@imageproc/cloud-functions");
// foo.jpg => files/foo.jpg
exports.processImage = imageproc.handleStorageObjectCreated({
ignorePattern: /^files\//,
destNamePrefix: "files/",
opration: {
name: "resizeAspectFit",
params: {
maxWidth: 2000,
maxHeight: 2000,
noScaleUp: true
}
}
});
- Using other buckets for source and destination.
const imageproc = require("@imageproc/cloud-functions");
exports.processImage = imageproc.handleStorageObjectCreated({
sourceBucket: "source-image",
destBucket: "dest-images",
...
});
- Deploy
gcloud functions deploy processImage \
--runtime nodejs10 \
--trigger-resource YOUR_BUCKET \
--trigger-event google.storage.object.finalize \
--memory 2048MB \ # recommended
--project YOUR_PROJECT