Skip to content

Instantly share code, notes, and snippets.

@ajpinedam
Created January 16, 2017 02:23
Show Gist options
  • Save ajpinedam/82e944475efcaed87aea561add678c0c to your computer and use it in GitHub Desktop.
Save ajpinedam/82e944475efcaed87aea561add678c0c to your computer and use it in GitHub Desktop.
/// <summary>
/// Gets files name in an Azure shared file storage
/// </summary>
/// <param name="fileStorageName">File storage name. Como fue llamado en Azure</param>
/// <returns>An array with the files name</returns>
async Task<string []> GetSharedFiles (string fileStorageName)
{
// Necesitas un objeto de Microsoft.WindowsAzure.Storage.File.FileContinuationToken en el nuevo metodo
// En la primera llamada lo pasas null y el te retornara uno nuevo en el resultado de la llamada.
// sigues haciendo llamadas al mismo metodo mientras este sea diferente de null (esto significa que ya no ya mas archivos).
FileContinuationToken continuationToken = null;
var fileName = new List<string> ();
var storageInstance = CloudStorageAccount.Parse ("YOUR_AZURE_SAS_KEY");
var fileClient = storageInstance.CreateCloudFileClient ();
var share = fileClient.GetShareReference (fileStorageName);
if (!await share.ExistsAsync ())
{
return fileName.ToArray();
}
var root = share.GetRootDirectoryReference ();
// Si los archivos estan dentro de un directorio haces esto de lo contrario utilizas el
// root en el paso mas abajo.
var myFiles = root.GetDirectoryReference ("MyFiles");
if (!await myFiles.ExistsAsync ())
{
return fileName.ToArray ();
}
do
{
var files = await myFiles.ListFilesAndDirectoriesSegmentedAsync (continuationToken);
var uris = files.Results.Select (a => a.Uri);
continuationToken = files.ContinuationToken;
foreach (var item in uris)
{
var name = Path.GetFileName (item.AbsolutePath);
fileName.Add (name);
}
} while (continuationToken != null);
return fileName.ToArray ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment