'MD5 is not a known hash algorithm' 尝试将 blob 上传到 Azure 云时出现异常?
'MD5 is not a known hash algorithm' exception while trying to upload a blob to Azure Cloud?
我正在尝试使用 SAS 令牌在 Azure 中上传文件,但我在尝试上传文件时收到此错误:
MD5 is not a known hash algorithm
我有这两种方法,一种用于生成文件 link,它将用于上传文件:
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));
}
并且此方法也抛出异常,应该在 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)
{
}
}
在 UploadFromStreamAsync
方法的第二个方法中抛出异常。
我猜框架使用 MD5 算法,但 Azure 使用另一种加密哈希算法,但我不知道应该做什么。
按照官方的样例代码测试了一下,没问题。可以参考我的测试代码
测试结果:
示例代码如下:
public static async Task MyFunc()
{
var filepath = @"C:\Users\jason\Desktop.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);
}
我正在尝试使用 SAS 令牌在 Azure 中上传文件,但我在尝试上传文件时收到此错误:
MD5 is not a known hash algorithm
我有这两种方法,一种用于生成文件 link,它将用于上传文件:
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));
}
并且此方法也抛出异常,应该在 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)
{
}
}
在 UploadFromStreamAsync
方法的第二个方法中抛出异常。
我猜框架使用 MD5 算法,但 Azure 使用另一种加密哈希算法,但我不知道应该做什么。
按照官方的样例代码测试了一下,没问题。可以参考我的测试代码
测试结果:
示例代码如下:
public static async Task MyFunc()
{
var filepath = @"C:\Users\jason\Desktop.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);
}