【问题标题】:Azure blob upload with content-md5 set设置了 content-md5 的 Azure Blob 上传
【发布时间】:2020-09-17 01:34:21
【问题描述】:

目标:将文件上传到 Azure Blob Storage,并设置用户下载文件时可以验证的 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&amp;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 命令给了我似乎不正确的信息?

什么可能导致错误?

【问题讨论】:

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


    【解决方案1】:

    尝试将哈希转换为 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==
    

    【讨论】:

    • 成功了!!谢谢!现在我唯一关心的是[System.IO.File]::ReadAllBytes 的性能——这有文件大小限制吗?现在我正在用小文件进行测试;然而,实际上这将用于 500mb 的文件。这不是很多,我知道,但仍然......
    • 我还没有真正尝试过。对于大文件,您可以尝试使用BufferedStream。例如:stackoverflow.com/questions/1177607/….
    • 我刚刚用一个 3.9GB 的文件试了一下,效果很好。
    【解决方案2】:

    我想为此添加自己的“解决方案”,以防有人更喜欢这种方法。

    方法 1 在 Gaurav 的回答中有所描述。我们从文件中读取字节,计算 MD5,并使用 Base64 对其进行编码。这将导致az storage blob upload 将验证并接受的MD5 字符串。这似乎是正确的方法。

    方法二是不设置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
    

    我不知道第二种方法有多正确,但它仍然可以工作。

    【讨论】:

    • 使用第二种方法,我看到两个问题:1)您正在发出额外的网络请求,因此可能会失败。和 2) 因为您只更新 content-md5 属性,所有其他属性(如 content-typecache-control 等)将被覆盖。
    猜你喜欢
    • 2021-03-31
    • 2019-04-08
    • 2021-08-30
    • 2016-04-27
    • 2017-10-29
    • 2021-10-19
    • 2016-05-10
    • 1970-01-01
    • 2015-06-03
    相关资源
    最近更新 更多