【问题标题】:how to get SHA1 hash without downloading for a blob block stored in azure storage in node js?如何在不下载存储在节点 js 中 Azure 存储中的 blob 块的情况下获取 SHA1 哈希?
【发布时间】:2020-10-22 04:35:12
【问题描述】:

我想使用 sha-1 哈希进行文件验证,我需要 sha-1 哈希值而不从 azure storageblob 下载块。请帮我提供代码示例(Node js)。

  //I am using this method to upload BlockBlob (@azure/storage-blob npm)
  blockBlobClient.commitBlockList(arr).then(async (res)=>{
                  console.log('res',res);         
              }).
              catch((err)=>console.log(err))
          });

【问题讨论】:

    标签: node.js azure azure-blob-storage


    【解决方案1】:

    Azure Blob 存储在放置 Blob 时自动将 Blob 的 MD5 哈希(不是 SHA1)存储为属性。您可以使用您正在使用的同一 blob SDK (@azure/storage-blob) 检索 blob 属性。不幸的是,如果您特别需要的话,如果不下载/流式传输 blob,则没有开箱即用的 SHA1 哈希支持。如果 MD5 适合你,那么下面的 sn-p 将在不下载 blob 的情况下获得它。

    const props = await blockBlobClient.getProperties();
    const md5 = props.contentMD5.toString('base64');
    

    注意:当您上传 blob(文件或字节)时,存储会计算 MD5 哈希。但是由于这里是分块上传,所以存储不会计算完整 blob 的 MD5 哈希,因为每个块都是单独写入的。

    【讨论】:

    • 好的先生。我明白了。但是我该如何设置 contentmd5 属性。你能帮我提供示例代码吗?
    • 我正在获取未定义的 contentMD5 值。所以,请您帮我看看如何在 commitblocklist() 方法中设置 contentMD5 属性的代码示例。谢谢先生的帮助。
    • 上传 blob 时无需设置 contentMD5。正如我所提到的,它是由 Azure 存储自动设置的。您可以使用我上面分享的代码 sn-p 来检索它。那返回未定义吗?如果你的 JS 函数不是异步的,那么使用下面的 sn-p:blockBlobClient.getProperties().then(props =>{ const md5 = props.contentMD5.toString('base64'); }) .catch((err)= >console.log(err));
    • 是的,先生。我也在做同样的事情。但是,它返回未定义的先生。
    • blockBlobClient.commitBlockList(blockidarray).then(async (res)=>{ })。 catch((err)=>console.log(err)) }); blockBlobClient.getProperties().then((props)=>{ let prop= props.contentMD5.toString('base64'); console.log('prop',prop); })
    【解决方案2】:
          var hash = crypto.createHash('md5');
          hash.setEncoding('hex');    
          hash.update(filechunk);
          hash.end();
          let val= hash.read();
          //Here stageblock will check userspecifiedmd5value with server calculated value if it matches then it will proceed further or else throw error
          let stageRes = await blockBlobClient.stageBlock(blockID,DATA,fileSize,{transactionalContentMD5:Buffer.from(val,"hex")});
        
    

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 2013-02-03
      • 2010-10-11
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 2017-07-26
      相关资源
      最近更新 更多