There are many ways to upload files to Azure Storage. We will discuss 3 different ways today. Before that let's create a storage container on Azure Portal.
1. Create a Storage account in Azure Portal
2. Click on the container, create a new container, and select the public access level.
3. Click on the "Access keys" option and copy the connection string. We will use it in our code.
Let's learn different ways to upload files to Azure Storage. To do so, you need to install Azure.Storage.Blobs package from Nuget and you can run the below code based on your requirements.
1. Upload bytes/base64 string to Azure Storage
[HttpPost]
public async Task<string> UploadFile(string imageBase64, string fileName)
{
if (!string.IsNullOrEmpty(imageBase64))
{
var bytes = Convert.FromBase64String(imageBase64);
var stream = new MemoryStream(bytes);
var connectionString = "Default";// pass connectionstring here
var blobServiceClient = new BlobServiceClient(connectionString);
//replace containername with your container name var containerClient = blobServiceClient.GetBlobContainerClient("containername");
if (containerClient == null)
{
return string.Empty;
}
var blobClient = containerClient.GetBlobClient(fileName);
var result = await blobClient.UploadAsync(stream);
return blobClient.Uri.AbsoluteUri;
}
return string.Empty;
}
2. Upload IFormFile to Azure Storage
[HttpPost]
public async Task<string> UploadFile(IFormFile file,string fileName)
{
if (file != null)
{
var stream = file.OpenReadStream();
var connectionString = "Default";// pass connectionstring here
var blobServiceClient = new BlobServiceClient(connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient("containername");
if (containerClient == null)
{
return string.Empty;
}
var blobClient = containerClient.GetBlobClient(fileName);
var result = await blobClient.UploadAsync(stream);
return blobClient.Uri.AbsoluteUri;
}
return string.Empty;
}
3. Upload Local file to Azure Storage
[HttpPost]
public async Task<string> UploadFile(string fileName)
{
string localfilePath = "Pass local file path here";
var stream = System.IO.File.OpenRead(localfilePath);
var connectionString = "Default";// pass connectsting here
var blobServiceClient = new BlobServiceClient(connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient("containername");
if (containerClient == null)
{
return string.Empty;
}
var blobClient = containerClient.GetBlobClient(fileName);
var result = await blobClient.UploadAsync(stream);
return blobClient.Uri.AbsoluteUri;
}
4. Upload Dynamic Json to Azure Storage
string storageConnectionString = "your_storage_connection_string";
CloudStorageAccount storageAccount;
if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
{
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("your_container_name");
await container.CreateIfNotExistsAsync();
CloudBlockBlob blob = container.GetBlockBlobReference("your_blob_name.json");
dynamic jsonObject = new
{
Name = "John Doe",
Age = 30,
Email = "john.doe@example.com"
};
string jsonContent = JsonConvert.SerializeObject(jsonObject);
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter(ms))
{
writer.Write(jsonContent);
writer.Flush();
ms.Position = 0;
await blob.UploadFromStreamAsync(ms);
Console.WriteLine("JSON file uploaded successfully.");
}
}
}
else
{
Console.WriteLine("Invalid storage connection string.");
}
This is all about this article.
No comments:
Post a Comment