如何使用带有应用程序注册的 javascript SDK 在 azure 中预签名 Url - 应用程序(客户端)ID、客户端 key (值)、Tenant_id 以及帐户名称、容器名称、blob 名称。 我无法生成容器级 SAS token 来临时访问我的文件。
const account = "accountName";
const containerName = "containerName";
const blobName = "blob";
const credential = new ClientSecretCredential(
"AZURE_TENANT_ID",
"AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET"
);
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
credential
);
const containerClient = blobServiceClient.getContainerClient(containerName);
let blobs = containerClient.listBlobsFlat({includeMetadata: true}); // works ok
await credential.getToken(); // how to generate sas token for my container to sign url ?
我不想使用帐户 key ,并且 generateBlobSASQueryParameters
函数似乎适用于帐户 key 。
最佳答案
我已成功执行以下步骤。
必须从存储容器 IAM 将 Storage Blob Delegator
和 Storage Blob Data Reader
角色添加到我的应用程序客户端,并将以下代码添加到上面的角色。
const userDelegationKey = await blobServiceClient.getUserDelegationKey(new Date(), new Date(new Date().valueOf() + 86400));
const containerSAS = generateBlobSASQueryParameters({
containerName,
permissions: ContainerSASPermissions.parse("r"),
startsOn: new Date(),
expiresOn: new Date(new Date().valueOf() + 86400),
version: "2018-11-09"
},
userDelegationKey,
account
).toString();
console.log(`${containerClient.getBlockBlobClient(blobName).url}?${containerSAS}`);
关于azure - 如何从应用程序客户端在azure JS SDK中生成SAS token ,而不使用帐户 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71595155/