【发布时间】:2020-04-11 11:45:20
【问题描述】:
我无法从 azure 存储帐户下载 blob。我已经完全按照官方所示 文档here。
文档代码
const { BlobServiceClient } = require("@azure/storage-blob");
const account = "<account name>";
const sas = "<service Shared Access Signature Token>";
const containerName = "<container name>";
const blobName = "<blob name>"
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net${sas}`
);
async function main() {
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
// Get blob content from position 0 to the end
// In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
const downloadBlockBlobResponse = await blobClient.download();
const downloaded = await blobToString(await downloadBlockBlobResponse.blobBody);
console.log(
"Downloaded blob content",
downloaded
);
我的代码
exports.downloadBlob = async (req, res, next)=>{
const ref = req.params.id;
try{
const containerClient = blobServiceClient.getContainerClient('xxxxxxxxx');
const blobClient = containerClient.getBlobClient(ref);
// Get blob content from position 0 to the end
// In Node.js, get downloaded data by accessing downloadBlockBlobResponse.readableStreamBody
const downloadBlockBlobResponse = await blobClient.download();
const downloaded = await streamToString(downloadBlockBlobResponse.readableStreamBody);
console.log("Downloaded blob content:", downloaded);
return res.send(downloaded)
}
catch(err){
return res.send(err)
}
}
我已经传递了 blob 的名称以及当我们保存任何 blob 时它发回的参考号
【问题讨论】:
-
请编辑您的问题并包含您收到的任何错误消息。
-
能否请您检查
ref是否是您的 blob 名称并提供错误信息? -
@JimXu UnhandledPromiseRejectionWarning:错误:意外状态代码:404 at new RestError (D:\wd\javascript\Projects\blue - secure\node_modules\@azure\core-http\dist\coreHttp.node .js:1716:28) 在 D:\wd\javascript\Projects\blue - secure\node_modules\@azure\core-http\dist\coreHttp.node.js:3416:37 在 process._tickCallback (internal/process/ next_tick.js:68:7) (node:9396) UnhandledPromiseRejectionWarning: 未处理的承诺拒绝。
-
@AmaanItiyaz 根据错误,您的 blob 可能不存在。请检查您是否提供了正确的 blob 名称。
-
@Jim Xu ,我现在正在获取流,但它是在 readableStreamBody 中加密的。有什么方法可以解密吗
标签: node.js azure express azure-storage azure-blob-storage