【发布时间】:2023-03-03 02:38:02
【问题描述】:
我正在尝试将文件上传到 Azure Blob 存储。到目前为止我做了什么:
npm i @azure/identity @azure/storage-blob
使用用户委托密钥生成 SAS 查询参数:
async function generateSas() {
const startDate = new Date();
const expiryDate = new Date();
startDate.setTime(startDate.getTime() - 100 * 60 * 1000);
expiryDate.setTime(expiryDate.getTime() + 100 * 60 * 60 * 1000);
const credential = new DefaultAzureCredential();
const blobServiceClient = new BlobServiceClient(STORAGE, credential);
const key = await blobServiceClient.getUserDelegationKey(startDate, expiryDate);
return generateBlobSASQueryParameters({
containerName: CONTAINER,
startsOn: startDate,
expiresOn : expiryDate,
permissions: ContainerSASPermissions.parse('rwl'),
}, key, ACCOUNT).toString();
}
使用生成的 SAS 上传
async function upload(sasToken: string) {
const blobClient = new BlockBlobClient(
`https://${ACCOUNT}.blob.core.windows.net/${CONTAINER}/test.json?${sasToken}`);
const content = 'some content';
const response = await blobClient.upload(content, content.length);
}
在我运行这个之前,我用我的帐户az login。
错误:
(node:19584) UnhandledPromiseRejectionWarning: RestError: This request is not authorized to perform
this operation using this permission.
如果我使用相同的登录名从 Azure 存储资源管理器 复制 SAS,则代码有效!所以我假设有某种方法可以为我的帐户检索有效的 SAS。
【问题讨论】:
标签: javascript node.js azure azure-storage azure-sdk