Cannot Upload new file to Azure.Storage.Blob?

currently I am trying to upload my files to Azure Storage using Azure.Storage.Blobs. I have correctly initalize a BlobContainerClient using the connection string and container name, however at the moment when I try to initialize a BlobClient using GetBlobClient(filepath), for example:

GetBlobClient('./data/image.png')

I need to have a file exist in ./data/image.png for the program to pick up and overwrite this file as I upload my file by overwriting. And if I just change file path to the same directory but with different file name (i.e change image.png -> newimage.png) then file BlobClient does not exist.

Can I ask how to upload new file to the Azure Storage Container? Is this something related to container permission or something else?

Thank you so much.

Here is my current code:

BlobContainerClient blobContainerClient = new BlobContainerClient(connectionString, containerName);
try
   {
    //Require a "file" exist before hand, or else the client does not exist
    BlobClient blobClient = blobContainerClient.GetBlobClient(filepath);
    if (blobClient.Exists())
    {
      Console.WriteLine("BlobClient exist!");
      var stream = File.OpenRead("image.png")
      blobClient.UploadAsync(stream, overwrite: true);
      succeeded = true;
    }
    else
    {
      Console.WriteLine("BlobClient does not exist");
      succeeded = false;
    }
      
}
catch(Exception e)
{
  throw new Exception(e);
}

Upvotes: 0

Views: 2036

Answers (1)

RithwikBojja
RithwikBojja

Reputation: 11183

Cannot Upload new file to Azure.Storage.Blob?

You can upload a file using that.

Can I ask how to upload new file to the Azure Storage Container?

I have reproduced in my environment to upload files when a file is present in directory, if not it gives error as message and I followed Microsoft-Document:

using Azure.Storage.Blobs;
using System.Runtime.CompilerServices;

Console.WriteLine("Hello Rithwik running your code!!!");
Console.WriteLine("**************");
Console.WriteLine("**************");
string cs = "DefaultEndpointsProtocol=https;AccountName=rithwik;AccountKey=pPBW9kA==;EndpointSuffix=core.windows.net";
string cn = "rithwik1";
BlobContainerClient bcc = new BlobContainerClient(cs, cn);

try
{
    string fp = @"C:\Users\Downloads\Rith.png"; 
    string bn = "image.png"; 

    BlobClient bc = bcc.GetBlobClient(bn);

    using (FileStream fileStream = File.OpenRead(fp))
    {
        await bc.UploadAsync(fileStream, overwrite: true);
    }

    Console.WriteLine($"File uploaded successfully into conatiner : {cn} .");
    Console.WriteLine("**************");
    Console.WriteLine("**************");
    Console.WriteLine("Thank You");
}
catch (Exception e)
{

    Console.WriteLine($"Error has detected in the above code : {e.Message}");
    Console.WriteLine("**************");
    Console.WriteLine("**************");
    Console.WriteLine("Please do the needful");
}

Console.ReadLine();

enter image description here

cn is container name

cs is connection string

fp is filepath

Output:

If file present in directory(it overwrites too) :

enter image description here

enter image description here

If file is not present in directory:

enter image description here

Upvotes: 0

Related Questions