0
votes

I have a node.js application and I want to use Azure Blob storage as my place to upload files and download files through Node.js

I have followed instructions from : https://github.com/Azure-Samples/storage-blob-upload-from-webapp-node/tree/master/

I have created my blob storage account but now when I run my application I get the following errors:

 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
1
May be the credentials to your StorageAccount are not use properly. could you point at what line that Exception occured?Reinhard
I updated the question with error detailsMJ X
Error says that the container doesn’t exist. Can you please check that? Also, please edit your question and include the code.Gaurav Mantri
I have created the container and put the connection string as stated in the MS Azure siteMJ X
@MJVM Is it useful for you? If it is useful, could you please accept the answer? It may help more people.Jim Xu

1 Answers

0
votes

According to my test, we can use the following code to upload and download 1. install sdk

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. code
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;

  });

For more details, please refer to the document and the sample