2014-05-06 4 views
15

Ich muss Namen von Azure Blob-Dateinamen auflisten. Momentan kann ich alle Dateien mit URL auflisten, aber ich brauche nur eine Liste von Namen. Ich möchte vermeiden, Namen zu analysieren.Liste der Namen von Azure Blob-Dateien in einem Container abrufen?

CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(blobConectionString); 

var backupBlobClient = backupStorageAccount.CreateCloudBlobClient(); 
var backupContainer = backupBlobClient.GetContainerReference(container); 

var list = backupContainer.ListBlobs(); 
+3

Enthält Ihr Blob-Container nur Blobs? Wenn das der Fall ist, dann könntest du einfach so etwas wie folgendes tun: 'List blobNames = list.Select (b => (b als CloudBlockBlob) .Name);'. –

Antwort

2

Sie die BlobProperties zugreifen können, um den Namen zu erhalten:

foreach (object o in list) 
{ 
    BlobProperties bp = o as BlobProperties; 
    if (bp != null) 
    { 
     BlobProperties p = _Container.GetBlobProperties(bp.Name); 
     var name = p.Name; // get the name 
    } 
} 
+0

danke. Kann es in Lambda/Linq gemacht werden? Wenn Sie nur so denken, wird die Funktion GetBlobProperties für jede Blobdatei aufgerufen, richtig? Dein Rat bitte. – Toubi

24

Wenn Sie mit Windows-Azure Storage 4.3.0, versuchen Sie dies Können Sie bitte meinen Code unten und finden Sie im Handbuch Code.

List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList(); 
+0

Wir können einige zusätzliche Informationen wie Größe, Änderungsdatum und mehr bekommen. Verweise meine Antwort unten. –

11

Hier ist eine weitere Möglichkeit, dies zu tun zu bekommen:

CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(blobConectionString); 

var backupBlobClient = backupStorageAccount.CreateCloudBlobClient(); 
var backupContainer = backupBlobClient.GetContainerReference(container); 

// useFlatBlobListing is true to ensure loading all files in 
// virtual blob sub-folders as a plain list 
var list = backupContainer.ListBlobs(useFlatBlobListing: true); 
var listOfFileNames = new List<string>(); 

foreach (var blob in blobs) { 
    var blobFileName = blob.Uri.Segments.Last(); 
    listOfFileNames.Add(blobFileName); 
} 

return listOfFileNames; 

Quelle: How to load list of Azure blob files recursively?

0

Vollständige Antwort mit Details.

 // Parse the connection string and return a reference to the storage account. 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureBlobConnectionString")); 

     CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

     // Retrieve reference to a previously created container. 
     CloudBlobContainer container = blobClient.GetContainerReference("container_name"); 

     // Retrieve reference to a blob named "test.csv" 
     CloudBlockBlob blockBlob = container.GetBlockBlobReference("BlobName.tex"); 

     //Gets List of Blobs 
     var list = container.ListBlobs(); 
     List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList(); 
0

Wir können einige zusätzliche Informationen wie Größe bekommen, Änderungsdatum und Namen.

CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(YOUR_CON_STRING); 

var backupBlobClient = backupStorageAccount.CreateCloudBlobClient(); 
var backupContainer = backupBlobClient.GetContainerReference("CONTAINER"); 


var blobs = backupContainer.ListBlobs().OfType<CloudBlockBlob>().ToList(); 

foreach (var blob in blobs) 
{ 
    string bName = blob.Name; 
    long bSize = blob.Properties.Length; 
    string bModifiedOn = blob.Properties.LastModified.ToString();   
} 

Sie können auch herunterladen eine bestimmte Datei nach Namen.

// Download file by Name 
string fileName = "Your_file_name"; 
CloudBlockBlob blobFile = backupContainer.GetBlockBlobReference(fileName); 
blobFile.DownloadToFile(@"d:\"+ fileName, System.IO.FileMode.Create); 
Verwandte Themen