Sarah
Sarah

Reputation: 1293

Azure Blob storage client library get downloading links

This question is regarding which property of the Azure blob storage client library will give me the url of the blob.

I am following the following tutorial to get a list of all blobs in my container. I want to be eventually able to click on a blob and download it.

https://learn.microsoft.com/en-us/azure/storage/blobs/quickstart-blobs-javascript-browser#prerequisites

What I don't know is how to get the SAS URL link for the blob from the code below. Like what property of blobItem will give me the URL to download it from.

blobItem. which property???

What property will allow me to get a downloadable link and then I will need to stream it down.

on this line of code

fileList.innerHTML += `<option value=${blobItem.value.name}>${blobItem.SOME LINK}</option>`;

from the following block

const listFiles = async () => {
    fileList.size = 0;
    fileList.innerHTML = "";
    try {
        reportStatus("Retrieving file list...");
        let iter = containerClient.listBlobsFlat();
        let  blobItem = await iter.next();
        while (!blobItem.done) {
            fileList.size += 1;
           // fileList.innerHTML += `<option>${blobItem.value.name}</option>`;
            fileList.innerHTML += `<option value=${blobItem.value.name}>${blobItem.SOME LINK}</option>`;
            blobItem = await iter.next();
        }
        if (fileList.size > 0) {
            reportStatus("Done.");
        } else {
            reportStatus("The container does not contain any files.");
        }
    } catch (error) {
        reportStatus(error.message);
    }
};

Upvotes: 0

Views: 239

Answers (2)

Sarah
Sarah

Reputation: 1293

I think I got the answer in case it will help someone. This line of code will give the url to the blob item inside a container

${containerClient.getBlobClient(blobItem.value.name).url}

And this is how it can be seen in the document posted above. Replace this line

  fileList.innerHTML += `<option>${blobItem.value.name}</option>`;

with to be able to see the url

fileList.innerHTML += `<option value=${blobItem.value.name}>${containerClient.getBlobClient(blobItem.value.name).url}</option>`;

Upvotes: 1

WaitingForGuacamole
WaitingForGuacamole

Reputation: 4301

According to the documentation at Create a service SAS for a blob:

function getBlobSasUri(containerClient, blobName, sharedKeyCredential, storedPolicyName) {
    const sasOptions = {
        containerName: containerClient.containerName,
        blobName: blobName
    };

    if (storedPolicyName == null) {
        sasOptions.startsOn = new Date();
        sasOptions.expiresOn = new Date(new Date().valueOf() + 3600 * 1000);
        sasOptions.permissions = BlobSASPermissions.parse("r");
    } else {
        sasOptions.identifier = storedPolicyName;
    }

    const sasToken = generateBlobSASQueryParameters(sasOptions, sharedKeyCredential).toString();
    console.log(`SAS token for blob is: ${sasToken}`);

    return `${containerClient.getBlockBlobClient(blobName).url}?${sasToken}`;
}

Upvotes: 0

Related Questions