Search code examples
javascriptangularnpmazure-blob-storageazure-storage

Azure Storage Javascript library getcontainerclient error


I am using @azure/storage-blob library in angular. I get the BlobServiceClient and then call the getContainerClient method and recieve the following error:

"Uncaught (in promise): TypeError: Failed to construct 'URL': Invalid URL TypeError: Failed to construct 'URL': Invalid URL"

var client = new BlobServiceClient(`https://mystorage.blob.core.windows.net${sas}`);
let containers = client.listContainers();
const containerClient = client.getContainerClient('containerName');

I've search but an unable see if I am doing something wrong..


Solution

  • To perform a GetContainerClient operation, you would need a service SAS. This can be created from the Azure storage account portal as shown below:

    enter image description here

    Sample code to call the GetContainerClient using SAS token is here:

    const { BlobServiceClient } = require("@azure/storage-blob");
    
    const account = "<account name>";
    const sas = "<service Shared Access Signature Token>";
    const containerName = "<container name>";
    const blobName = "<blob name>";
    
    const blobServiceClient = new BlobServiceClient(`https://${account}.blob.core.windows.net${sas}`);
    
    async function main() {
      const containerClient = blobServiceClient.getContainerClient(containerName);
      const blobClient = containerClient.getBlobClient(blobName);
    ...
    ...
    
    }
    

    If you want to create Service SAS token for a Azure Blob Storage container using javascript, you can refer this sample code. Hope this helps.