gpt4 book ai didi

azure - 如何在 NodeJS 中为 Azure 存储容器生成共享访问签名 token

转载 作者:行者123 更新时间:2023-12-03 06:14:57 25 4
gpt4 key购买 nike

目前,我在节点 JS 中有代码,可以为指定的 Azure 存储容器内的 blob 生成 SAS token 。我希望能够生成一个 SAS token ,该 token 将授予用户对整个容器本身的访问权限,而不是为每个单独的 blob 生成 token 。我该怎么做呢?下面是当前为 blob 生成 token 的代码。

const containerName = "samplecontainer";
const blobName = "sample.csv";

const account = "sampleaccount";
const key = process.env["StorageConKey"]; // storage connection key
const credentials = new storage.StorageSharedKeyCredential(account, key);

const blobSASToken = storage.generateBlobSASQueryParameters({
containerName,
blobName,
permissions: storage.BlobSASPermissions.parse("r"),
startsOn: new Date(),
expiresOn: new Date(new Date().valueOf() + 60000)
},
credentials).toString();

最佳答案

I want to be able to generate a SAS token that will grant the user access to the entire container itself.

您可以使用以下 node.js 代码创建容器 SAS token 和带有 SAS token 的容器 URL。

代码:

const {BlobServiceClient,
ContainerSASPermissions,
generateBlobSASQueryParameters,
SASProtocol,StorageSharedKeyCredential} = require("@azure/storage-blob");

async function createContainerSas(){
const accountName = "venkat123";
const containerName = "test";
const accountkey = "Your-account-key"

const credential = new StorageSharedKeyCredential(accountName, accountkey);
// Create a BlobServiceClient object using the StorageSharedKeyCredential object
const blobServiceClient = new BlobServiceClient(`https://${accountName}.blob.core.windows.net`, credential);
// Get a reference to a container
const containerClient = blobServiceClient.getContainerClient(containerName);

const sasOptions = {
containerName,
protocol: SASProtocol.HttpsAndHttp
};
sasOptions.permissions = ContainerSASPermissions.parse('c');
sasOptions.expiresOn = new Date(new Date().valueOf() + 3600*1000);
const sasToken = generateBlobSASQueryParameters(sasOptions, credential).toString();

console.log(`SAS token for blob container is: ${sasToken}`);
console.log(`${containerClient.url}?${sasToken}`);
}
createContainerSas()

输出:

SAS token for blob container is: sv=2022-11-02&spr=https%2Chttp&se=2023-05-27T07%3A03%3A30Z&sr=c&sp=c&sig=xxxxxxxxxxxxx
https://venkat123.blob.core.windows.net/test?sv=2022-11-02&spr=https%2Chttp&se=2023-05-27T07%3A03%3A30Z&sr=c&sp=c&sig=xxxxxxxx

enter image description here

引用:

Create a service SAS for a container or blob with JavaScript - Azure Storage | Microsoft Learn

关于azure - 如何在 NodeJS 中为 Azure 存储容器生成共享访问签名 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76343825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com