Tuesday 13 June 2023

Upload file in Blob Storage in C#

 string connectionString = "DefaultEndpointsProtocol=https;AccountName=abcStorage;AccountKey=/ABCDEFGHIJKKlaDfOG6ix+Ek+24J7cBi==;EndpointSuffix=core.windows.net";

                                    //"DefaultEndpointsProtocol=https;AccountName=your_storage_account;AccountKey=your_storage_account_key;EndpointSuffix=core.windows.net";
            string containerName = "mycontainer";
            string filePath = @"C:\MyCustomDoc\123.csv";
            string blobName = @"FileData\123.csv";
            UploadFile(connectionString, containerName, filePath, blobName);
            Console.WriteLine("file uploaded successfully.");

---------------------------------------

static void UploadFile(string connectionString, string containerName, string filePath, string blobName)
        {
            try
            {
                BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

 

                using (FileStream fileStream = File.OpenRead(filePath))
                {
                    containerClient.UploadBlob(blobName, fileStream);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }

        }