0
votes

I am using Azure SDK for .Net (version 9.3.1, Platform .NET-Standard 2.0) for working with Azure Blob storage, and having troubles with referencing block blobs having blank space in the blob name. I have uploaded a block blob JSON Test.json into a private container via Azure Storage Explorer 1.6.1.

Blob properties as per Azure Storage Explorer:

Name: `JSON Test.json`
URI: `https://<myaccountname>/<mycontainername>/JSON%20Test.json`

Now, I am trying to check whether that Blob exists with CloudBlock​Blob.​Exists​Async() method passing to GetBlockBlobReference non-encoded filename JSON Test.json

And getting FALSE as result.

Now, I am creating a blob programatically in a different container, passing the same filename non-encoded, using the same GetBlockBlobReference and getting created a blob with encoded filename.

Name: `JSON%20Test.json` 
URI: `https://<myaccountname>/<mycontainername2>/JSON%20Test.json`

What I am doing wrong? Why my block blob with blank space in its name created via Azure Storage Explorer is not found while referencing it with non-encoded filename? When creating a block blob programatically a passing non-encoded filename, why the filename gets encoded via the wire?

Please help.

Thanks a lot in advance!

public async Task<bool> CheckExistsAsync(string connectionString, string containerName, string fileName)
        {
            var blockBlob = GetBlockBlobReference(connectionString, containerName, fileName);
            return await blockBlob.ExistsAsync();
        }

private static CloudBlockBlob GetBlockBlobReference(string connectionString, string containerName, string fileName)
        {
            return CloudStorageAccount
                .Parse(connectionString)
                .CreateCloudBlobClient()
                .GetContainerReference(containerName)
                .GetBlockBlobReference(fileName);
        }
2
If the answer works for you, could you please help mark it as answer? thanks. - Ivan Yang

2 Answers

0
votes

Please try to update WindowsAzure.Storage to the latest version v9.3.3.

I used your code for the testing, and no issues with blob name contains white space.

Sample code:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Threading.Tasks;

namespace AzureBlobConsole
{
    class Program
    {
        static void Main(string[] args)
        {         
            string conn = "xxxx";
            bool x = CheckExistsAsync(conn, "f11", "222 json test.json").GetAwaiter().GetResult();

            //to see if the file exists or not
            Console.WriteLine(x);
            Console.WriteLine("completed.");
            Console.ReadLine();
        }

        public static async Task<bool> CheckExistsAsync(string connectionString, string containerName, string fileName)
        {
            var blockBlob = GetBlockBlobReference(connectionString, containerName, fileName);
            return await blockBlob.ExistsAsync();
        }

        private static CloudBlockBlob GetBlockBlobReference(string connectionString, string containerName, string fileName)
        {
            return CloudStorageAccount
                .Parse(connectionString)
                .CreateCloudBlobClient()
                .GetContainerReference(containerName)
                .GetBlockBlobReference(fileName);
        }

    }
}

Test result:

enter image description here

0
votes

To check if blob exists try this code:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

var found = await blobClient.GetBlobReferenceFromServerAsync(new Uri(filename));

You probably need to access file via Uri not by string in your last line. I havent really used async methods you used, but the code above is the code that works for me.