目标:将文件上传到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==
当我尝试上传带有THAT散列的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==
我想为此添加自己的“解决方案”,以防万一有人喜欢这种方法。
方法1在Gaurav的答案中进行了描述。我们从文件中读取字节,计算MD5,并使用Base64对其进行编码。这将导致az storage blob upload
将验证并接受的MD5字符串。这似乎是正确的方法。
方法2是在不设置content-md5的情况下上传文件,然后再进行更新。
获取MD5哈希
Get-FileHash -Algorithm MD5 .\AutoSetup.zip
Algorithm Hash Path
--------- ---- ----
MD5 693EF0DB938308AC2C362F50F7CB9F9F C:\MyFiles\AutoSetup.zip
上传不带MD5的Blob
az storage blob upload --account-name mystorageaccount --container-name mycontainername --file AutoSetup.zip --name Autosetup2.zip
Finished[#############################################################] 100.0000%
使用content-md5更新Blob。我们可以在此处编写任何我们想要的内容,因此可以使用较早版本的MD5!
az storage blob update --account-name mystorageaccount --container-name mycontainername --name Autosetup2.zip -- content-md5 693EF0DB938308AC2C362F50F7CB9F9F
我不知道第二种方法的正确性,但是仍然可以使用。