【问题标题】:How to serve CDN image urls in node.js via Azure blob storage?如何通过 Azure blob 存储在 node.js 中提供 CDN 图像 URL?
【发布时间】:2021-07-30 11:04:33
【问题描述】:

我目前通过直接获取图片网址向用户提供图片(公共访问)。

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");
        }
    })

我正在使用 https://azure.github.io/azure-storage-node/global.html 官方 nodejs 库与我在上面的云中看到的 azure blob 存储进行通信。

而且我知道您可以将您的 blob 存储“缓存”到这样的 cdn 端点:https://docs.microsoft.com/en-us/azure/cdn/cdn-create-new-endpoint

我正在努力寻找的东西,我怎样才能将 cdn 图像 url 提供给客户端而不是 blob 的直接公共 url?我在他们的 sdk 中看不到与 cdn 相关的任何内容,我想像:“ blobService.getNeartestCdnUrl(containerName, blobName, hostName, ...etc)”?

注意:我刚刚发现了这个旧的未维护包:https://github.com/bestander/deploy-azure-cdn,但它似乎只是用于部署而不是用于服务。

【问题讨论】:

    标签: node.js azure-storage cdn azure-cdn


    【解决方案1】:

    在 Azure 存储前配置 CDN 时,还会使用 CDN 创建一个新端点(例如,https://mycontent.azureedge.net)。全局所有客户端请求都旨在使用此 CDN 端点,由您在 CDN 中配置的 CDN 或规则确定哪些请求被路由到哪里。

    您的 CDN 配置将为其公开的内容的路径和名称设置规则。基本配置将使用原始名称通过 CDN 公开所有 blob,例如:https://mycontent.azureedge.net/path/myblob.txt。如果您知道原始 blob 路径的名称,则可以在代码中将其转换为 CDN 相对路径。

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2017-11-27
      • 2016-09-12
      • 1970-01-01
      • 2017-05-16
      • 2022-12-14
      • 2019-11-05
      • 2020-07-12
      • 2019-04-03
      相关资源
      最近更新 更多