I came across some weird behavior. I have to upload file to azure storage and I am using package to upload.

OnProgress callback is getting completed means my file is uploaded successfully. But as soon as I call, azure api to fetch files again it's not returning that latest uploaded file.

If I am calling fetch api in setInterval then in some of the call I am getting this file.

So is it related to concurrency at azure or I am doing something wrong here?

const sassUrl = generateSasUrl() // to generate sasUrl
const azureClient = new ContainerClient(sassUrl).getBlobClient(fileName)

azureClient.uploadData(file , {
    onProgress: ({loadedBytes}) => {
       const isUploaded = loadedBytes === fileSize;
          if(isUploaded){
            fetchAzureFiles()
          }

     }  
})


async function fetchAzureFiles(){
 const url = `https://${accoutnName}/blob.core.windows.net/${containerName}`;
 
 const containerClient = new ContainerClient(containerUrl, new ManagedIdentityCredential(clientId, UAMI_CLient_id));
 
 const files = await containerClient.listAdlsFiles();
 return files



 

}
1

There are 1 best solutions below

1
On

Here is the way i'm able to call, azure api to fetch files again, below code will call the Azure API to fetch files again after uploading a file to the container. This is because the fetchAzureFiles function is called within the uploadFileToAzureStorage function after the file has been uploaded. The fetchAzureFiles function retrieves the list of files in the container, which includes the newly uploaded file.

const { BlobServiceClient, StorageSharedKeyCredential } =  require("@azure/storage-blob");

const  accountName  =  "accountName";
const  accountKey  =  "accountKey";
const  containerName  =  "containerName";
const  fileName  =  "example.txt";
const  filePath  =  "./example.txt"; // Adjust the path based on the location of your file

async  function  uploadFileToAzureStorage() {

const  sharedKeyCredential  =  new  StorageSharedKeyCredential(accountName, accountKey);
const  blobServiceClient  =  new  BlobServiceClient(`https://${accountName}.blob.core.windows.net`, sharedKeyCredential);
const  containerClient  =  blobServiceClient.getContainerClient(containerName);
const  blockBlobClient  =  containerClient.getBlockBlobClient(fileName);

try {

await  blockBlobClient.uploadFile(filePath);

console.log("File uploaded successfully.");

const  fileList  =  await  fetchAzureFiles();

console.log("Files in the container:", fileList);

} catch (error) {
console.error("Error uploading file:", error.message);
}
}

async  function  fetchAzureFiles() {

const  sharedKeyCredential  =  new  StorageSharedKeyCredential(accountName, accountKey);
const  blobServiceClient  =  new  BlobServiceClient(`https://${accountName}.blob.core.windows.net`, sharedKeyCredential);

const  containerClient  =  blobServiceClient.getContainerClient(containerName);
  

const  files  = [];
try {
let  marker  =  undefined;
do {
const  response  =  await  containerClient.listBlobFlatSegment(marker);
marker  =  response.nextMarker;
for (const  blob  of  response.segment.blobItems) {
files.push(blob.name);
}
} while (marker);
return  files;
} catch (error) {
console.error("Error fetching Azure files:", error.message);
throw  error;
}
}

uploadFileToAzureStorage();

Result enter image description here

enter image description here