繁体   English   中英

Azure:尝试获取Blob内容时功能停止工作

[英]Azure: Function stops working when trying to get blob content

我正在使用nodeJS和Azure函数。 我正在尝试获取blob(pptx)的内容,然后进一步使用该pptx(使用admzip解压缩)。

但是,每当我尝试获取内容时,该函数都会停止运行而不会出现任何错误,并且在一段时间后会超时。 我尝试首先获取blob的属性(以检查blob是否存在),并且可以正常工作。

这是我的功能:

const storage = require('azure-storage');
const STORAGE_ACCOUNT_NAME = 'storage-account';
const ACCOUNT_ACCESS_KEY = 'storage-key';
let AdmZip = require('adm-zip');
let fs = require('file-system');

const blobService = storage.createBlobService(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    getBlobProperties('default-powerpoint', 'download.pptx').then((properties) => {
        context.log('Properties: ', properties);
        getBlobContent('default-powerpoint', 'download.pptx').then((content) => {
            context.log('Blob Content: ', content);
        })
    });
};

function getBlobProperties(containerName, fileName) {
    return new Promise((resolve, reject) => {
        blobService.getBlobProperties(
            containerName,
            fileName,
            function (err, properties, status) {
                if (err) {
                    reject(err);
                } else {
                    resolve(properties);
                }
            });
    })
}

function getBlobContentAsStream(containerName, fileName, res) {
    return new Promise((resolve, reject) => {
        blobService.getBlobToStream(containerName, fileName, res, function (err, results) {
            if (err) {
                reject(err);
            } else {
                resolve(JSON.stringify(results, null, 2));
            }
        });
    })
}

function getBlobContent(containerName, blobName) {
    return new Promise((resolve, reject) => {
        blobService.getBlobToText(
            containerName,
            blobName,
            function (err, blobContent, blob) {
                if (err) {
                    reject(err);
                } else {
                    resolve({
                        'content': blobContent,
                        'blob': blob
                    });
                }
            });
    })
}

如您所见,我尝试了getBlobToStreamgetBlobToText但结果相同。 getBlobProperties可以正常工作,我可以获得有关Blob的所有信息,而不仅仅是内容。

谁能帮助我获取Blob的内容。

编辑

如果有人感兴趣,这是属性的输出:

BlobResult {
  container: 'default-powerpoint',
  name: 'download.pptx',
  metadata: {},
  lastModified: 'Wed, 14 Aug 2019 08:28:16 GMT',
  creationTime: 'Wed, 14 Aug 2019 08:28:16 GMT',
  etag: '"something"',
  blobType: 'BlockBlob',
  contentLength: '4658',
  serverEncrypted: 'true',
  requestId: 'someID',
  contentSettings: { contentType: 'image/jpeg' },
  lease: { status: 'unlocked', state: 'available' },
  copy: 
   { id: 'id123',
     status: 'success',
     source: 'sourceURL',
     progress: '4658/4658',
     bytesCopied: 4658,
     totalBytes: 4658,
     completionTime: 'Wed, 14 Aug 2019 08:28:16 GMT' } }

这是我在应用程序中使用的工作代码

  return new Promise(async r => {
            bst.getBlobToText(containername, name, (err, text) => r(err ? null : text));
  })

完整的源代码

我能够使用代码解决此问题

const storage = require('azure-storage');
const fs = require('fs');

const STORAGE_ACCOUNT_NAME = '<account_name>';
const ACCOUNT_ACCESS_KEY = '<access_key>';

const blobService = storage.createBlobService(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);

function getBlobContentAsStream(containerName, fileName, res) {
    return new Promise((resolve, reject) => {
        blobService.getBlobToStream(containerName, fileName, fs.createWriteStream('task1-download.txt'), function(error, serverBlob) {
            if(!error) {
                resolve(serverBlob);
            } else { 
                reject(err); 
            }
        });
    })
}

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    context.log('Starting...');

    getBlobContentAsStream('default-powerpoint', 'download.pptx').then((content) => {
        context.log('Blob Content: ', content);
        context.done();
    }, function(err) {
        console.log.error(err);
        context.done();
    });

};

并跟踪输出

在此处输入图片说明

如果将Functions连接到代码,则您实际上可以在Application Insights跟踪中看到的代码中发生的问题。 您没有收到任何错误,因为您没有为then回调执行程序添加错误处理。

getBlobContent('default-powerpoint', 'download.pptx').then((content) => {
    context.log('Blob Content: ', content);
    context.done();
})

采用

getBlobContent('default-powerpoint', 'download.pptx').then((content) => {
    context.log('Blob Content: ', content);
    context.done();
}, function(err) {
    console.log(err);
    context.done();
}))

你会看到

在此处输入图片说明

详细介绍

在此处输入图片说明

Error: An incorrect number of bytes was read from the connection. 
The connection may have been closed.

因此, getBlobToText的问题是,它尝试将Buffer对象返回为字符串,并且无法验证MD5。 我在某处阅读,可以使用写入流功能写入缓冲区而不是文件,但是现在找不到。

我可能会抓住一些NodeJS内存流库,并尝试在那里输出它,因为我假设您不想直接保存到文件。 但是也许你会做,决定自己。

在此处输入图片说明

如果最终使用“ fs”库,请记住使用推荐的安全非阻塞模式,例如

const fs = require('fs');
const util = require('util');
const readFileAsync = util.promisify(fs.readFile);

module.exports = async function (context) {
    try {
        const data = await readFileAsync('./hello.txt');
    } catch (err) {
        context.log.error('ERROR', err);
        // This rethrown exception will be handled by the Functions Runtime and will only fail the individual invocation
        throw err;
    }
    context.log(`Data from file: ${data}`);
}

问题可能在于api已更改。 我在下面检查过,回调函数在getBlobToText仅接受两个参数:

https://github.com/Azure-Samples/storage-blobs-node-quickstart/blob/master/index.js

const downloadBlob = async (containerName, blobName) => {
    const dowloadFilePath = path.resolve('./' + blobName.replace('.txt', '.downloaded.txt'));
    return new Promise((resolve, reject) => {
        blobService.getBlobToText(containerName, blobName, (err, data) => {
            if (err) {
                reject(err);
            } else {
                resolve({ message: `Blob downloaded "${data}"`, text: data });
            }
        });
    });
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM