skip to Main Content

I am trying to download a file which is inside a subfolder of a blob container using Azure C# functions. This is all in a storage account.

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(fileUri);  
string containerName = blobUriBuilder.BlobContainerName;
string blobName = blobUriBuilder.BlobName; 
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
// Check if the document exists
if (!await blobClient.ExistsAsync())
{
    return new NotFoundObjectResult(new { error = "Verify the file name", details = "Unable to locate the file in the provided location" });
} 
BlobDownloadInfo download = await blobClient.DownloadAsync();

I am able to download file from a root directory, but when I try to download a file inside a subfolder, it throws an error.

2

Answers


  1. Chosen as BEST ANSWER
     BlobUriBuilder blobUriBuilder = new BlobUriBuilder(fileUri);
    
     string containerName = blobUriBuilder.BlobContainerName;
    
     // Split the BlobName into subfolder and blob name
     string[] pathParts = blobUriBuilder.BlobName.Split('/');
    
     // The first part is the subfolder, the rest is the blob name
     string subfolder = pathParts.Length > 1 ? pathParts[0] : null;
     string blobName = pathParts.Length > 1 ? string.Join('/', pathParts.Skip(1)) : pathParts[0];
    
     // If subfolder is not null, concatenate it with the blob name
     string blobPath = subfolder != null ? $"{subfolder}/{blobName}" : blobName;
    
     var blobServiceClient = new BlobServiceClient(connectionString);
     BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
     BlobClient blobClient = containerClient.GetBlobClient(blobPath);
    
     // Check if the document exists
     if (!await blobClient.ExistsAsync())
     {
         return new NotFoundObjectResult(new { error = "Verify the file name", details = "Unable to locate the file in the provided location" });
     }
    
     BlobDownloadInfo download = await blobClient.DownloadAsync();
    
     string fileShareName = data.MoveFileRequest.TargetFileShareLocation.Split('/')[0];
     string targetSubfolder = data.MoveFileRequest.TargetFileShareLocation.Contains("/")
         ? data.MoveFileRequest.TargetFileShareLocation.Substring(data.MoveFileRequest.TargetFileShareLocation.IndexOf('/') + 1)
         : "";
    
     ShareClient shareClient = new ShareClient(connectionString, fileShareName);
    
     // Check if the file share exists
     if (!await shareClient.ExistsAsync())
     {
         return new NotFoundObjectResult(new { error = "Verify the target file share location", details = "Unable to move the file, File share does not exist in the directory" });
     }
    
     ShareDirectoryClient directoryClient = shareClient.GetRootDirectoryClient();
    

    This worked. Thanks guys for the input.


  2. I could achieve your requirement with below steps:

    • Uploaded a text file in the subfolder of my container in the Azure Storage Account:

    enter image description here

    • The file name along with the folder name should be passed in the GetBlobClient() as shown below:
    BlobClient blobClient = containerClient.GetBlobClient($"{subfolderName}/{fileName}");
    

    My Code Snippet:

    var connectionString = "<storage_connection_string>";
    var url = "https://<storage>.blob.core.windows.net/<Blob_name>/<Folder1>/<folder2><file.exe>";
    BlobUriBuilder blobUriBuilder = new BlobUriBuilder(new Uri(url));
    string containerName = blobUriBuilder.BlobContainerName;
    string blobName = blobUriBuilder.BlobName;
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);
    
    if (!await blobClient.ExistsAsync())
    {
        return new NotFoundObjectResult(new { error = "Verify the file name", details = "Unable to locate the file in the provided location" });
    }
    BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync();
    string downloadedData = downloadResult.Content.ToString();
    log.LogInformation("Downloaded data: " + downloadedData);
    
    • In this case, BlobName contains the full path of the blob within the directory/folder. i.e., "subfolder/file.exe".

    enter image description here

    Console Output:

    • Able to read the content of the downloaded file.
    Azure Functions Core Tools
    Core Tools Version:       4.0.5530 Commit hash: N/A +c8883e7f3c06e2XXXX3806c19d8d91418c (64-bit)
    Function Runtime Version: 4.28.5.21962
    
    [2024-03-07T11:48:50.219Z] Found C:Usersuname...FunctionApp.csproj. Using for user secrets file configuration.
    
    Functions:
    
            Function1: [GET,POST] http://localhost:7236/api/Function1
    
    For detailed output, run func with --verbose flag.
    [2024-03-07T12:11:43.018Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
    [2024-03-07T12:11:59.840Z] Executing 'Function1' (Reason='This function was programmatically called via the host APIs.', Id=5744c98b-ccXXX8f-fb4cfe85d25b)
    [2024-03-07T12:11:59.941Z] C# HTTP trigger function processed a request.
    [2024-03-07T12:12:22.316Z] Downloaded data: Secret-1
    [2024-03-07T12:12:22.333Z] Secret-2
    [2024-03-07T12:12:22.344Z] Secret-3
    [2024-03-07T12:12:22.546Z] Executed 'Function1' (Succeeded, Id=5744c98b-cca6-43XXXXX4cfe85d25b, Duration=22797ms)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search