【问题标题】:How to generate an azure blob url with SAS signature in nodejs sdk v12?如何在 nodejs sdk v12 中生成带有 SAS 签名的 azure blob url?
【发布时间】:2020-05-01 12:39:24
【问题描述】:

以前(在像 v2 这样的旧 sdk 中)您可以生成一个 sas url(一个 blob 的签名可共享 url),如下所示:

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

var startDate = new Date();
var expiryDate = new Date(startDate);
expiryDate.setMinutes(startDate.getMinutes() + 100);
startDate.setMinutes(startDate.getMinutes() - 100);

var sharedAccessPolicy = {
  AccessPolicy: {
    Permissions: azure.BlobUtilities.SharedAccessPermissions.READ,
    Start: startDate,
    Expiry: expiryDate
  }
};

var token = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy);
var sasUrl = blobService.getUrl(containerName, blobName, token);

我想知道我们如何在 sdk v12 中生成该 url? 我在 v12 中找不到任何有关 Sas URL 的文档。

BlobUtilities 和 getUrl() 方法在 v12 中也不可用(在 v12 中,每个模块都有单独的包,在我的情况下,我使用的是 require("@azure/storage-blob");)

谢谢。

【问题讨论】:

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


    【解决方案1】:

    关于问题,请参考以下代码

    var storage = require("@azure/storage-blob")
      const accountname ="blobstorage0516";
        const key = "";
        const cerds = new storage.StorageSharedKeyCredential(accountname,key);
        const blobServiceClient = new storage.BlobServiceClient(`https://${accountname}.blob.core.windows.net`,cerds);
        const containerName="test";
        const client =blobServiceClient.getContainerClient(containerName)
        const blobName="help.txt";
        const blobClient = client.getBlobClient(blobName);
    
        const blobSAS = storage.generateBlobSASQueryParameters({
          containerName, 
          blobName, 
          permissions: storage.BlobSASPermissions.parse("racwd"), 
          startsOn: new Date(),
          expiresOn: new Date(new Date().valueOf() + 86400)
        },
        cerds 
      ).toString();
    
        const sasUrl= blobClient.url+"?"+blobSAS;
        console.log(sasUrl);
    

    【讨论】:

    • 这是我找到的最佳答案。我尝试从头开始编写可能并不断得到签名不匹配。我复制并粘贴了这段代码,更改了详细信息并且 SAS url 有效。我花了 6 个小时来解决这个问题。谢谢一百万。
    • 如果您希望使用signedURL 上传Blob,请不要忘记在请求标头中添加x-ms-blob-type: BlockBlob,否则会失败。参考这里:docs.microsoft.com/en-us/rest/api/storageservices/…
    【解决方案2】:

    您可以使用generateBlobSASQueryParameters 来执行此操作。例如,看下面的代码:

    const AZURE_STORAGE_ACCOUNT = 'account-name';
    const AZURE_STORAGE_ACCESS_KEY = 'account-key';
    const { StorageSharedKeyCredential, BlobServiceClient, generateBlobSASQueryParameters, BlobSASPermissions } = require("@azure/storage-blob");
    const sharedKeyCredential = new StorageSharedKeyCredential(AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_ACCESS_KEY);
    
    
    const blobServiceClient = new BlobServiceClient(
      `https://${AZURE_STORAGE_ACCOUNT}.blob.core.windows.net`,
      sharedKeyCredential
    );
    
    const containerName = 'container-name';
    const blobName = 'blob-name';
    const containerClient = blobServiceClient.getContainerClient(containerName);
    const blockBlobClient = containerClient.getBlockBlobClient(blobName);
    // const credentials = new StorageSharedKeyCredential()
    const sasToken = generateBlobSASQueryParameters({
      containerName: containerName,
      blobName: blobName,
      expiresOn: new Date(new Date().valueOf() + 86400),
      permissions: BlobSASPermissions.parse("racwd")
    }, sharedKeyCredential);
    
    const sasUrl = `${blockBlobClient.url}?${sasToken}`;
    
    console.log(sasUrl);
    

    【讨论】:

    猜你喜欢
    • 2020-10-12
    • 2021-10-05
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 2021-01-21
    • 2020-06-28
    相关资源
    最近更新 更多