I'm trying to fetch all the file names from a Blob Container in Azure storage account.
This is the code I have written to do the same.
string connectionString = "DefaultEndpointsProtocol=https;AccountName=filesStorage;AccountKey=xxxx;EndpointSuffix=core.windows.net";
string containerName = "firstContainer" "\\" "secondFolder";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
var blobs = containerClient.GetBlobs();
foreach (var item in blobs)
{
blobFilesList.Add(item.Name);
}
When I run this code , I'm getting an error at foreach statement :
InvalidUri
The requested URI does not represent any resource on the server.
But the connectionstring I'm using is correct. I'm able to login into Azure storage account using this connectionstring.
Can anyone tell me what am I doing wrong here. I searched for similar problems , but I couldn't get any relevant solution for this. Any help is greatly helpful. Many thanks.
CodePudding user response:
The reason you're running into this issue is because you're including the name of your virtual folder when you're creating your blob container client. It should only include the blob container name.
To list the blobs from a virtual folder, you will need to pass the full path of that virtual folder as prefix
parameter in your GetBlobsMethod
.
Can you please try something like:
string connectionString = "DefaultEndpointsProtocol=https;AccountName=filesStorage;AccountKey=xxxx;EndpointSuffix=core.windows.net";
string containerName = "firstContainer";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
var blobs = containerClient.GetBlobs(prefix:"secondFolder/");
foreach (var item in blobs)
{
blobFilesList.Add(item.Name);
}