问题描述
在 official doc 之后,我编写此脚本尝试连接自定义域 azure 存储空间:
const { BlobServiceClient } = require("@azure/storage-blob");
const account = "validaccount";
const sas = "sv=xxxx&.......";
const blobServiceClient = new BlobServiceClient(`https://${account}.blob.core.customdomain.name${sas}`);
//===============
async function main() {
let i = 1;
let containers = blobServiceClient.listContainers();
for await (const container of containers) {
console.log(`Container ${i++}: ${container.name}`);
}
}
main();
出现错误:Server Failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
我确定 SASURI 是有效的,我可以在 azure blob storage explorer 中使用它,但在我的代码中它不起作用。
我尝试了一些组合,例如:
-
https://${account}.blob.core.customdomain.name?${sas}
//添加一个'?' -
https://${account}.blob.core.customdomain.name/abc?${sas}
//abc 是一个有效的容器名称 -
https://${account}.blob.core.customdomain.name/abc${sas}
//删除'?'但保留容器名称 -
https://${account}.blob.core.customdomain.name',sas
//尝试作为两个参数传递。
但都失败了。
我不确定是否还有其他方法。
我猜可能是因为 SAS 令牌仅授权给 abc
容器,它无法读取域根。
但是如果是这样,为什么第二个组合也失败了。
我使用@azure/storage-blob v12.3.0
解决方法
我找到了正确答案。
问题的核心是他们给我的SAS Token,已经绑定在名为“abc”的容器上。
SAS 令牌不是对域下所有内容的授权,令牌仅授权我访问容器“abc”。
因此,当我创建 BlobServiceClient 对象时,“位置”(如果可以的话)已经在容器“abc”下。由于我已经在一个容器中,我不能再list container
。
当我更改完整路径并尝试连接根时,实际上不允许使用令牌。当然授权失败了。
结论:
SAS 令牌已经绑定到特定容器“abc”,所以我既不能列出容器,也不能访问域的根路径。
我只能列出特定容器中的 blob 数据。
blob-storage 包的错误信息不是很清楚。
这里不是代码
const { BlobServiceClient } = require("@azure/storage-blob");
const account = "validaccount";
const sas = "sv=xxxx&.......";
const blobServiceClient = new BlobServiceClient(`https://${account}.blob.core.customdomain.name/abc?${SAS}`);
////====== just change 'list container' code to 'list blobs' code
const containerName = ""; //empty string,since you already in the container.
async function main() {
const containerClient = blobServiceClient.getContainerClient(containerName);
let i = 1;
let blobs = containerClient.listBlobsFlat();
for await (const blob of blobs) {
console.log(`Blob ${i++}: ${blob.name}`);
}
}
main();
,
也许你在生成 SAS 时需要检查所有 Allowed resource types
:
我认为正确的格式应该是 https://myaccount.blob.core.windows.net?sasString
。