Accessing blobs from specific directory inside container using event grid trigger

Published Jan 05 2021 04:45 AM 1,296 Views
Microsoft

Storage Accounts with Data lake Storage gen2 enabled allows us to create directories within container.

When we use When a blob is added or modified (properties only) trigger. It works fine for destination where we are looking for blobs inside containers but not inside any directories.

 

For example:

  • It works for these blobs which are directly inside container.

     pic1.PNG

  • It won’t work for these blobs which are present inside directory in container.

        pic2.PNG

 

Even though we choose the location of this directory for When a blob is added or modified (properties only) trigger. The trigger won’t get fired when new blob is created or modified inside directory.

 

 

To overcome this, we can use event grid trigger When a resource event occurs.

Make sure Microsoft.EventGrid is registered under resource providers in subscription.

 

pic3.PNG

 

This is webhook type trigger and it registers a webhook with the endpoint. In this case it will be a specified path inside container of a storage account.

  • In Event Type we can choose from the given list.
  • We need to add a new parameter named prefix filter.
  • if we want to register a webhook with any container we can give:  /blobServices/default/containers/containerName inside prefix filter.
  • If we want to give path of specific directory, we need to append /blobs after container name and then directory names after /blobs.

For example:

  • for a directory named folder1 inside container container1 the path would be:

         /blobServices/default/containers/container1/blobs/folder1/

 

Note: the last forward slash ‘/’ after directory name is very important, otherwise you would not get the desired result.

 

Getting the content of those blobs:

For getting the content inside blobs we can use either HTTP action or get blob content (using path) action.

This is the data object returned after trigger got fired.

 

pic4.PNG

 

"data": {

    "api": "PutBlob",

    "clientRequestId": "clientRequestIdValue",

    "requestId": "requestIdValue",

    "eTag": "eTagValue",

    "contentType": "text/plain",

    "contentLength": 6,

    "blobType": "BlockBlob",

    "blobUrl": "https://storageAccountName.blob.core.windows.net/containerName/folderName/fileNameWithExtension",

    "url": "https://storageAccountName.blob.core.windows.net/containerName/folderName/fileNameWithExtension",

    "sequencer": "sequencerValue",

    "storageDiagnostics": {

      "batchId": "batchIdValue"

    }

  }

 

We can use either blobUrl or url element from data value to retrieve the path for getting blob content.

 

  • We can use managed identity for HTTP action to get the content from URL.
 

pic5.PNG

 

Expression Inside URI :  triggerBody()?['data']?['blobUrl']

You can also use below expression to frame audience from url element of data object.

concat(split(triggerBody()?['data']?['url'],'net')[0],'net')

 

  • Using get blob content :
 

        pic6.PNG

 

