gpt4 book ai didi

node.js - azuregenerateAccountSAS 与generateBlobSAS

转载 作者:行者123 更新时间:2023-12-03 04:46:03 27 4
gpt4 key购买 nike

我有一个包含多个容器和 blob 的 azure 存储帐户:

container1 -> blob1.txt, blob2.jpg
container2 -> blob1.png
container3 ->

我想生成一个只能从container1读取和写入的SAS。

第一个问题:我应该使用generateAccountSASQueryParameters还是generateBlobSASQueryParameters来执行此操作?

第二个问题:我一直在尝试使用generateBlobSASQueryParameters 来实现这一点。但是,我不断收到此错误:

<Error>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of Authorization header is
formed correctly including the signature. RequestId:1db4c573-201e-0083-2396-ab3df5000000
Time:2019-12-05T18:05:52.8934222Z
</Message>
<AuthenticationErrorDetail>
The specified signed resource is not allowed for the this resource level
</AuthenticationErrorDetail>
</Error>

我用来执行此操作的代码在这里:

const account = "storageaccountname";
const accountKey = "<myKey>";
const sharedKeyCredential = new AzureStorageBlob.StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new AzureStorageBlob.BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);

const containerClient = blobServiceClient.getContainerClient('container1')

const sas = AzureStorageBlob.generateBlobSASQueryParameters({
containerName: 'container1',
protocol: AzureStorageBlob.SASProtocol.HTTPS,
permissions: 'rw',
resourceTypes: 'sco',
startsOn: new Date(),
expiresOn: new Date(new Date().valueOf() + 86400),
}, sharedKeyCredential)
res.status(200).json(`https://${account}.blob.core.windows.net/?${sas.toString()}`)

感谢任何帮助。谢谢

最佳答案

根据我的测试,如果你想创建一个SAS token来在一个容器中创建和读取blob,请引用以下代码

  1. 安装包
npm install @azure/storage-blob
  • 代码
  • var storage = require("@azure/storage-blob")

    async function generateSasToken1() {

    const accountname ="blobstorage0516";
    const key = "";
    const cerds = new storage.StorageSharedKeyCredential(accountname,key);
    var startDate = new Date();
    var expiryDate = new Date();
    startDate.setTime(startDate.getTime() - 5*60*1000);
    expiryDate.setTime(expiryDate.getTime() + 24*60*60*1000);
    const containerName="test";
    // if the container does not exit, please run the following code to create
    // const blobServiceClient = new storage.BlobServiceClient(
    // `https://${accountname}.blob.core.windows.net`,
    // sharedKeyCredential
    // );
    // const containerClient = blobServiceClient.getContainerClient(containerName)
    // await containerClient.create()
    const containerSAS = storage.generateBlobSASQueryParameters({
    expiresOn : expiryDate,
    permissions: storage.ContainerSASPermissions.parse("rwl"),
    protocol: storage.SASProtocol.Https,
    containerName: containerName,
    startsOn: startDate,
    version:"2018-03-28"

    },cerds).toString();

    console.log(containerSAS);
    const containerUrl="https://"+accountname+".blob.core.windows.net/"+containerName+"?"+containerSAS
    const client =new storage.ContainerClient(containerUrl);

    for (let index = 0; index < 7; index++) {
    // Create a blob(write)
    const content = "hello";
    const blobName = "newblob" + new Date().getTime();
    const blobClient = client.getBlobClient(blobName);
    const blockBlobClient = blobClient.getBlockBlobClient();
    const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
    console.log(`Uploaded block blob ${blobName} successfully`, uploadBlobResponse.requestId);
    }

    // List blobs(list)
    let i = 1;
    let iter = await client.listBlobsFlat();
    for await (const blob of iter) {
    console.log(`Blob ${i++}: ${blob.name}`);
    }

    // download blob(read)
    const blobClient = client.getBlobClient("test.json");
    const downloadBlockBlobResponse = await blobClient.download(0);
    console.log(
    "Downloaded blob content",
    await streamToString(downloadBlockBlobResponse.readableStreamBody)
    );
    }
    async function streamToString(readableStream) {
    return new Promise((resolve, reject) => {
    const chunks = [];
    readableStream.on("data", (data) => {
    chunks.push(data.toString());
    });
    readableStream.on("end", () => {
    resolve(chunks.join(""));
    });
    readableStream.on("error", reject);
    });
    }

    generateSasToken1()
    .then(() => {
    console.log("Successfully executed sample.");
    })
    .catch((err) => {
    console.log(err.message);
    });

    enter image description here

    更多详情请引用document

    关于node.js - azuregenerateAccountSAS 与generateBlobSAS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59201013/

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