Node.js Azure:尝试获取blob内容时函数停止工作
我正在使用nodeJS和Azure函数。我正在尝试获取blob pptx的内容,然后进一步使用该pptx使用admzip解压它 然而,每当我尝试获取内容时,函数就会停止,没有任何错误,并且在一段时间后超时。我首先尝试获取blob的属性,以检查blob是否存在并且是否有效 以下是我的功能:Node.js Azure:尝试获取blob内容时函数停止工作,node.js,azure,azure-functions,azure-storage-blobs,Node.js,Azure,Azure Functions,Azure Storage Blobs,我正在使用nodeJS和Azure函数。我正在尝试获取blob pptx的内容,然后进一步使用该pptx使用admzip解压它 然而,每当我尝试获取内容时,函数就会停止,没有任何错误,并且在一段时间后超时。我首先尝试获取blob的属性,以检查blob是否存在并且是否有效 以下是我的功能: const storage = require('azure-storage'); const STORAGE_ACCOUNT_NAME = 'storage-account'; const ACCOUNT_A
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
});
}
});
})
}
如您所见,我尝试了getBlobToStream和getBlobToText,但结果相同。GetBlobProperty工作正常,我获得了关于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' } }
问题可能是api已更改。我刚刚在下面检查过,回调函数在getBlobToText中只接受两个参数:
我能够用代码解决这个问题
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();
});
};
使用
你会看到
细节
因此,getBlobToText的问题是,它试图以字符串形式返回缓冲区对象,但无法验证MD5。我在某个地方读到,可以使用write-to-stream函数来写入缓冲区而不是文件,但我现在找不到它
我可能会抓取一些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}`);
}
这是我在应用程序中使用的工作代码
return new Promise(async r => {
bst.getBlobToText(containername, name, (err, text) => r(err ? null : text));
})
可能不相关,但为什么您的内容类型为“image/jpeg”?这也是运行时v1或v2?节点的哪个版本?@AdamMarczak其节点版本11.9和运行时版本:2.0.12641.0~2。内容类型可以忽略我添加了一个图像,以查看powerpoint类型是否有问题,并忘记添加pptx的输出,但尽管内容类型相同,输出将完全相同,稍后更新还记得在2.0中,您需要调用context.done;或导出异步函数模块。导出=异步函数。但我不确定这是否是个问题,我回家后会检查一下。@AdamMarczak谢谢!我将尝试context.done,并将更新outputI重新创建的OP场景,并使用您的代码对其进行测试。错误消息与从连接读取的字节数不正确的消息相同。连接可能已关闭,因此很遗憾,这无法解决问题。您的代码将适用于文本文件,而不是二进制缓冲区文件。@threxx有帮助吗?您测试过这个吗?是的,错误处理工作正常。我假设您不想直接保存到文件,这是什么意思?我需要文件,更改一些内容,然后将其保存回去,这是否仍然适用于fs库和streams?我的意思是不建议将文件直接保存为“file.pptx”,因为并行执行此操作时可能会遇到问题。最好的解决方案是使用内存流,而不是将其保存在系统驱动器上。
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.
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}`);
}
return new Promise(async r => {
bst.getBlobToText(containername, name, (err, text) => r(err ? null : text));
})