C# 从 Azure blob 存储读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11013953/
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
Read file from Azure blob storage
提问by Hope
I want to read a PDF file bytes from azure storage, for that I have a file path.
我想从 azure 存储中读取 PDF 文件字节,为此我有一个文件路径。
https://hostedPath/pdf/1001_12_Jun_2012_18_39_05_594.pdf
So it possible to read content from blob storage by directly passing its Path name? Also I am using c#.
那么可以通过直接传递其路径名来从 blob 存储中读取内容吗?我也在使用 c#。
回答by David Makogon
As long as the blob is public, you can absolutely pass the blob url. For instance, you can embed it in an html image or link:
只要 blob 是public,您绝对可以传递 blob url。例如,您可以将其嵌入到 html 图像或链接中:
<a href="https://myaccount.blob.core.windows.net/pdf/1001_12_Jun_2012_18_39_05_594.pdf">click here</a>
By default, blob containers are private. To enable public read access, you just have to change the container permissions when creating the container. For instance:
默认情况下,blob 容器是私有的。要启用公共读取访问,您只需在创建容器时更改容器权限。例如:
var blobStorageClient = storageAccount.CreateCloudBlobClient();
var container = blobStorageClient.GetContainerReference("pdf");
container.CreateIfNotExist();
var permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
回答by Sandrino Di Mattia
Just like David explained you can access any blob through its url as long as the container is not private.
就像 David 解释的那样,只要容器不是私有的,您就可以通过其 url 访问任何 blob。
If the container is private you can still make your files accessible through the url by using Shared Access Signatures(SAS). This will allow you grant users the right do download the file (by providing them with the SAS, usually appended to the URL) but limiting them in time.
如果容器是私有的,您仍然可以使用共享访问签名(SAS)通过 url 访问您的文件 。这将允许您授予用户下载文件的权利(通过向他们提供 SAS,通常附加到 URL)但限制他们的时间。
This is perfect when you have paying downloads for example, to protect your files but allowing them to be downloaded for a limited time if someone payed for it.
例如,当您进行付费下载时,这是完美的,以保护您的文件,但如果有人付费,则允许在有限的时间内下载它们。
Now, in your question you state that you're using C#. If you want to download the file in a WPF/Windows Forms/Console application, you can simply use the WebClient to download the file (if the container is public or you append the URL with the SAS):
现在,在您的问题中,您声明您正在使用 C#。如果您想在 WPF/Windows 窗体/控制台应用程序中下载文件,您可以简单地使用 WebClient 下载文件(如果容器是公共的或者您附加了带有 SAS 的 URL):
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile("https://myaccount.blob.core.windows.net/pdf/1001_12_Jun_2012_18_39_05_594.pdf", @"D:\Data\myPdfFile.pdf");

