I have a page developed in NodeJs and I want to pass a file.txt from my local to my storage account using azcopy-node.The code I currently have is as follows:
const { AzCopy } = require("@azure-tools/azcopy-node");
const azCopy = new AzCopy({
accountName: "myaccount",
accountKey: "mykey",
});
// Copy a local file to a storage account
azCopy.copy(__dirname + "/../../auto_load_event/1239.txt", "https://mystorage.blob.core.windows.net/container/1239.txt");
The problem I have is that when running it tells me that azcopy is not a constructor, and when I see the documentation of this link: https://www.npmjs.com/package/@azure-tools/azcopy-node I tried the code shown as an example and it does not work for me either, the error that shows me with this code is:
TypeError: Cannot read properties of undefined (reading 'fromTo')
at AzCopyClient.copy (D:\WebApp\node_modules\@azure-tools\azcopy-node\dist\src\AzCopyClient.js:105:80)
at Object.redirectFILE (D:\WebApp\src\security\Centinela\redireccion_nativo.js:11:30)
at Object.<anonymous> (D:\WebApp\src\index.js:97:5)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
I would like to be able to pass .TXT files from my local to my storage_account using Azcopy or if you know a better way to transfer files I thank you for sharing the information
If you know a better way to transfer files I thank you for sharing the information
You can try with @azure/storage-blob
package to copy a file from local to Azure blob storage.
Code:
const { BlobServiceClient } = require("@azure/storage-blob");
const fs = require("fs");
async function uploadFileToBlobStorage() {
const connectionString = "<your-storage connection string>";
const containerName = "<container name>";
const blobName = "sample.txt";
const filePath = "C:\\Users\\v-xxx\\xxx\\xxx\\file.txt";
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadOptions = {
bufferSize: 4 * 1024 * 1024,
maxBuffers: 20
};
const stream = fs.createReadStream(filePath);
const uploadResult = await blockBlobClient.uploadStream(stream, uploadOptions.bufferSize, uploadOptions.maxBuffers);
console.log("Upload successful");
}
uploadFileToBlobStorage();
Output:
Reference:
Upload a blob with JavaScript - Azure Storage | Microsoft Learn