2

I am trying to upload files to Azure blob storage via C# code, however, I need a specific directory tree. My code so far:

BlobServiceClient bSC = new BlobServiceClient(MY_CONN_STRING);

string filename = $"test/files/file.json";
string localFilePath = Path.Combine(filename);
var containerClient = bSC.GetBlobContainerClient("mycontainer");
BlobClient blobClient = containerClient.GetBlobClient(filename);

using FileStream uploadFileStream = File.OpenRead(localFilePath);
await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();

I have other piece of code that generates a file in test/files/file.json. In Azure blob storage, however, I want it to be uploaded to the directory tree of my choosing. Example: /mycontainer/Events/production/file.json, whever Events needs to be created as well. Right now it is recreating the tree structure within my project of test/files/file.json.

crystyxn
  • 1,411
  • 4
  • 27
  • 59

1 Answers1

4

String parameter in GetBlobClient is responsible for "path" your file will have once uploaded, it should not be exactly the same as file name in your local system.

BlobClient blobClient = containerClient.GetBlobClient("mycontainer/Events/production/file.json");
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40
  • 5
    One important piece of information to add for OP, Azure Blob Storage uses a flat paradigm to represent blobs, you can make it "look" like it's hierarchical but you should know that's an _abstracted_ lie. See https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blobs-list?tabs=dotnet#flat-listing-versus-hierarchical-listing for more. – evilSnobu Oct 15 '20 at 17:26
  • This was what I was looking for and not understanding. Thank you – crystyxn Oct 15 '20 at 17:47