azureasp.net-corecloudmd5shared-access-signatures

'MD5 is not a known hash algorithm' exception while trying to upload a blob to Azure Cloud?


I'm trying to upload a file in Azure by using a SAS token but I receive this error while I try to upload the file :

MD5 is not a known hash algorithm

I have these 2 methods that I use, one to generate the file link which will be used to upload the file:

   public string GetBlobSASUploadFileLink(string fileName)
   {
            var connectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, AccessKey);
            var storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(FilesContainer);

            SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
            sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5);
            sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Create;

            var blob = container.GetBlockBlobReference(fileName);
            return string.Format("{0}{1}", blob.Uri, blob.GetSharedAccessSignature(sasConstraints));
   }

And this method where also the exception is thrown, which should upload the file in Azure:

 public async Task UploadFilesToBlob(string fileLink, IBrowserFile file)
 {
        try
        {
            var cloudBlockBlob = new CloudBlockBlob(new Uri(fileLink));
            await cloudBlockBlob.UploadFromStreamAsync(file.OpenReadStream(912000000));
        }
        catch (Exception ex)
        {

        }
  }

In this second method in the UploadFromStreamAsync method the exception is thrown. I'm guessing the framework uses the MD5 algorithm but the Azure uses another cryptographic hashing algorithm but I don't know what should be done.


Solution

  • I follow the official sample code and test it, it works fine. You can refer my test code.

    Test Result:

    enter image description here

    enter image description here

    Sample code like below:

        public static async Task MyFunc()
        {
            var filepath = @"C:\Users\jason\Desktop\1.txt";
            var file = new FileStream(filepath, FileMode.Open);
    
            CloudStorageAccount storageAccount;
    
            string container_name = "memorydumps";
            var policyName = "Pan";
    
            var connectionString = string.Format("DefaultEndpoi***;EndpointSuffix=core.windows.net");
            storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(container_name);
            container.CreateIfNotExists();
    
            var storedPolicy = new SharedAccessBlobPolicy()
            {
                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5),
                Permissions = SharedAccessBlobPermissions.Create |
                  SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read
            };
            var permissions = container.GetPermissions();
    
            // optionally clear out any existing policies on this container
            permissions.SharedAccessPolicies.Clear();
            // add in the new one
            permissions.SharedAccessPolicies.Add(policyName, storedPolicy);
            // save back to the container
            container.SetPermissions(permissions);
    
            //reading file name & file extention    
            string file_extension = Path.GetExtension(filepath);
            string filename_withExtension = Path.GetFileName(filepath);
    
            CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename_withExtension);
            cloudBlockBlob.Properties.ContentType = file_extension;
            await cloudBlockBlob.UploadFromStreamAsync(file);
            var returnpath = string.Format("{0}{1}", cloudBlockBlob.Uri, cloudBlockBlob.GetSharedAccessSignature(storedPolicy));
            Console.WriteLine(returnpath);
        }