设置了 content-md5 的 Azure blob 上传

标签 azure powershell azure-blob-storage azure-cli

目标:将文件上传到 Azure Blob 存储并设置 MD5,以便在用户下载文件时进行验证。

使用 Azure CLI Powershell。

Get-FileHash -Algorithm MD5 .\AutoSetup.zip
Algorithm       Hash                                             Path
---------       ----                                             ----
MD5             693EF0DB938308AC2C362F50F7CB9F9F                 C:\MyFiles\AutoSetup.zip

az storage blob upload --account-name mystorageaccount --container-name mycontainername --file AutoSetup.zip --name Autosetup2.zip --content-md5 693EF0DB938308AC2C362F50F7CB9F9F
Finished[#############################################################]  100.0000%
The MD5 value specified in the request is invalid. MD5 value must be 128 bits and base64 encoded. ErrorCode: InvalidMd5
<?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidMd5</Code><Message>The MD5 value specified in the request is invalid. MD5 value must be 128 bits and base64 encoded.
RequestId:9f27334a-801e-0028-6db4-3539c5000000
Time:2020-05-29T12:28:23.7677258Z</Message></Error>

编辑 1:

我也尝试过以这种方式获取哈希

$someFilePath = "C:\MyFiles\AutoSetup.zip"
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($someFilePath)))
Write-Host $hash
69-3E-F0-DB-93-83-08-AC-2C-36-2F-50-F7-CB-9F-9F

似乎无论我做什么,文件返回的 MD5 都是 693EF0DB938308AC2C362F50F7CB9F9F 但 Azure 不会接受它...

编辑2:

我生成了一个随机的 128 位字符串 $B&E)H@McQfTjWnZ 并继续以 Base64 对其进行编码,这给了我 JEImRSlIQE1jUWZUalduWg== 当我尝试上传带有该哈希值的 blob,我收到不同的错误消息:

The MD5 value specified in the request did not match with the MD5 value calculated by the server. ErrorCode: Md5Mismatch

上面的内容是有道理的,因为我刚刚创建了一个随机的 128 位 base64 编码的哈希值。但是,现在我想知道为什么 Powershell 的 Get-FileHash 命令给出的结果似乎不正确?

什么可能导致错误?

最佳答案

尝试将哈希值转换为 Base64 编码的字符串。像这样的东西:

$someFilePath = "C:\MyFiles\AutoSetup.zip"
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider    
$hash = [System.Convert]::ToBase64String($md5.ComputeHash([System.IO.File]::ReadAllBytes($someFilePath)))
Write-Host $hash
8SzzdVQAV4Wdbp8Z9qsczg==

关于设置了 content-md5 的 Azure blob 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62086677/

相关文章:

azure - 如何将 Sql 查询结果从 Azure Synapse 笔记本传递到 Synapse Pipeline 中的下一个事件?

azure - "' New-AzureStorageContext ' is not recognized,"模块已安装

sql-server - 如何通过powershell关闭数据库加密

azure - 下载大文件并将其拆分为 blob 存储中的 100 MB block

azure - 如何从 Azure ACS 获取对称 key ?

azure - Microsoft Azure - 多区域访问 blob 存储

java.lang.NoSuchMethodError : com. microsoft.azure.storage.core.StorageCredentialsHelper.signBlobAndQueueRequest

powershell - 如何在 PowerShell 中用 %20 替换空格?

c# - 以编程方式获取 Azure 存储帐户属性

azure - 将流写入 blob 时租用 blob,直至完成