简体   繁体   中英

Mock azure create file - delete file on blob storage using nodejs

I want to mock the creation/deletion of a file on blob storage and I don't know how to do it I did this as:

//create file
  const blobService = azure.createBlobService(storageAccountName, accessKey);
  await workbook.stream.pipe(
      blobService.createWriteStreamToBlockBlob(containerName, blobName)
  );

//Delelete file:

 blobService.deleteBlobIfExists(containerName, fileName, (err: any, result: any) => {
    if (err) { reject(err);
    } else { resolve({statusCode: 200});}
});

I did that to get connection information:

import { BlobServiceClient } from '@azure/storage-blob';
BlobServiceClient.fromConnectionString = jest.fn().mockResolvedValueOnce('fromConnectionString-test');
console.log(BlobServiceClient.prototype.getAccountInfo());

I've some error that logic because i'm not sur the connection is correctly mocked...

 TypeError: Cannot read property 'getAccountInfo' of undefined

Thank you for your help

You are trying to get the account info from the prototype. However, instead of that, you will need to instantiate BlobServiceClient :

const blobServiceClient = BlobServiceClient.fromConnectionString(
  AZURE_STORAGE_CONNECTION_STRING
);
StorageAccountInfo accountInfo = blobServiceClient.getAccountInfo();

See

https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-nodejs?tabs=environment-variable-windows https://learn.microsoft.com/en-us/java/api/com.azure.storage.blob.blobserviceclient.getaccountinfo?view=azure-java-stable#com-azure-storage-blob-blobserviceclient-getaccountinfo()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM