如何阅读 Azure Blob Url?
How to read Azure Blob Url?
我正在 React 和 Express/Azure SQL 数据库中创建博客列表 post。我能够使用 Azure blob 存储来存储与 post 关联的图像。我还能够获取 blob url 并将其存储在我的 SQL 数据库中。但是,当我想直接读取 url 时,它抛出了找不到资源的错误。在搜索文档和其他 Whosebug 答案后,我可以推断它与 SAS 令牌有关。任何人都可以解释什么是解决这个问题的更好方法?
https://你的域名.blob.core.windows.net/imagecontainer/你的图片.png
下面是nodejs代码。
router.post('/image', async function (req, res) {
try {
console.log(req.files.files.data);
const blobName = 'test' + uuidv1() + '.png';
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(req.files.files.data, req.files.files.data.length)
res.send({tempUrl:blockBlobClient.url});
} catch (e) {
console.log(e);
}
})
However when I want to read the url directly it threw an error
resource not found.
您收到此错误的原因很可能是包含 blob 的 blob 容器具有 Private
ACL,并且由于该匿名访问被禁用。要启用匿名访问,请将 blob 容器的 ACL 更改为 Blob
或 Public
即可解决此问题。
如果您不能(或不想)更改 blob 容器的 ACL,其他选择是在 blob 上创建 Shared Access Signature (SAS)
。 SAS 本质上提供了对 blob 的时间和权限绑定访问。根据您的需要,您需要创建一个仅具有 Read
权限的短期 SAS 令牌。
要生成 SAS 令牌,您需要使用 generateBlobSASQueryParameters
方法。创建 SAS 令牌后,您需要将其附加到 blob 的 URL 以获得 SAS URL.
这是执行此操作的示例代码。它使用 @azure/storage-blob
节点包。
const permissions = new BlobSASPermissions();
permissions.read = true;//Set read permission only.
const currentDateTime = new Date();
const expiryDateTime = new Date(currentDateTime.setMinutes(currentDateTime.getMinutes()+5));//Expire the SAS token in 5 minutes.
var blobSasModel = {
containerName: 'your-blob-container-name',
blobName: 'your-blob-name',
permissions: permissions,
expiresOn: expiryDateTime
};
const sharedKeyCredential = new StorageSharedKeyCredential('your-storage-account-name', 'your-storage-account-key');
const sasToken = generateBlobSASQueryParameters(blobSasModel, sharedKeyCredential);
const sasUrl = blockBlobClient + "?" + sasToken;//return this SAS URL to the client.
我正在 React 和 Express/Azure SQL 数据库中创建博客列表 post。我能够使用 Azure blob 存储来存储与 post 关联的图像。我还能够获取 blob url 并将其存储在我的 SQL 数据库中。但是,当我想直接读取 url 时,它抛出了找不到资源的错误。在搜索文档和其他 Whosebug 答案后,我可以推断它与 SAS 令牌有关。任何人都可以解释什么是解决这个问题的更好方法?
https://你的域名.blob.core.windows.net/imagecontainer/你的图片.png
下面是nodejs代码。
router.post('/image', async function (req, res) {
try {
console.log(req.files.files.data);
const blobName = 'test' + uuidv1() + '.png';
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(req.files.files.data, req.files.files.data.length)
res.send({tempUrl:blockBlobClient.url});
} catch (e) {
console.log(e);
}
})
However when I want to read the url directly it threw an error resource not found.
您收到此错误的原因很可能是包含 blob 的 blob 容器具有 Private
ACL,并且由于该匿名访问被禁用。要启用匿名访问,请将 blob 容器的 ACL 更改为 Blob
或 Public
即可解决此问题。
如果您不能(或不想)更改 blob 容器的 ACL,其他选择是在 blob 上创建 Shared Access Signature (SAS)
。 SAS 本质上提供了对 blob 的时间和权限绑定访问。根据您的需要,您需要创建一个仅具有 Read
权限的短期 SAS 令牌。
要生成 SAS 令牌,您需要使用 generateBlobSASQueryParameters
方法。创建 SAS 令牌后,您需要将其附加到 blob 的 URL 以获得 SAS URL.
这是执行此操作的示例代码。它使用 @azure/storage-blob
节点包。
const permissions = new BlobSASPermissions();
permissions.read = true;//Set read permission only.
const currentDateTime = new Date();
const expiryDateTime = new Date(currentDateTime.setMinutes(currentDateTime.getMinutes()+5));//Expire the SAS token in 5 minutes.
var blobSasModel = {
containerName: 'your-blob-container-name',
blobName: 'your-blob-name',
permissions: permissions,
expiresOn: expiryDateTime
};
const sharedKeyCredential = new StorageSharedKeyCredential('your-storage-account-name', 'your-storage-account-key');
const sasToken = generateBlobSASQueryParameters(blobSasModel, sharedKeyCredential);
const sasUrl = blockBlobClient + "?" + sasToken;//return this SAS URL to the client.