Advertisements

Archive

Posts Tagged ‘Azure Blob’

[Code Snippet] Upload file to Azure blob – C#

In this article I am going to provide details and code snippets on how to upload attachment to Azure blob storage from console application.

Prerequisites: 

Below are the prerequisites to run the code snippet and upload the file

  • Azure subscription:You need an Azure subscription as the first  step.
    • You can spin up 30 days trail Azure subscription. Click here
    • Note: You need to share valid credit card details to complete the subscription and you will be charged 2 INR.
  • Storage Account:Add a storage account

Azure - Storage Account

  • Container:
    • Add a Container
    • Copy the Container Name.

Azure - Storage Account - Containers

  • Access Keys:Need the ‘Key’ to connect to Azure Blob from your C# console.
    • Copy and keep below 2 values as shown in screenshot
      • Storage Account Name
      • Key 1

Azure - Storage Account - Keys

  • Nuget package:Add below nuget packages to your console project
      • Microsoft.WindowsAzure.Storage

C# Code Snippet:

// Namespaces

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

private static void AddFileToBlob(){
var accountName = “{Storage Account Name}“; // Refer Prerequisites for value
var keyValue = “{key 1}“; // Refer Prerequisites for value
var useHttps = true;
var connValid = true;

// Establish connection to Azure

var storageCredentials = new StorageCredentials(accountName, keyValue);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps);
var blobConString = storageAccount.ToString(connValid);

// Retrieve storage account from connection string.
storageAccount = CloudStorageAccount.Parse(blobConString);

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Set container name
CloudBlobContainer container = blobClient.GetContainerReference(“{Container Name}“); // Refer Prerequisites for value

// Set your blob name; It can be anything
CloudBlockBlob blockBlob = container.GetBlockBlobReference(“{Your desired blob name}“);

// Set your file path which you want to upload to blob
using (var fileStream = System.IO.File.OpenRead(@”D:\ABC.PNG”)) {
blockBlob.UploadFromStream(fileStream);
}

Console.WriteLine(“File added to Blob!!!”);
}

🙂

Advertisements
Categories: Azure Tags: ,

Move Attachments from CRM to Azure Blob – “Attachment Management” tool

December 17, 2017 2 comments

Recently I was exploring options to move attachments from CRM (i.e., Notes and Email) to Azure Blob storage and came across “Attachment Management” tool from Microsoft Labs.

This tool can be installed as a solution to your Dynamics 365 instance. Detailed steps and usage guide are available here.

In this article I am going to detail the imperative Azure and Dynamics 365 settings which would avoid the connectivity issues you may encounter during my usage.

Pre-requisites to use this solution:

  • Dynamics 365 instance (You can subscribe for 30 days trail)
  • Enable the “Attachment Management” solution from App Source.
  • Microsoft Azure Account (You can subscribe for trail)
    • Note: You can use the same Dynamics Office 365 account to register for Azure trail account. 

Setting up Azure Storage Account:

Make sure your Azure Storage Account settings matches with below

  • Azure “Storage Account” ‘KIND’ must be “BlobStorage”.

Attachment - Azure Storage Account

  • “Storage Account” Containers ‘Public Access Level’ must be “Blob”.

Attachment - Containers

  • “Shared Access Signature” (SAS token)
    • Set the “Start” to Previous Day as the Azure time zone depends the data Center you choose. (For example, your IST date may be 1 day ahead of your ‘Data Center’ date which may make your ‘SAS token’ invalid)
    • Set the “End” to at least a week ahead of today.

Attachment - Shared Access Signature

Settings in Dynamics 365:

  • With the “Attachment Management” solution you will get 3 entities and a ‘Site Map Area’ (i.e., Azure Attachment Storage)
  • Make sure you configure settings only from ‘Azure Attachment Storage’ site map link and not from ‘Advanced Find’

Attachment - Sitemap

  • In “Azure Blob Storage Setup” page, make sure the ‘Storage Account Name’, ‘Container names’ and ‘SAS token’ matches with Azure settings

Attachment - BlobStorageSettings

  • In “NOTES ATTACHMENT SETTING ENTITY” page, pick your entities and provide correct “Container” name or leave that blank.
    • Note: If you have Dynamics Portal solution installed, uncheck the ‘Web Files’ entity, if its selected.

Attachment - NotesAttachment Page

Troubleshooting steps:

  • After you configure all the settings try adding a Note with attachment to the entity you configured.
  • If you don’t see your attachments move to Azure Container, go to ‘System Settings’ and look for “MicrosoftLabs.AttachmentManagement…..” job for exception.

Attachment - System Jobs

🙂