简体   繁体   中英

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 .

在此处输入图片说明

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?

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);

在此处输入图片说明

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}");
    }
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM