0
votes

I am currently serving images (public access) to users by getting the image urls directly.

const azure = require('azure-storage');
const blobService = azure.createBlobService();

router.route('/image').get( async (req, res, next) => {
        try{
            let containerName = process.env.CLOUD_PUBLIC_CONTAINER;
            let category = req.query.category;
            let blobName = category + '/'+ req.query.filename;
            let hostName = process.env.AZURE_STORAGE_HOST || 'https://storageaxxxxxx.blob.core.windows.net/';
            let url = blobName ? blobService.getUrl(containerName, blobName, null, hostName) : null;
            res.json(url)
            
        }catch(e){
            console.log(e);
            res.status(500).send("Internal Error");
        }
    })

I am using the https://azure.github.io/azure-storage-node/global.html official nodejs library to communicate with my azure blob storage as you see in the above cloud.

And I know that you can 'cache' your blob storage to a cdn endpoint like this: https://docs.microsoft.com/en-us/azure/cdn/cdn-create-new-endpoint

The thing that I am struggling to find out, how can I serve the cdn image url to client instead of the blob's direct public url? I see nothing in their sdk related to cdn, I imagine something like: " blobService.getNeartestCdnUrl(containerName, blobName, hostName, ...etc)" ?

Note: I just found this old unmaintained package: https://github.com/bestander/deploy-azure-cdn but it's for deployment only it seems and not for serving.

1

1 Answers

0
votes

When you configure a CDN in front of your Azure Storage, you also create a new endpoint with the CDN (for example, https://mycontent.azureedge.net). All client requests globally are meant to use this CDN endpoint, with the CDN or rules you configure in the CDN determining which requests are routed where.

Your CDN configuration will have rules for paths and names of content it exposes. A basic config would have all blobs exposed through the CDN using their original names, for example: https://mycontent.azureedge.net/path/myblob.txt. If you knew the name of the original blob path, you could transform it to the CDN relative path in code.