更新:我已经更新了函数以提供广泛的时间范围以避免过期。我还包括我更新以启用公共访问的帐户设置的屏幕截图。还包括当前的控制台结果。图像无法打开的页面上的相同结果。
async function sasKey(mediaLoc) {
try {
console.log('starting sasKey in bloboperations mediaLoc:', mediaLoc)
console.log('process.env.BLOB_CONTAINER is: ', process.env.BLOB_CONTAINER)
var storage = require("@azure/storage-blob")
const accountname = process.env.BLOB_ACCT_NAME;
console.log('accountname is: ', accountname)
const key = process.env.BLOB_KEY;
const cerds = new storage.StorageSharedKeyCredential(accountname,key);
const blobServiceClient = new storage.BlobServiceClient(`https://${accountname}.blob.core.windows.net`,cerds);
const containerName=process.env.BLOB_CONTAINER;
const client =blobServiceClient.getContainerClient(containerName)
const blobName=mediaLoc;
const blobClient = client.getBlobClient(blobName);
const checkDate = new Date();
const startDate = new Date(checkDate.valueOf() - 1200000);
const endDate = new Date(checkDate.valueOf() + 3600000);
console.log('checkDate, startDate, endDate: ', checkDate, startDate, endDate)
const blobSAS = storage.generateBlobSASQueryParameters({
containerName,
blobName,
permissions: storage.BlobSASPermissions.parse("racwd"),
startsOn: startDate,
expiresOn: endDate
},
cerds
).toString();
console.log( 'blobSAS is: ', blobSAS)
// const sasUrl= blobClient.url+"?"+encodeURIComponent(blobSAS);
const sasUrl = 'https://' + accountname + '/' + containerName + '/' + blobName + '?' + blobSAS
console.log('sasURL is: ', sasUrl);
return sasUrl
}
catch (error) {
console.log(error);
}
}
我正在尝试通过 node.js 函数从我的 Azure 存储 blob 容器中获取有效的 SAS URI。我正在使用@azure/storage-blob 库。我收到了 Azure 的回复,但浏览器说它未经授权。我已经四次检查我的帐户和密钥是否正确。这些设置正在将媒体上传到容器。
我不确定如何进行故障排除,因为没有任何错误消息返回到节点 api。相同的代码返回一个在另一个(开发)容器中工作的 URI。但是,该容器现在允许公共访问。因此,无论如何,您都可以从该 blob 访问 blob 是有道理的。请问有什么关于如何解决这个问题的建议吗?
访问控制台中生成的 blob 的错误:
5x6gyfbc5eo31fdf38f7fdc51ea1632857020560.png:1 获取 https://**********.blob.core.windows.net/prod/5x6gyfbc5eo31fdf38f7fdc51ea1632857020560.png?sv%3D2020-06-12%26st%3D2021-98T91 %253A23%253A41Z%26se%3D2021-09-28T19%253A25%253A07Z%26sr%3Db%26sp%3Dracwd%26sig%3Du6Naiikn%252B825koPikqRGmiOoKMJZ5L3mfcR%252FTCT3Uyk%253D 上不允许访问此帐户。
生成 URI 的代码:
async function sasKey(mediaLoc) {
try {
var storage = require("@azure/storage-blob")
const accountname = process.env.BLOB_ACCT_NAME;
const key = process.env.BLOB_KEY;
const cerds = new storage.StorageSharedKeyCredential(accountname,key);
const blobServiceClient = new storage.BlobServiceClient(`https://${accountname}.blob.core.windows.net`,cerds);
const containerName=process.env.BLOB_CONTAINER;
const client =blobServiceClient.getContainerClient(containerName)
const blobName=mediaLoc;
const blobClient = client.getBlobClient(blobName);
const blobSAS = storage.generateBlobSASQueryParameters({
containerName,
blobName,
permissions: storage.BlobSASPermissions.parse("racwd"),
startsOn: new Date(),
expiresOn: new Date(new Date().valueOf() + 86400)
},
cerds).toString();
const sasUrl= blobClient.url+"?"+encodeURIComponent(blobSAS);
console.log('blobOperations.js returns blobSAS URL as: ', sasUrl);
console.log( 'blobSAS is: ', blobSAS)
return sasUrl
}
catch (error) {
console.log(error);
}
}