I am using @azure/storage-blob:^12.8.0, @azure/storage-queue: ^12.7.0 packages to authenticate and connect to Azure blob storage, authentication goes through Managed Identities and the destination granted access with Storage Blob Data Contributor role, below is my code
const credentialOptions = {
managedIdentityClientId: process.env.MANAGED_IDENTITY_CLIENT_ID
};
const CONTAINER_NAME = 'C1';
const credential = new DefaultAzureCredential(credentialOptions);
const blobSvc = new BlobServiceClient(process.env.STORAGE_HOST, credential);
const containerClient = blobSvc.getContainerClient(CONTAINER_NAME);
export const store = async (file: string, folder: string) => {
const fileName = path.basename(file);
const blob = `${folder}/${fileName}`;
try {
console.debug(`starting store ${blob}...`);
const blobClient = containerClient.getBlockBlobClient(blob);
await blobClient.uploadFile(file);
console.debug('store done.');
return blob;
}
catch (err) {
console.error(err);
throw Error(err);
}
};
// in other module
import {store} from 'BlobStorageHelper';
store('folder1/folder2/folder3', '/temporary/path/to/the/file.jpg');
everything works except that the file is stored in the wrong place, for example, by calling store('folder1/folder2/folder3', 'path/file.jpg') I expect the file to be stored at folder1/folder2/folder3/file.jpg but the file gets stored at C1/folder1/folder2/folder3/file.jpg C1 is the name of the container which is wrong and should not happen, the code runs as Azure functions node.
to make it clear, the expectation is
but the actual result is
Any idea ?
Try with this code , I tried this code in my system able to store the file with folder structure in azure container
const blobServiceClient = BlobServiceClient.fromConnectionString("Connection String");
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobName="test1/test2 "+ "/" + "test"
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const file="C: \\blobs\\test.txt"
await blockBlobClient.uploadFile(file);
console.log('\nUploading to Azure storage as blob:\n\t', blobName);
OUTPUT
The files are saved in container in azure storage
The problem was a wrong value coming from the AppSettings which was not obvious in the code. it should be https://accountname.blob.core.windows.net but instead, it was https://accountname.blob.core.windows.net/ContainerName.