You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Node.js将文件上传至Azure容器的子目录?

Uploading Files to Azure Blob Storage Subdirectories with Node.js

Hey there! I get it—you’ve got file uploads working to a root Azure container with Node.js, but can’t find docs on targeting subdirectories, even though C# and Java make this seem straightforward. Let’s fix that!

First, a quick key point about Azure Blob Storage: subdirectories aren’t actual physical folders—they’re just a visual abstraction. Blobs are organized using virtual paths in their names, using / as the separator. So to upload to a "subdirectory", you just need to include that path in your blob name.

Here’s how to adjust your existing code:

Instead of passing a plain filename for BlobName, use a path like subfolder1/subfolder2/your-file.txt. Azure will automatically render this as nested folders in the portal, and treat the path as part of the blob’s identifier.

Modified code snippet:

var azure = require('azure-storage'); // Ensure you have this package installed
var BlobService = azure.createBlobService(storageAccount, accessKey);

// Include the virtual subdirectory path in the blob name
const blobNameWithPath = "my-subdirectory/uploaded-file.txt"; 
const filePath = "./local-file.txt";

BlobService.createBlockBlobFromLocalFile(container, blobNameWithPath, filePath, function(error, result, response) {
  if (error) {
    console.log("Upload failed:", error);
  } else {
    console.log("Successfully uploaded to azure: ", result.name);
  }
});

Want to "create" an empty subdirectory (for visibility)?

If you want the subdirectory to show up in the Azure portal even without files in it, you can upload an empty blob with a name ending in / (e.g., my-subdirectory/). This tells the portal to display it as a folder:

BlobService.createBlockBlobFromText(container, "my-subdirectory/", "", function(error) {
  if (!error) {
    console.log("Virtual subdirectory created");
  }
});

That’s all there is to it! The Node.js SDK handles the virtual path just like the C#/Java SDKs—you just need to format the blob name correctly.

内容的提问来源于stack exchange,提问作者Sk Sunny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:52:04