Sample Code: Upload a File (Node.js)
Use the Azure Storage File Share SDK to upload a local file to a share.
// npm install @azure/storage-file-share
const { ShareServiceClient } = require("@azure/storage-file-share");
const fs = require("fs");
const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING;
const serviceClient = ShareServiceClient.fromConnectionString(connectionString);
async function uploadFile() {
const shareName = "myshare";
const directoryName = "";
const fileName = "example.txt";
const shareClient = serviceClient.getShareClient(shareName);
const directoryClient = shareClient.getDirectoryClient(directoryName);
const fileClient = directoryClient.getFileClient(fileName);
const fileContent = fs.readFileSync("./example.txt");
await fileClient.create(fileContent.length);
await fileClient.uploadRange(fileContent, 0, fileContent.length);
console.log("File uploaded successfully");
}
uploadFile().catch(console.error);
Toggle Syntax Highlighting