1. Create a Storage Account
Connect to Azure portal.
In the left pane you can see the "Storage accounts". Click on the link.
On the top left hand side you can click on "Add" and add new storage account. Specify the storage name and etc.. You can either create a new resource group or use an existing group for your storage's resource group.
once created you get the below page, listing your storage accounts. click on the newly created storage account.
Note that you get a list of storage options which you can modify and add. You can also get the connection string to the storage account under the "Access keys" section.
The next step is to create a container inside the storage to hold the blobs, in my care its going to be the images.
2. Create the container to store the blobs
Click on containers. create a new container. I have named it as "Images"
3. Now is the fun part. Coding to upload the images to Azure Blob Storage
public string UploadImageUsingByteArray(byte[] byteArray, string fileNamewithGuid)
{
string blobUrl = string.Empty;
try
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString); //here the storageConnectionString you can obtain from the storage properties under Access keys
//Create a blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(imageContainerName); //my container name is image
container.CreateIfNotExists();
if (container.Exists())
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileNamewithGuid);
blockBlob.Properties.ContentType = "image/jpeg";
blockBlob.UploadFromByteArray(byteArray, 0, byteArray.Length);
blobUrl = blockBlob.Uri.AbsoluteUri; //returns the blob's URL. How awesome is that
}
}
catch (Exception)
{
throw;
}
return blobUrl;
}
Here I have used the option to upload a byte array using the method UploadFromByteArray(). You can also use the method UploadFromStream() if your input is a stream. It works for a file stream as well.
Refresh the container when your code has run, and you get your images in Azure blob
Connect to Azure portal.
In the left pane you can see the "Storage accounts". Click on the link.
On the top left hand side you can click on "Add" and add new storage account. Specify the storage name and etc.. You can either create a new resource group or use an existing group for your storage's resource group.
once created you get the below page, listing your storage accounts. click on the newly created storage account.
Note that you get a list of storage options which you can modify and add. You can also get the connection string to the storage account under the "Access keys" section.
The next step is to create a container inside the storage to hold the blobs, in my care its going to be the images.
2. Create the container to store the blobs
Click on containers. create a new container. I have named it as "Images"
3. Now is the fun part. Coding to upload the images to Azure Blob Storage
public string UploadImageUsingByteArray(byte[] byteArray, string fileNamewithGuid)
{
string blobUrl = string.Empty;
try
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString); //here the storageConnectionString you can obtain from the storage properties under Access keys
//Create a blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(imageContainerName); //my container name is image
container.CreateIfNotExists();
if (container.Exists())
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileNamewithGuid);
blockBlob.Properties.ContentType = "image/jpeg";
blockBlob.UploadFromByteArray(byteArray, 0, byteArray.Length);
blobUrl = blockBlob.Uri.AbsoluteUri; //returns the blob's URL. How awesome is that
}
}
catch (Exception)
{
throw;
}
return blobUrl;
}
Here I have used the option to upload a byte array using the method UploadFromByteArray(). You can also use the method UploadFromStream() if your input is a stream. It works for a file stream as well.
Refresh the container when your code has run, and you get your images in Azure blob
No comments:
Post a Comment