Hello Gabe,
Thank you for posting your query here!
I understand that you are trying to download a json file from Blob storage container using Access keys or connection string from the storage account instead of Shared access tokens.
Here is a JavaScript code that uses the @azure/storage-blob library to download a blob from Azure Blob Storage. The code provided is using a connection string for authentication.
const { BlobServiceClient } = require('@azure/storage-blob');
const fs = require("fs");
const connectionString = "<your-connection-string>"; // Replace with your actual connection string
const containerName = "<your-container-name>"; // Replace with your actual container name
const blobName = "<your-blob-name>"; // Replace with your actual blob name
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
// Download the blob
const downloadBlockBlobResponse = await blobClient.download();
// Convert the readable stream to a string
const downloadedContent = await streamToString(downloadBlockBlobResponse.readableStreamBody);
console.log("Downloaded blob content:", downloadedContent);
// Function to convert a readable stream to a string
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data.toString());
});
readableStream.on("end", () => {
resolve(chunks.join(""));
});
readableStream.on("error", reject);
});
}
Please note that using keys in code is not considered a recommended practice for security reasons. It's generally better to use Azure Managed Identities or Shared Access Signatures (SAS) for better security and easier rotation. If possible, consider using Azure Key Vault to store and manage secrets securely.
As you are facing issue with SAS token due to expiry, I would recommend checking the following article to configure SAS expiration policy: https://learn.microsoft.com/en-us/azure/storage/common/sas-expiration-policy?tabs=azure-portal#how-to-configure-a-sas-expiration-policy
You can also upload and download data between your local machine and Azure Storage accounts with the help of Azure Storage Explorer, you can connect it to Storage accounts using connection strings: https://learn.microsoft.com/en-us/azure/vs-azure-tools-storage-manage-with-storage-explorer?tabs=windows#account-name-and-key
Please let us know if you have any further queries. I’m happy to assist you further.
Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.