如何将生成的SAS令牌从NodeJS Azure函数传递到HTML?

ozxc1zmp  于 2023-06-06  发布在  其他
关注(0)|答案(2)|浏览(151)

我使用Azure Function和NodeJS创建了一个API。它包含一个HTML前端,用户可以使用SAS令牌从Azure存储容器上传或下载文件。我的一个新要求是SAS令牌应该在Azure函数本身中生成,所以我现在有一个生成SAS令牌的函数。这样做的问题是,我不确定如何将生成的令牌从我的Azure函数传递到HTML前端。我该怎么做?
我尝试将SAS令牌函数插入到我的HTML脚本部分,但是它不起作用,因为我的代码在尝试将包导入到HTML文件时失败。我还尝试从Azure函数调用SAS令牌函数到我的HTML,但也不起作用。

编辑:添加代码,使代码更加清晰。
HTML(index.html):

<script type="text/javascript" src="./index.js"></script><input type="button" onclick="createContainerSas()" value="run external javascript">

Javascript(index.js):

function createContainerSas(){
const accountName = "myaccountname";
const containerName = "mycontainername";
const accountkey = "myaccountkey";

const credential = new StorageSharedKeyCredential(accountName, accountkey);
const blobServiceClient = new BlobServiceClient(`https://${accountName}.blob.core.windows.net`, credential);
const containerClient = blobServiceClient.getContainerClient(containerName);

const sasOptions = {
    containerName,
    protocol:  SASProtocol.HttpsAndHttp
};

sasOptions.permissions = ContainerSASPermissions.parse('cw'); // for creating and writing.
sasOptions.expiresOn = new Date(new Date().valueOf() + 3600*1000);
const sasToken = generateBlobSASQueryParameters(sasOptions, credential).toString();
return sasToken;
}

返回两个错误-“加载资源失败:服务器返回状态404(Not Found)"and“Uncaught ReferenceError:createContainerSas未定义”

lb3vh1jj

lb3vh1jj1#

在HTML文件中,可以使用fetch()函数向Azure Function发送HTTP请求并获取SAS令牌。

// send request to Azure Function
fetch('https://your-function-app.azurewebsites.net/api/your-function-name')
.then(response => response.json()) // parse response as JSON
.then(data => {
    // now `data.token` contains the SAS token
    const sasToken = data.token;

    // use sasToken to interact with Azure Storage
    // ...
});

请记住将“https://your-function-app.azurewebsites.net/api/your-function-name”替换为Azure函数的实际URL。

ezykj2lf

ezykj2lf2#

使用下面的Node js Azure函数代码,我可以生成存储帐户的SAS令牌

代码:

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

module.exports = async function (context, req) {
    const accountName = '<your_storage_account_name>';
    const accountKey = '<your_storage_account_key>';
    const containerName = "<your_container_name>";
    const blobName = "<your_blob_name>";

    const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
    const blobServiceClient = new BlobServiceClient(`https://<your_storage_account_name>.blob.core.windows.net`, sharedKeyCredential);

    const blobClient = blobServiceClient.getContainerClient(containerName).getBlobClient(blobName);

    const sasToken = generateBlobSASQueryParameters({
        containerName: containerName,
        blobName: blobName,
        permissions: "read",
        startsOn: new Date(),
        expiresOn: new Date(new Date().valueOf() + 86400)
    }, sharedKeyCredential).toString();

    context.res = {
        body: {
            sasToken: sasToken
        }
    };
};

输出:

通过上面的URL,我在browser得到了SAS token输出,如下所示:

相关问题