2014-09-18 7 views
6

Sto usando container.ListBlobs, ma sembra che stia restituendo un elenco {Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.LazyEnumerable} tuttavia quando eseguo un foreach l'oggetto sembra essere CloudBlobDirectory invece di un elenco di CloudBlockBlobs . Sto facendo qualcosa di sbagliato, o è questo ciò che dovrebbe tornare? C'è un modo in cui posso solo ottenere un elenco dei BLOB, piuttosto che blobdirectories?container.ListBlobs sta dando un elenco di CloudBlobDirectory Mi aspettavo un elenco di CloudBlockBlobs?

var storageAccount = CloudStorageAccount.Parse(conn); 
var blobClient = storageAccount.CreateCloudBlobClient(); 
var container = blobClient.GetContainerReference(containerName); 
var blobs = container.ListBlobs(); 
foreach (var blob in blobs) 
{ 
    Console.WriteLine(blob.GetType().ToString()); 
} 
+0

Hai controllato per vedere come si presenta la struttura del tuo file? Se hai delle directory con Blob nel tuo contenitore, mostrerebbero le directory. –

risposta

9

Secondo il MSDN per CloudBloblContainer.ListBlobs():

The types of objects returned by the ListBlobs method depend on the type of listing that is being performed. If the UseFlatBlobListing property is set to true, the listing will return an enumerable collection of CloudBlob objects. If UseFlatBlobListing is set to false (the default value), the listing may return a collection containing CloudBlob objects and CloudBlobDirectory objects. The latter case provides a convenience for subsequent enumerations over a virtual blob hierarchy.

Quindi, se desideri solo blob, è necessario impostare l'opzione UseFlatBlobListing proprietà su true.

var storageAccount = CloudStorageAccount.Parse(conn); 
var blobClient = storageAccount.CreateCloudBlobClient(); 
var container = blobClient.GetContainerReference(containerName); 
// ** new code below ** // 
BlobRequestOptions options = new BlobRequestOptions(); 
options.UseFlatBlobListing = true; 
// ** new code above ** // 
var blobs = container.ListBlobs(options); // <-- add the parameter to overload 
foreach (var blob in blobs) 
{ 
    Console.WriteLine(blob.GetType().ToString()); 
} 
+4

Bob I non è stato in grado di fare esattamente come hai detto (options.UseFlatBlobListing = true;) non era in arrivo ma l'ho fatto: var blobs = container.ListBlobs (useFlatBlobListing: true); – DermFrench

+0

Con l'ultimo SDK di Azure, container.ListBlobs (prefisso: "come necessario", useFlatBlobListing: true) funziona. +1 @DermFrench –

Problemi correlati