我是 Azure 存储的新手。因此,请考虑帖子中的文本/代码。
我在 Azure 存储
上存储了一些文件。
示例:
http://my.blob.core.windows.net/2015/4/10/fea1fc9d-d04102015115229.jpg
http://my.blob.core.windows.net/2015/4/10/asdfc9d-d04102015115229.jpg
现在我想使用 Container/Blob
名称将这些文件下载到特定文件夹。
DownloadFiles.aspx:
protected void Callme(string sourceURLPath)
{
string thumbDirectoryName = string.Empty;
string sourcePath = sourceURLPath;
string targetUrl = string.Empty;
CloudStorageAccount cloudStorageAccount;
CloudBlobClient blobClient;
CloudBlobContainer blobContainer;
BlobContainerPermissions containerPermissions;
CloudBlob blob;
cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=" + ConfigurationManager.AppSettings["AzureStorageAccountName"] + ";AccountKey=" + ConfigurationManager.AppSettings["AzureStorageAccountKey"] + "");
blobClient = cloudStorageAccount.CreateCloudBlobClient();
blobContainer = blobClient.GetContainerReference(DateTime.Now.Year.ToString());
blobContainer.CreateIfNotExist();
containerPermissions = new BlobContainerPermissions();
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
blobContainer.SetPermissions(containerPermissions);
//need to get files here and download in to specific folder
}
有什么建议吗?
最佳答案
你就快到了:)。以下是您需要执行的操作:
- 首先,您必须列出容器中的 blob。为此,您可以使用
ListBlobs
容器上的方法。列出 Blob 时,请确保将prefix
作为空字符串 (string.Empty)
传递,将useFlatBlobListing
作为true
传递>。这将给出容器中的 blob 列表。 - 接下来,您将迭代 Blob 列表,将每个 Blob 转换为
CloudBlockBlob
,然后调用DownloadToFile
每个 blob 上的方法。
关于您的代码的一些建议:
由于您正在从 Blob 容器读取文件,因此该容器已经存在。因此无需尝试在每个请求上创建该容器。或者换句话说,从代码中删除这一行:
blobContainer.CreateIfNotExist();
同样,您也不需要以下代码行,因为您所做的只是读取 blob。
containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; blobContainer.SetPermissions(containerPermissions);
关于c# - 如何使用 C# 从 Azure 容器/Blob 读取/下载所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076558/