【问题标题】:Hash mismatch (integrity check failed) with Azure Blob StorageAzure Blob 存储的哈希不匹配(完整性检查失败)
【发布时间】:2015-08-17 01:10:32
【问题描述】:

我正在关注http://willi.am/blog/2014/07/03/azure-blob-storage-and-node-downloading-blobs/

尽管代码完全相同,但当我下载 blob 时,Azure 会给出错误:

[错误:哈希不匹配(完整性检查失败),预期值为...]

正在运行的行是 blobService.getBlobToText,其中 blobService 是与 Azure 的连接(createBlobService...)

发生了什么事? :S

我的代码如下:

    // Azure test
    function downloadImageAsText(blobService, containerName, blobName) {

         blobService.getBlobToText(
              containerName,
              blobName,
              function(err, blobContent, blob) {
                  if (err) {
                      console.error("Couldn't download blob %s", blobName);
                      console.error(err);
                  } else {
                      console.log("Sucessfully downloaded blob %s", blobName);
                      console.log(blobContent);
                  }
              });

    }

    function uploadImage(blobService, containerName, blobName, fileName) {

      blobService.getBlobProperties(
        containerName,
        blobName,
        function(err, properties, status) {
            if (status.isSuccessful) {
                // Blob exists
            } else {
                blobService.createBlockBlobFromLocalFile(
                    containerName,
                    blobName,
                    fileName,
                    function(error, result, response){
                        if(error){
                            console.log("Couldn't upload file %s", fileName);
                            console.error(error);
                        } else {
                            console.log('File %s uploaded successfully', fileName);
                            downloadImageAsText(blobService, containerName, blobName);
                        }
                    });
            }
        });
    }


    function testAzure() {

      accountName / hash = my details

      var storage = require('azure-storage');
      var blobService = storage.createBlobService(accountName, hash);
      var containerName = 'tst';
      var blobName = 'test.png';
      var fileName = 'test.png';

      blobService.createContainerIfNotExists(containerName, function(err, result, response) {
          if (err) {
              console.log("Couldn't create container %s", containerName);
              console.error(err);
          } else {
              if (result) {
                  console.log('Container %s created', containerName);
                  uploadImage(blobService, containerName, blobName, fileName);
              } else {
                  console.log('Container %s already exists', containerName);
                  uploadImage(blobService, containerName, blobName, fileName);
              }
          }
      });
    }

    function startServer() {
      http = require('http');
      const PORT = 8080;
      var server = http.createServer(handleRequest);
      server.on('listening',function(){
        console.log("Server listening on: http://178.62.117.207:%s", PORT);
      });
      server.listen(PORT);
    }

    startServer();
    testAzure();

【问题讨论】:

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


    【解决方案1】:

    万一其他人遇到这种情况;当您存储 Buffer 对象但检索 string 时发生。

    【讨论】:

    • 知道该怎么做吗?
    • @K48 IIRC,您将Buffer 存储在blob 中,然后使用getBlobToText 将该数据作为String 检索。由于某种原因,两种类型的哈希计算不同,因此操作失败。
    【解决方案2】:

    尝试以下方法(修改您提到的博客文章中的代码):

    var blobName = 'my-awesome-text-blob';
    blobService.getBlobToText(
        containerName,
        blobName, {'disableContentMD5Validation': true },
        function(err, blobContent, blob) {
            if (err) {
                console.error("Couldn't download blob %s", blobName);
                console.error(err);
            } else {
                console.log("Sucessfully downloaded blob %s", blobName);
                console.log(blobContent);
            }
        });
    

    看看这是否有帮助。

    【讨论】:

    • 现在得到错误:错误:从连接中读取的字节数不正确。连接可能已关闭..
    • 我认为当内容长度不匹配时会出现此错误。您的文档是否包含重音字符?这可能与您的问题有关:github.com/azure-contrib/node-azure-search/issues/4
    • 如果是图片,为什么要下载为文本?更好的功能是getBlobToStream: github.com/Azure/azure-storage-node/blob/master/lib/services/… 恕我直言。
    • 或者,如果您在上传图像时需要使用文本,请将其转换为 base64 编码字符串并上传,而不是二进制数据。
    • 感谢所有帮助 - 将尝试使用 getBlobToStream 并执行此操作。 toText 函数不起作用似乎仍然很奇怪,要么它坏了,微软需要修复它,要么他们需要更好的错误处理,因为我得到的错误实际上是无用的。 :P
    【解决方案3】:

    这可能是因为许多内部 MD5 检查失败,当您使用 HTTPS 时工作方式不同。您可以尝试将您的存储帐户指定为 https 吗?喜欢 -

    var blobService = storage.createBlobServiceAnonymous('https://MyAccountXX.blob.core.windows.net/');

    否则对我来说,这个下载功能工作正常。

    作为参考,您可以尝试以下实际文档 - https://azure.microsoft.com/en-in/documentation/articles/storage-nodejs-how-to-use-blob-storage/

    【讨论】:

    • 然而,在文档中,他们将 blob 下载到服务器。这也适用于我 - 不起作用的是 getBlobToText,它被提及但未演示。
    【解决方案4】:

    当您存储一个 Buffer 对象时,您可以使用 getBlobToStream 检索它。

    const data: Buffer[] = [];
    const stream = new PassThrough();
    
    stream.on('data', (d: Buffer) => {
        data.push(d);
    });
    
    this.blobStorageContext.service.getBlobToStream(
        this.blobStorageContext.getContainerName(),
         blobName, stream, (error) => {
             if (error) {
               console.log(error);
             } else { 
               console.log(Buffer.concat(data));
             }
    });
    

    【讨论】:

      【解决方案5】:

      我最近自己也遇到了这个问题。不知何故 npm -install azure-storage -g 帮助了它。可能是包在 npm 上更新了,md5 计算从那以后发生了变化。一旦我更新了 azure-storage 包,一切都像魅力一样。

      【讨论】:

        猜你喜欢
        • 2020-01-27
        • 1970-01-01
        • 2016-04-27
        • 2020-10-19
        • 2022-10-08
        • 1970-01-01
        • 2013-12-16
        • 1970-01-01
        相关资源
        最近更新 更多