【发布时间】:2017-12-15 22:19:09
【问题描述】:
我有一个 Azure blob 存储设置,其中包含几个文件。当文件较小(KB 大小)时,我可以将文件下载到 Stream 中,但是当文件稍大(MB 大小)时,我会收到 404 错误。我已经从门户网站手动下载了一个返回 404 的图像,并调整了该图像的大小,然后将较小的图像上传回容器,然后我可以按语法将其下载到流中。
这是我用来下载 blob 的代码
private static byte[] PerformDownload(string fileName, CloudBlobContainer container)
{
var blockBlob = container.GetBlockBlobReference(fileName);
using (var memoryStream = new MemoryStream())
{
blockBlob.DownloadToStream(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
var binaryReader = new BinaryReader(memoryStream);
var bytes = binaryReader.ReadBytes((int)memoryStream.Length);
return bytes;
}
}
容器被传递到这个方法中,正如我提到的,我可以毫无问题地从容器中下载一些文件,但是如果你需要该代码,我也可以添加它
使用您找到的标准示例检索容器,但这里是代码
private static CloudBlobContainer GetContainer(string containerName)
{
var storageAccount = CloudStorageAccount.Parse(ConnectionString);
var container = CreateContainerIfNeeded(storageAccount, containerName);
return container;
}
private static CloudBlobContainer CreateContainerIfNeeded(CloudStorageAccount storageAccount, string containerName)
{
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
return container;
}
另外,区分大小写也不是问题,因为容器的名称是 2017-106,文件是 4448.jpg。
【问题讨论】:
-
请添加代码。另请注意,blob 名称区分大小写,因此如果您提供不同大小写的 blob 名称(当 blob 实际保存为
MyImage.png时请求myimage.png),您将收到 404 错误。 -
我用附加代码更新了问题
标签: c# .net azure azure-blob-storage