Azure Blob存储的哈希不匹配(完整性检查失败)

时间:2015-06-03 02:13:04

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

我正在关注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();

5 个答案:

答案 0 :(得分:2)

万一有人碰到这个;当您存储Buffer对象但检索string时会发生。

答案 1 :(得分:1)

可能会发生这种情况,因为许多内部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/

答案 2 :(得分:1)

尝试以下内容(修改您提到的博文中的代码):

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);
        }
    });

看看这是否有帮助。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

存储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));
         }
});