【问题标题】:Create shareable URI from azure blob storage with SAS (NODEJS)使用 SAS (NODEJS) 从 azure blob 存储创建可共享的 URI
【发布时间】:2020-03-18 08:32:03
【问题描述】:

我正在尝试为 azure blob 存储中的单个文件创建具有共享访问签名的 URI。我想在我的后端创建这个并将链接发送到我的角度前端,以便用户可以直接下载文件。

我在这里找到了一个类似的 C# 示例:How to download a file to browser from Azure Blob Storage

但是,我无法为 NodeJS 找到正确的方法。我查看了https://docs.microsoft.com/de-de/rest/api/storageservices/create-user-delegation-sas,但我不确定如何使用该信息。

【问题讨论】:

    标签: node.js angular azure download


    【解决方案1】:

    这是获取 blob sas url 的示例代码。

    var azure = require('azure-storage');
    var blobService = azure.createBlobService('storage connection');
    // Create a SAS token that expires in an hour
        // Set start time to five minutes ago to avoid clock skew.
        var startDate = new Date();
        startDate.setMinutes(startDate.getMinutes() - 5);
        var expiryDate = new Date(startDate);
        expiryDate.setMinutes(startDate.getMinutes() + 60);
    
        permissions = azure.BlobUtilities.SharedAccessPermissions.READ;
    
        var sharedAccessPolicy = {
            AccessPolicy: {
                Permissions: permissions,
                Start: startDate,
                Expiry: expiryDate
            }
        };
        var container='test';
        var blobName='test.txt';
        var sasToken = blobService.generateSharedAccessSignature(container, blobName, sharedAccessPolicy);
        var url=blobService.getUrl(container,blobName,sasToken);
        console.log(url);
    

    【讨论】:

    • 感谢您的快速回复。我设法使用提供的示例创建了一个 URL。但是,该 url 不包含参数“se”和“st”,因此在使用创建的 URL 时出现错误“se 是必需的。不能为空”。我使用相同的代码来创建日期和 sharedAccessPolicy。
    • 请分享您的代码,因为检查我的图片,有'se'和'st'。
    • 我找到了问题,我在“AccessPolicy”中有错字:)。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 2020-10-05
    • 2022-01-07
    • 2023-04-04
    • 1970-01-01
    • 2018-04-23
    • 2014-02-22
    • 2020-09-19
    相关资源
    最近更新 更多