C# 从 Azure Blob 存储下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13052673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:28:55  来源:igfitidea点击:

Download File from Azure Blob Storage

c#azuredownloadazure-storage-blobs

提问by c.dunlap

I have an application that allows users to upload photos, which are stored in Azure Blob Storage. The user also has the ability to view these photos. To view them, we want the application to download the image to the default download location. At the moment, the upload works perfect. But the download function that I found for Azure API doesn't seem to be doing anything. Also, I can't really specify a download location because this functionality needs to work on desktop/laptops as well as on mobile devices which have different default directories.

我有一个允许用户上传照片的应用程序,这些照片存储在 Azure Blob 存储中。用户还可以查看这些照片。要查看它们,我们希望应用程序将图像下载到默认下载位置。目前,上传工作完美。但是我为 Azure API 找到的下载功能似乎没有做任何事情。此外,我无法真正指定下载位置,因为此功能需要在台式机/笔记本电脑以及具有不同默认目录的移动设备上运行。

This seems like it should be simple but I cannot find anything to help me.

这似乎应该很简单,但我找不到任何可以帮助我的东西。

Here is a sample of my code:

这是我的代码示例:

CloudBlobContainer container = blobClient.GetContainerReference("photos");
CloudBlob blob = container.GetBlobReference(photo.BlobUrl);
//copy blob from cloud to local gallery
blob.DownloadToFile(photo.ImageName);

The blob.DownloadToFile(photo.ImageName);causes a server request but nothing happens, aka no file is downloaded.

blob.DownloadToFile(photo.ImageName);导致服务器请求,但没有任何反应,也称为无文件下载。

采纳答案by dunnry

Any reason you cannot have the client download the file directly from blob storage and use the built-in browser capability to save it? If the files are private, it is pretty easy to generate a SAS signature. This will offload the download from coming through your server and remove that overhead.

有什么原因不能让客户端直接从 blob 存储下载文件并使用内置浏览器功能来保存它?如果文件是私有的,则很容易生成 SAS 签名。这将从通过您的服务器卸载下载并消除该开销。

回答by astaykov

I wonder what do you expect from this code, but especially in the case of different platform this would not work. I suggest that you use the DownloadToStreammethod instead. Then you can decide what to do with the stream. For example ask the user where does he want to save the file.

我想知道您对这段代码有什么期望,但尤其是在不同平台的情况下,这将不起作用。我建议您改用DownloadToStream方法。然后您可以决定如何处理流。例如,询问用户要将文件保存在哪里。

回答by ElvisLives

Here:

这里:

blob.DownloadToFile(photo.ImageName);

You need to tell it where to save the file and the name you want it to be:

您需要告诉它保存文件的位置以及您希望它的名称:

blob.DownloadToFile("C:\mycutepuppy.jpg");

You could use System.Environment.SpecialFolder enum to figure out the folder based on the current system then append what you want the image name to be;

您可以使用 System.Environment.SpecialFolder 枚举根据当前系统找出文件夹,然后附加您想要的图像名称;

Here is a more complete example:

这是一个更完整的例子:

var imgPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), + photo.ImageName);

blob.DownloadToFile(imgPath);

This would save the image to the correct path where the user's desktop directory is. Here is the documentation on that enum for special folders.

这会将图像保存到用户桌面目录所在的正确路径。这是有关特殊文件夹的枚举的文档。

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx