繁体   English   中英

如何使用 Javascript ZF20E3C5E54C0AB3D96F5D660B36 为 Azure 存储生成用户委托 SAS

[英]How to generate user delegation SAS for Azure Storage using Javascript SDK

我正在尝试将文件上传到 Azure Blob 存储。 到目前为止我所做的:

npm i @azure/identity @azure/storage-blob

使用用户委托密钥生成 SAS 查询参数:

async function generateSas() {
    const startDate = new Date();
    const expiryDate = new Date();
    startDate.setTime(startDate.getTime() - 100 * 60 * 1000);
    expiryDate.setTime(expiryDate.getTime() + 100 * 60 * 60 * 1000);

    const credential = new DefaultAzureCredential();
    const blobServiceClient = new BlobServiceClient(STORAGE, credential);
    const key = await blobServiceClient.getUserDelegationKey(startDate, expiryDate);

    return generateBlobSASQueryParameters({
        containerName: CONTAINER,
        startsOn: startDate,
        expiresOn : expiryDate,
        permissions: ContainerSASPermissions.parse('rwl'),
    }, key, ACCOUNT).toString();
}

使用生成的SAS上传

async function upload(sasToken: string) {
    const blobClient = new BlockBlobClient(
      `https://${ACCOUNT}.blob.core.windows.net/${CONTAINER}/test.json?${sasToken}`);
    const content = 'some content';
    const response = await blobClient.upload(content, content.length);
}

在我运行它之前,我使用我的帐户进行az login

错误:

(node:19584) UnhandledPromiseRejectionWarning: RestError: This request is not authorized to perform 
this operation using this permission. 

如果我使用相同的登录名从Azure 存储资源管理器复制 SAS,则代码有效。 所以我假设有某种方法可以为我的帐户检索有效的 SAS。

我怀疑这是一个权限问题

在仔细分析无法列出 azure 数据湖的文件系统与 javascriptManagedIdentityCredential failed when used to list blob container #5539问题后,我认为Owner角色不足以在您的 blob 存储帐户中上传 blob。 您必须使用Storage Blob Data *角色之一(如Storage Blob Data Owner ,然后才能上传 Blob。

因此,请尝试将Storage Blob Data Owner角色添加到您的预期用户并尝试再次运行代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM