Enmanuel López
Enmanuel López

Reputation: 3

Save specific Office 365 Email Attachments to Azure Blob using Power Automate

I want to Save Office 365 Email Attachments email into Azure Blob using Power Automate and it's work but the only issue is that I want to specify to store one specific file in the Azure Blob, I mean, it doesn't store any other email that I receive just a specific file.

For example: in Power Automate, when a new email arrives store in a Azure blob container and I want to do that doesn't store any other file in the blob container just a specific file.

enter image description here

Any help please?

Upvotes: 0

Views: 2056

Answers (1)

SwethaKandikonda
SwethaKandikonda

Reputation: 8254

You can achieve this by creating the container before creating a blob inside azure storage.

NOTE: Make sure you are creating the container with unique name. Below is the expression I used to create a unique name for the container and stored it in a variable.

int(formatDateTime(utcNow(),'yyyyMMddHHmmss'))

To create a container, I'm using HTTP connector with https://<STORAGE_ACCOUNT_NAME>.blob.core.windows.net/<CONTAINER_NAME>?restype=container&<SAS_URL> URI and using PUT method.

enter image description here

I have used increment variable to have a change in container name so that the next attachment would directly go in a new container.

enter image description here

Below is the complete flow of my logic app

enter image description here

To reproduce the same in your logicapp you can use the below codeview.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@int(formatDateTime(utcNow(),'yyyyMMddHHmmss'))",
                "runAfter": {},
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Create_blob_(V2)": {
                        "inputs": {
                            "body": "@{triggerBody()?['attachments']},",
                            "headers": {
                                "ReadFileMetadataFromServer": true
                            },
                            "host": {
                                "connection": {
                                    "name": "@parameters('$connections')['azureblob']['connectionId']"
                                }
                            },
                            "method": "post",
                            "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files",
                            "queries": {
                                "folderPath": "@{variables('containerName')}",
                                "name": "@items('For_each')?['name']",
                                "queryParametersSingleEncoded": true
                            }
                        },
                        "runAfter": {
                            "HTTP": [
                                "Succeeded"
                            ]
                        },
                        "runtimeConfiguration": {
                            "contentTransfer": {
                                "transferMode": "Chunked"
                            }
                        },
                        "type": "ApiConnection"
                    },
                    "HTTP": {
                        "inputs": {
                            "headers": {
                                "x-ms-blob-type": "BlockBlob",
                                "x-ms-date": "Tue, 20 Sep 2022 23:39:12 GMT",
                                "x-ms-meta-Name": "StorageSample",
                                "x-ms-version": "2014-02-14 "
                            },
                            "method": "PUT",
                            "uri": "https://<STORAGE_ACCOUNT_NAME>.blob.core.windows.net/<CONTAINER_NAME>?restype=container&<SAS_URL>"
                        },
                        "runAfter": {
                            "Increment_variable": [
                                "Succeeded"
                            ]
                        },
                        "type": "Http"
                    },
                    "Increment_variable": {
                        "inputs": {
                            "name": "containerName",
                            "value": 1
                        },
                        "runAfter": {},
                        "type": "IncrementVariable"
                    }
                },
                "foreach": "@triggerBody()?['attachments']",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "containerName",
                            "type": "integer",
                            "value": "@outputs('Compose')"
                        }
                    ]
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "When_a_new_email_arrives_(V3)": {
                "inputs": {
                    "fetch": {
                        "method": "get",
                        "pathTemplate": {
                            "template": "/v3/Mail/OnNewEmail"
                        },
                        "queries": {
                            "folderPath": "Inbox",
                            "importance": "Any"
                        }
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['office365']['connectionId']"
                        }
                    },
                    "subscribe": {
                        "body": {
                            "NotificationUrl": "@{listCallbackUrl()}"
                        },
                        "method": "post",
                        "pathTemplate": {
                            "template": "/GraphMailSubscriptionPoke/$subscriptions"
                        },
                        "queries": {
                            "folderPath": "Inbox",
                            "importance": "Any"
                        }
                    }
                },
                "splitOn": "@triggerBody()?['value']",
                "type": "ApiConnectionNotification"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "azureblob": {
                    "connectionId": "/subscriptions/<SUBID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/azureblob",
                    "connectionName": "azureblob",
                    "id": "/subscriptions/<SUBID>/providers/Microsoft.Web/locations/centralus/managedApis/azureblob"
                },
                "office365": {
                    "connectionId": "/subscriptions/<SUBID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/office365",
                    "connectionName": "office365",
                    "id": "/subscriptions/<SUBID>/providers/Microsoft.Web/locations/centralus/managedApis/office365"
                }
            }
        }
    }
}

Upvotes: 0

Related Questions