我尝试使用代码从单个Azure容器中下载所有Blob。当我下载每个Blob时,我希望新文件名是从2.jpg向上到3.jpg,4.jpg 5.jpg等的连续数字。这不是天蓝色容器中Blob的名称,我不知道代码运行时每个Blob的名称。
我似乎陷入了最后的障碍,我无法弄清楚要放入我的foreach
块以将文件放入我本地硬盘上的\home\pi\Pictures\
目录。
urls.DownloadToStream(FileStream);
正在抛出错误
List String不包含DownloadToStream的定义
和
FileStream是一种在给定上下文中无效的类型。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
namespace CPGetAdverts
{
class Program
{
static void Main(string[] args)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureImagesConnection"].ConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
var container = blobClient.GetContainerReference("xxxxxx/").ListBlobs();
// Retrieve filenames from container List
var urls = new List<string>();
int fileName = 2;
foreach (var blob in container)
{
using (var fileStream = System.IO.File.OpenWrite(@"\home\pi\Pictures\"+ fileName + ".jpg")) ;
urls.DownloadToStream(FileStream);
fileName++;
}
}
}
}
非常感谢任何帮助。
答案 0 :(得分:2)
这一行有几个问题:
urls.DownloadToStream(FileStream);
DownloadToStream
方法FileStream
应为fileStream
尝试将循环更改为:
foreach (var blob in container)
{
using (var fileStream = System.IO.File.OpenWrite(@"\home\pi\Pictures\"+ fileName + ".jpg"))
{
var blobReference = blobClient.GetBlobReferenceFromServer(blob.Uri);
blobReference.DownloadToStream(fileStream);
fileName++;
}
}