locked
Copy blob from one storage account to another storage account RRS feed

  • Question

  • Hello,

    I am trying to build an event-based logic app that copies a newly created blob from a primary storage account to a secondary storage account. How can I make the name of the blob in the secondary storage account the same as the original?

    Here are the connectors that I used so far:

    1. Event Grid trigger to start the logic app with Storage Account as the resource.

    2. Compose - Parse JSON to get the data of the event.

    3. Azure Blob Storage for blob storage actions.

    The logic app is triggered when a new file is uploaded to a primary storage account (A). Data of the event is parsed using the Compose connector. Blob path is specified in Get Blob content using path action using the url data. This is followed by the Create blob action, which creates (copies) the blob in the secondary storage account (B).

    I want to use a dynamic content in the Blob name field so that the new blob in B will have the same name as the blob in A.

    Do you have any recommendations on how I can do this?

    Thanks!


    Monday, June 11, 2018 11:09 AM

Answers

  • Hi Rose,

    You can use Get Blob Metadata using Path, to get the metadata, which includes the name of the blob. Here is what it provides:

    So in your case you would do two calls, one for the metadata, and another one for the content then push this to the new blob.

    I hope this helps, Wagner.

    Monday, June 11, 2018 2:04 PM
  • Hi Rose,

    I just tried, this Expression Works for me.

    @{replace(body('Get_Blob_Metadata_using_path')?['Path'],body('Get_Blob_Metadata_using_path')?['Name'],'')}

    In the above Expression you have kept the Single Quotes('body('Get_Blob_Metadata_using_path')?['Name']') for the Expression, May be that's why you get the Error.

    Path will have the Blobname/Container name/myblob.ext

    Name will have myblob.ext

    when you get the output it will be Blobname/Container name/

    So Container Name might be the name of you blob


    Sujith

    Saturday, June 16, 2018 8:12 PM

