我正在嘗試使用 SAS 令牌在 Azure 中上傳檔案,但在嘗試上傳檔案時收到此錯誤:
MD5 不是已知的哈希演算法
我使用了這兩種方法,一種是生成用于上傳檔案的檔案鏈接:
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 使用另一種加密散列演算法,但我不知道應該做什么。
uj5u.com熱心網友回復:
我按照官方示例代碼進行了測驗,效果很好。你可以參考我的測驗代碼。
測驗結果:


示例代碼如下:
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/394416.html