Expression inside Blob: split(triggerBody()?['data']?['blobUrl'],'.net/')[1]

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2031881%22%20slang%3D%22en-US%22%3EAccessing%20blobs%20from%20specific%20directory%20inside%20container%20using%20event%20grid%20trigger%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2031881%22%20slang%3D%22en-US%22%3E%3CP%3EStorage%20Accounts%20with%20%3CSTRONG%3EData%20lake%20Storage%20gen2%20%3C%2FSTRONG%3Eenabled%20allows%20us%20to%20create%20directories%20within%20container.%3C%2FP%3E%0A%3CP%3EWhen%20we%20use%20%3CSTRONG%3EWhen%20a%20blob%20is%20added%20or%20modified%20(properties%20only)%20%3C%2FSTRONG%3Etrigger.%20It%20works%20fine%20for%20destination%20where%20we%20are%20looking%20for%20blobs%20inside%20containers%20but%20not%20inside%20any%20directories.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EFor%20example%3A%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIt%20works%20for%20these%20blobs%20which%20are%20directly%20inside%20container.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pic1.PNG%22%20style%3D%22width%3A%20433px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244223i11E8958C8F84CE15%2Fimage-dimensions%2F433x214%3Fv%3D1.0%22%20width%3D%22433%22%20height%3D%22214%22%20role%3D%22button%22%20title%3D%22pic1.PNG%22%20alt%3D%22pic1.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIt%20won%E2%80%99t%20work%20for%20these%20blobs%20which%20are%20present%20inside%20directory%20in%20container.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pic2.PNG%22%20style%3D%22width%3A%20571px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244225i9C076B055317921D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22pic2.PNG%22%20alt%3D%22pic2.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEven%20though%20we%20choose%20the%20location%20of%20this%20directory%20for%20%3CSTRONG%3EWhen%20a%20blob%20is%20added%20or%20modified%20(properties%20only)%20%3C%2FSTRONG%3Etrigger.%20The%20trigger%20won%E2%80%99t%20get%20fired%20when%20new%20blob%20is%20created%20or%20modified%20inside%20directory.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20overcome%20this%2C%20we%20can%20use%20event%20grid%20trigger%20%3CSTRONG%3EWhen%20a%20resource%20event%20occurs%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3EMake%20sure%20%3CSTRONG%3EMicrosoft.EventGrid%20%3C%2FSTRONG%3Eis%20registered%20under%20resource%20providers%20in%20subscription.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorpuspraj_9%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pic3.PNG%22%20style%3D%22width%3A%20781px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244241iE12E6A14FEDE6711%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22pic3.PNG%22%20alt%3D%22pic3.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20webhook%20type%20trigger%20and%20it%20registers%20a%20webhook%20with%20the%20endpoint.%20In%20this%20case%20it%20will%20be%20a%20specified%20path%20inside%20container%20of%20a%20storage%20account.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIn%20%3CSTRONG%3EEvent%20Type%3C%2FSTRONG%3E%20we%20can%20choose%20from%20the%20given%20list.%3C%2FLI%3E%0A%3CLI%3EWe%20need%20to%20add%20a%20new%20parameter%20named%20%3CSTRONG%3Eprefix%20filter.%20%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3Eif%20we%20want%20to%20register%20a%20webhook%20with%20any%20container%20we%20can%20give%3A%26nbsp%3B%20%3CFONT%20color%3D%22%230000FF%22%3E%2FblobServices%2Fdefault%2Fcontainers%2F%3CSTRONG%3EcontainerName%20%3C%2FSTRONG%3E%3C%2FFONT%3Einside%20prefix%20filter.%3C%2FLI%3E%0A%3CLI%3EIf%20we%20want%20to%20give%20path%20of%20specific%20directory%2C%20we%20need%20to%20append%20%3CSTRONG%3E%2Fblobs%20%3C%2FSTRONG%3Eafter%20container%20name%20and%20then%20directory%20names%20after%20%3CSTRONG%3E%2Fblobs.%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3EFor%20example%3A%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Efor%20a%20directory%20named%20%3CSTRONG%3Efolder1%20%3C%2FSTRONG%3Einside%20container%20%3CSTRONG%3Econtainer1%20%3C%2FSTRONG%3Ethe%20path%20would%20be%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2FblobServices%2Fdefault%2Fcontainers%2Fcontainer1%2Fblobs%2Ffolder1%2F%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20the%20last%20forward%20slash%20%E2%80%98%2F%E2%80%99%20after%20directory%20name%20is%20very%20important%2C%20otherwise%20you%20would%20not%20get%20the%20desired%20result.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGetting%20the%20content%20of%20those%20blobs%3A%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EFor%20getting%20the%20content%20inside%20blobs%20we%20can%20use%20either%20%3CSTRONG%3EHTTP%20action%3C%2FSTRONG%3E%20or%20%3CSTRONG%3Eget%20blob%20content%20(using%20path)%3C%2FSTRONG%3E%20action%3CSTRONG%3E.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThis%20is%20the%20data%20object%20returned%20after%20trigger%20got%20fired.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorpuspraj_10%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pic4.PNG%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244227i678DC5010E6F8BE5%2Fimage-dimensions%2F400x402%3Fv%3D1.0%22%20width%3D%22400%22%20height%3D%22402%22%20role%3D%22button%22%20title%3D%22pic4.PNG%22%20alt%3D%22pic4.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%22data%22%3A%26nbsp%3B%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22api%22%3A%26nbsp%3B%22PutBlob%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22clientRequestId%22%3A%26nbsp%3B%22clientRequestIdValue%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22requestId%22%3A%26nbsp%3B%22requestIdValue%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22eTag%22%3A%26nbsp%3B%22eTagValue%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22contentType%22%3A%26nbsp%3B%22text%2Fplain%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22contentLength%22%3A%26nbsp%3B6%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22blobType%22%3A%26nbsp%3B%22BlockBlob%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22blobUrl%22%3A%26nbsp%3B%22%3CFONT%20color%3D%22%23FF6600%22%3E%3CA%20href%3D%22https%3A%2F%2FstorageAccountName.blob.core.windows.net%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2FstorageAccountName.blob.core.windows.net%3C%2FA%3E%3C%2FFONT%3E%2F%3CFONT%20color%3D%22%230000FF%22%3EcontainerName%2FfolderName%2FfileNameWithExtension%3C%2FFONT%3E%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22url%22%3A%26nbsp%3B%22%3CA%20href%3D%22https%3A%2F%2FstorageAccountName.blob.core.windows.net%2FcontainerName%2FfolderName%2FfileNameWithExtension%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2FstorageAccountName.blob.core.windows.net%2FcontainerName%2FfolderName%2FfileNameWithExtension%3C%2FA%3E%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22sequencer%22%3A%26nbsp%3B%22sequencerValue%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22storageDiagnostics%22%3A%26nbsp%3B%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%22batchId%22%3A%26nbsp%3B%22batchIdValue%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20can%20use%20either%20%3CSTRONG%3EblobUrl%3C%2FSTRONG%3E%20or%20%3CSTRONG%3Eurl%3C%2FSTRONG%3E%20element%20from%20data%20value%20to%20retrieve%20the%20path%20for%20getting%20blob%20content.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EWe%20can%20use%20managed%20identity%20for%20HTTP%20action%20to%20get%20the%20content%20from%20URL.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CDIV%20id%3D%22tinyMceEditorpuspraj_11%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pic5.PNG%22%20style%3D%22width%3A%20667px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244228iF8AB1CCA3B0F6107%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22pic5.PNG%22%20alt%3D%22pic5.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EExpression%20Inside%20URI%20%3A%26nbsp%3B%20%3CFONT%20color%3D%22%230000FF%22%3EtriggerBody()%3F%5B'data'%5D%3F%5B'blobUrl'%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EYou%20can%20also%20use%20below%20expression%20to%20frame%20audience%20from%20%3CSTRONG%3Eurl%3C%2FSTRONG%3E%20element%20of%20data%20object.%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Econcat(split(triggerBody()%3F%5B'data'%5D%3F%5B'url'%5D%2C'net')%5B0%5D%2C'net')%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EUsing%20get%20blob%20content%20%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CDIV%20id%3D%22tinyMceEditorpuspraj_12%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pic6.PNG%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244239iDEAD1967BFBD42DA%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pic6.PNG%22%20alt%3D%22pic6.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EExpression%20inside%20Blob%3A%20%3CFONT%20color%3D%22%230000FF%22%3Esplit(triggerBody()%3F%5B'data'%5D%3F%5B'blobUrl'%5D%2C'.net%2F')%5B1%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2031881%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22eventgrid.png%22%20style%3D%22width%3A%20526px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244218iD7D65BB03365A79F%2Fimage-dimensions%2F526x224%3Fv%3D1.0%22%20width%3D%22526%22%20height%3D%22224%22%20role%3D%22button%22%20title%3D%22eventgrid.png%22%20alt%3D%22Event%20grid%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EEvent%20grid%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22datalakegen2.png%22%20style%3D%22width%3A%20413px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244219i432749416F93FAD4%2Fimage-dimensions%2F413x229%3Fv%3D1.0%22%20width%3D%22413%22%20height%3D%22229%22%20role%3D%22button%22%20title%3D%22datalakegen2.png%22%20alt%3D%22Storage%20Account%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EStorage%20Account%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22logicapps.jpg%22%20style%3D%22width%3A%20413px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244220iF6D7214C7DC52800%2Fimage-dimensions%2F413x334%3Fv%3D1.0%22%20width%3D%22413%22%20height%3D%22334%22%20role%3D%22button%22%20title%3D%22logicapps.jpg%22%20alt%3D%22Logic%20apps%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ELogic%20apps%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2031881%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eazure%20event%20grid%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ELogic%20Apps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Jan 05 2021 04:45 AM
Updated by: