如何使用Node.js将文件上传至Azure容器的子目录?
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