All replies

  • Hi Rose,

    You can use Get Blob Metadata using Path, to get the metadata, which includes the name of the blob. Here is what it provides:

    So in your case you would do two calls, one for the metadata, and another one for the content then push this to the new blob.

    I hope this helps, Wagner.

    Monday, June 11, 2018 2:04 PM
  • Thank you sir, that worked! :)

    A follow up question, how can I set the Folder path (still in the Create blob action) to a dynamic content so that the blob created in B will be placed in the same folder name as that one in A?

    I attempted to use the Path data but since it includes the blob name, a new folder with the same name as the blob, is automatically created. I can't seem to find a string function that will somehow remove the blob name.


    Tuesday, June 12, 2018 4:19 AM
  • Hi Rose,

    There are few ways to do this.

    1. Initialise an Array Variable of Array and use the split function store the path like below

    @split(body('Get_Blob_Metadata_using_path')?['Path'],'/')"

    2. Then in your Create Blob Action you can Concenate like below accesing the element of the Array

    @{concat(variables('arr')[2],'/',body('Get_Blob_Metadata_using_path')?['Name'])}

    2. if your blob name is constant you can directly use the replace function and take the blob name out of the Path

    Let us know if you need any help


    Sujith

    Tuesday, June 12, 2018 10:49 AM
  • Thank you Sujith! I will try both options. :)

    As for option #2, how do you mean by take the blob name out of the path? I am using a dynamic content for the blob name using Name obtained from the metadata.

    Tuesday, June 12, 2018 11:06 AM
  • hmm.. apparently I can't add an Initialize Variable action in the middle.

    If I add it at the top level, I will have no data to manipulate.

    Tuesday, June 12, 2018 11:38 AM
  • Hi Rose,

    you can Initialise a variable at the Top and Set the Variable if you need Like below

    Second Method Will be more like

    if you have Path like this /blob1/test1/delay.jpg,

    Then you can use the Replace Function like below 

    @{replace(body('Get_Blob_Metadata_using_path')?['Path'],'/blob1','')}

    So That you don't need to Use the Variables and directly pass it in the Create Blob action.

    If this answers your question please mark it accordingly. If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.



    Sujith

    Tuesday, June 12, 2018 12:59 PM
  • Hello Sujith,

    Thank you.

    For the second method, I'm actually aiming to get the folder names only. Using your example

    /blob1/test1/delay.jpg

    I want to use /blob1/test1/ in the Folder path field.

    

    I think using a replace function, will actually work if I have a constant blob name. But the blob name i'm actually passing is a dynamic data.

    Appreciate your feedback!

    Tuesday, June 12, 2018 1:47 PM
  • How about remove the blob name from the path. Something like this:

    @{replace(body('Get_Blob_Metadata_using_path')?['Path'],'Get_Blob_Metadata_using_path')?['Name'],'')}

    This could be used for the folder path, since it represents the original path minus the name of the blob.

    Cheers, Wagner.

    Tuesday, June 12, 2018 1:58 PM
  • Hi Wagner,

    It says invalid expression. Here's the complete expression I used:

    replace(body('Get_Blob_Metadata_using_path')?['Path'], '('Get_Blob_Metadata_using_path')?['Path']', '('Get_Blob_Metadata_using_path')?['Name']')

    Friday, June 15, 2018 4:48 AM
  • Hi Rose,

    I understand why your expression is not working:

    body('Get_Blob_Metadata_using_path')?['Path'] should have something like this: containername/myblob.ext

    body('Get_Blob_Metadata_using_path')?[Name'] should have something like this: myblob.ext

    So what we are trying to do is this:

    replace('containername/myblob.ext','myblob.ext','')

    which means that your expression should be:

    replace(body('Get_Blob_Metadata_using_path')?['Path'],body('Get_Blob_Metadata_using_path')?['Name'],'')

    Try to see if this work.

    Cheers, Wagner.

    Friday, June 15, 2018 11:01 AM
  • Hi Wagner,

    It still says invalid. :(

    But can't seem to follow the expression because based on the replace doc, the expression should go like this:

    replace('<text>', '<oldText>', '<newText>')

    So I tried using this:

    replace(body('Get_Blob_Metadata_using_path')?['Path'], 'body('Get_Blob_Metadata_using_path')?['Name']', '')

    Yet it still says invalid. :D



    Friday, June 15, 2018 12:04 PM
  • Hi Rose,

    I just tried, this Expression Works for me.

    @{replace(body('Get_Blob_Metadata_using_path')?['Path'],body('Get_Blob_Metadata_using_path')?['Name'],'')}

    In the above Expression you have kept the Single Quotes('body('Get_Blob_Metadata_using_path')?['Name']') for the Expression, May be that's why you get the Error.

    Path will have the Blobname/Container name/myblob.ext

    Name will have myblob.ext

    when you get the output it will be Blobname/Container name/

    So Container Name might be the name of you blob


    Sujith

    Saturday, June 16, 2018 8:12 PM
  • Thanks everyone!!!!!!! :)
    Tuesday, June 19, 2018 9:31 AM
  • Hello again!

    Is there a way we can automatically create a container in the secondary storage account if it doesn't exist yet?

    Appreciate your replies, thanks!

    Saturday, June 23, 2018 2:16 AM
  • As far as my Knowledge goes, i dint think we can automatically create a Container in Storage account.  i thin you might need an Azure Function for this.

    https://social.msdn.microsoft.com/Forums/azure/en-US/1ac5760b-1b96-496f-8a5f-452420a9772b/how-to-create-a-blob-containerdynamically-in-logic-app?forum=azurelogicapps


    Sujith

    Saturday, June 23, 2018 6:36 PM
  • Thank you Sujith!

    Is there an existing request for this in the user voice (so I can vote)?

    Sunday, June 24, 2018 11:37 AM
  • I think its already there 

    https://feedback.azure.com/forums/287593-logic-apps/suggestions/33202429-add-option-to-create-container-in-an-azue-blob-sto

    You can vote it


    Sujith

    Tuesday, June 26, 2018 7:55 PM