gpt4 book ai didi

c# - 访问 blob uri 时出现 ResourceNotFound

转载 作者:行者123 更新时间:2023-11-30 14:23:35 24 4
gpt4 key购买 nike

我尝试通过 Azure Blob 存储中的 URL 检索图像,但找不到它,这就是我得到的结果:

enter image description here

我的代码如下:

public async Task<bool> UploadFileAsync(string containerReference, string blobReference, string route)
{
CloudBlobContainer container = blobClient.GetContainerReference(containerReference);
container.CreateIfNotExists();
CloudBlockBlob blob = container.GetBlockBlobReference(blobReference);

try
{
using (var fileStream = System.IO.File.OpenRead(route))
{
await blob.UploadFromStreamAsync(fileStream);
}
}
catch (System.Exception)
{
return false;
}

return true;
}

成功将文件上传到 blob:

enter image description here

然后我尝试检索其 URL 以直接访问它:

public string GetBlobUrl(string containerReference, string blobReference)
{
CloudBlobContainer container = blobClient.GetContainerReference(containerReference);
CloudBlockBlob blob = container.GetBlockBlobReference(blobReference);

return blob.Uri.ToString();
}

我做错了什么?

最佳答案

默认情况下,容器及其 ​​Blob 只能由存储帐户所有者访问。如果您希望私有(private)容器中的 blob 可以通过匿名请求读取,您可以 grant public read access for blobs 。此外,正如 David Makogon 在评论中提到的,您还可以 grant temporary public access to a private blob via shared access signature .

CloudBlobContainer container = cloudBlobClient.GetContainerReference("mycontainer");   
CloudBlockBlob blob = container.GetBlockBlobReference("testimg.PNG");

SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddDays(7);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read;


string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);
string URL = blob.Uri + sasBlobToken;

关于c# - 访问 blob uri 时出现 ResourceNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44780625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com