Giter Site home page Giter Site logo

storage-blob-resize-function-node's Introduction

page_type languages products description urlFragment
sample
javascript
azure
This sample implements a function triggered by Azure Blob Storage to resize an image in Node.js.
storage-blob-resize-function-node

Azure Storage Blob Trigger Image Resize Function in Node.js

This sample implements a function triggered by Azure Blob Storage to resize an image in Node.js. Once the image is resized, the thumbnail image is uploaded back to blob storage.

The key aspects of this sample are in the function bindings and implementation.

Function bindings

In order to interface with image data, you need to configure the function to process binary data.

The following code sets the datatype parameter to binary in the function.json file.

{
  "disabled": false,
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "myEvent",
      "direction": "in"
    },
    {
      "name": "myBlob",
      "type": "blob",
      "direction": "in",
      "path": "{data.url}",
      "connection": "AZURE_STORAGE_CONNECTION_STRING",
      "datatype": "binary"
    }
  ]
}

Function implementation

The sample uses Jimp to resize an incoming buffer to a thumbnail. The buffer is then converted to a stream (as required by createBlockBlobFromStream) and uploaded to Azure Storage.

const Jimp = require('jimp');
const stream = require('stream');
const { BlockBlobClient } = require("@azure/storage-blob");

const ONE_MEGABYTE = 1024 * 1024;
const uploadOptions = { bufferSize: 4 * ONE_MEGABYTE, maxBuffers: 20 };

const containerName = process.env.BLOB_CONTAINER_NAME;
const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING;
const blobName = process.env.OUT_BLOB_NAME;

module.exports = async function (context, myEvent, inputBlob){
    const widthInPixels = 100;
    Jimp.read(inputBlob).then((thumbnail) => {
        thumbnail.resize(widthInPixels, Jimp.AUTO);
        thumbnail.getBuffer(Jimp.MIME_PNG, async (err, buffer) => {
          
            const readStream = stream.PassThrough();
            readStream.end(buffer);
            const blobClient = new BlockBlobClient(connectionString, containerName, blobName);
            
            try {
                await blobClient.uploadStream(readStream,
                    uploadOptions.bufferSize,
                    uploadOptions.maxBuffers,
                    { blobHTTPHeaders: { blobContentType: "image/jpeg" } });
            } catch (err) {
                context.log(err.message);
            }
        });
    });
};

You can use the Azure Storage Explorer to view blob containers and verify the resize is successful.

storage-blob-resize-function-node's People

Contributors

craigshoemaker avatar ggailey777 avatar jongio avatar josuejoshua avatar karlerickson avatar microsoftopensource avatar msftgits avatar pramodvalavala-msft avatar v-jiaodi avatar v-rajagt-zz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

storage-blob-resize-function-node's Issues

[Information] Error: w and h must be numbers

I'm getting the following output:

2019-08-19T17:19:24.531 [Information] Executing 'Functions.PdfToTiffConverter' (Reason='EventGrid trigger fired at 2019-08-19T17:19:24.4859961+00:00', Id=161802cf-f5b9-4555-8e36-a9ca7e0daf91)
2019-08-19T17:20:26.112 [Information] Error: w and h must be numbers
at Jimp.throwError (D:\home\site\wwwroot\node_modules@jimp\utils\dist\index.js:26:13)
at Jimp.resize (D:\home\site\wwwroot\node_modules@jimp\plugin-resize\dist\index.js:38:36)
at Jimp.read.then (D:\home\site\wwwroot\PdfToTiffConverter\index.js:14:19)

I'm using this code:


const stream = require('stream');
const Jimp = require('jimp');

const storage = require('azure-storage');
const blobService = storage.createBlobService();

module.exports = (context, myEvent, myBlob) => {

  const widthInPixels = process.env.THUMBNAIL_WIDTH;
  const blobName = myEvent.subject.split('/')[6];

    Jimp.read(myBlob).then((thumbnail) => {

        thumbnail.resize(widthInPixels, Jimp.AUTO);

        thumbnail.getBuffer(Jimp.MIME_PNG, (err, buffer) => {

            const readStream = stream.PassThrough();
            readStream.end(buffer);

            blobService.createBlockBlobFromStream('thumbnails', blobName, readStream, buffer.length, (err) => {
                context.done();
            });
        });

    }).catch(context.log);
};

[Action Needed] This repo is inactive

This GitHub repository has been identified as a candidate for archival

This repository has had no activity for more than 2 years. Long periods of inactivity present security and code hygiene risks. Archiving will not prevent users from viewing or forking the code. A banner will appear on the repository alerting users that the repository is archived.

Please see https://aka.ms/sunsetting-faq to learn more about this process.

Action

✍️

❗**If this repository is still actively maintained, please simply close this issue. Closing an issue on a repository is considered activity and the repository will not be archived.🔒

If you take no action, this repository is still inactive 30 days from today it will be automatically archived..

Need more help? 🖐️

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.