首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复Node.js azure blob在azure blob存储中上载和下载文件

如何修复Node.js azure blob在azure blob存储中上载和下载文件
EN

Stack Overflow用户
提问于 2020-02-02 20:00:56
回答 1查看 1.1K关注 0票数 0

我有一个node.js应用程序,我想使用Azure存储作为我通过Node.js上传文件和下载文件的地方

我遵循了以下指示:https://github.com/Azure-Samples/storage-blob-upload-from-webapp-node/tree/master/

我已经创建了blob存储帐户,但是现在当我运行应用程序时,会发现以下错误:

代码语言:javascript
复制
 There was an error contacting the blob storage container.

 StorageError: The specified container does not exist.

 \node_modules\azure-storage\lib\common\services\storageserviceclient.js:1191:23)

node_modules\azure-storage\lib\common\services\storageserviceclient.js:738:50

node_modules\azure-storage\lib\common\services\storageserviceclient.js:311:37)

node_modules\request\request.js:188:22

node_modules\request\request.js:1171:10
EN

回答 1

Stack Overflow用户

发布于 2020-02-04 01:46:54

根据我的测试,我们可以使用以下代码上传和下载1.installSDK

代码语言:javascript
复制
my package.json

"dependencies": {
    "@azure/abort-controller": "^1.0.1",
    "@azure/storage-blob": "^12.0.2",    
    "fs": "0.0.1-security",
    "stream": "0.0.2"
  }
  1. 代码
代码语言:javascript
复制
const accountname ="blobstorage0516";
    const key = "your account key";
    const cerds = new storage.StorageSharedKeyCredential(accountname,key);
    const containerName="test";
    const pipeline = storage.newPipeline(cerds, {
      retryOptions: { maxTries: 4 }, // Retry options
      userAgentOptions: { userAgentPrefix: "AdvancedSample V1.0.0" }, // Customized telemetry string
      keepAliveOptions: {
        // Keep alive is enabled by default, disable keep alive by setting false
        enable: false
      }
    });
    const blobServiceClient =new storage.BlobServiceClient( `https://${accountname}.blob.core.windows.net`,pipeline)
    var containerClient =blobServiceClient.getContainerClient(containerName)
    if(!containerClient.exists()){
       console.log("the container does not exit")
       await containerClient.create()

    }
    const blobNmae="test.jpg";
    const localFilePath="D:\\download\\test.jpg";
    const blockBlobClient =containerClient.getBlockBlobClient(blobNmae)
    // upload
    await blockBlobClient.uploadStream(fs.createReadStream(localFilePath), 4 * 1024 * 1024, 20, {
    abortSignal: AbortController.timeout(30 * 60 * 1000)})
    // download
    const downloadBlockBlobResponse = await blockBlobClient.download(0);
    const  datastream =  new stream.PassThrough();
    const  readableStream = downloadBlockBlobResponse.readableStreamBody
    readableStream.on("data", data => {
      datastream.push(data);

    });
    readableStream.on("end" , () => {
      fs.writeFileSync('D:\\test1.jpg', datastream.read())
      console.log("download successfully")
      datastream.destroy();

  });
  readableStream.on("error" , (error) => {

    datastream.destroy();
    throw error;

  });

有关更多细节,请参阅文档示例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60030053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档