Search code examples
c#azureazure-storageazure-blob-storage

The specified resource name length is not within the permissible limits Azure Blob Storage


I am getting an error as "The specified resource name length is not within the permissible limits" when I try to upload a blob to my Azure Storage Account.

enter image description here

Below is my code.

private async Task UploadToAzureBlobStorage(string path, string fileName) {
    try {
        if (CloudStorageAccount.TryParse(StorageConnectionString, out CloudStorageAccount cloudStorageAccount)) {
            var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            var cloudBlobContainer = cloudBlobClient.GetContainerReference(path);
            var cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
            await cloudBlockBlob.UploadFromFileAsync(path);
        }
        else {
            throw new CustomConfigurationException(CustomConstants.NoStorageConnectionStringSettings);
        }
    }
    catch(Exception ex) {
        throw new CustomConfigurationException($ "Error when uploading to blob: {ex.Message}");
    }
}

Anyone else have faced this same issue?


Solution

  • Sometimes the error messages can be misleading. I was getting this error when I call the await cloudBlockBlob.UploadFromFileAsync(path); and the reason behind this issue is that, I have provided an invalid parameter to the function cloudBlobClient.GetContainerReference(path);

    enter image description here

    This happened because of a recent change in the development. I was constantly concentrating on the fileName parameter in the GetBlockBlobReference function, as I thought the problem is with the blob name.

    Unfortunately I was wrong and the real issue was with the blob container name where it has some special characters in it, so I had introduced a new parameter schedule to the existing function, and values of this parameter can be daily, weekly, monthly and these are the names of the containers I have configured in the Azure Blob Storage.

    private async Task UploadToAzureBlobStorage(string schedule, string path, string fileName) {
        try {
            if (CloudStorageAccount.TryParse(StorageConnectionString, out CloudStorageAccount cloudStorageAccount)) {
                var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                var cloudBlobContainer = cloudBlobClient.GetContainerReference(schedule);
                var cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
                await cloudBlockBlob.UploadFromFileAsync(path);
            }
            else {
                throw new CustomConfigurationException(CustomConstants.NoStorageConnectionStringSettings);
            }
        }
        catch(Exception ex) {
            throw new CustomConfigurationException($ "Error when uploading to blob: {ex.Message}");
        }
    }
    

    enter image description here