public async Task <IActionResult> GetFilesName(string clientNameSx1) { var connectionString = _configuration.GetSection("StorageAccount:ConnectionString").Value; var shareName = _configuration.GetSection("StorageAccount:ShareName").Value; ShareClient share = new ShareClient(connectionString, shareName); ShareDirectoryClient directory = share.GetDirectoryClient("logos"); var filesName = new List <string>(); if (string.IsNullOrEmpty(clientNameSx1)) { filesName = directory.GetFilesAndDirectories().Select(f => f.Name).ToList(); } else { filesName = directory.GetFilesAndDirectories().Where(f => f.Name.Contains(clientNameSx1)).Select(f => f.Name).ToList(); } if (filesName == null || filesName.Count < 1) { NotFound(); } return(Ok(filesName)); }
public static FoundFile ScanForANewFile() { ShareClient share = new ShareClient(CONNECTIONSTRING, SHARENAME); foreach (ShareFileItem item in share.GetRootDirectoryClient().GetFilesAndDirectories()) // loop through all plants { if (item.Name == "System") { continue; } if (item.IsDirectory) { var subDirectory = share.GetRootDirectoryClient().GetSubdirectoryClient(item.Name); ShareDirectoryClient dir = subDirectory.GetSubdirectoryClient("immediateScan"); if (!dir.Exists()) { continue; } foreach (var file in dir.GetFilesAndDirectories()) { if (!file.IsDirectory) { string tempFileName = Path.GetTempFileName(); DownloadFile(dir.GetFileClient(file.Name), tempFileName); return(new FoundFile(/*item.Name, */ file.Name, dir.Path + "/" + file.Name, tempFileName)); } } } } return(null); }
public List <Metadata> GetMetadatas() { List <Metadata> metadatas = new List <Metadata>(); MetadataParser parser = new MetadataParser( new MetadataConverter()); try { var remaining = new Queue <ShareDirectoryClient>(); remaining.Enqueue(shareClient.GetDirectoryClient(this.path)); while (remaining.Count > 0) { ShareDirectoryClient dir = remaining.Dequeue(); foreach (ShareFileItem item in dir.GetFilesAndDirectories()) { if (item.IsDirectory) { remaining.Enqueue(dir.GetSubdirectoryClient(item.Name)); } else if (item.Name.Contains(FILE_EXTENSION)) { Console.WriteLine($" In {dir.Uri.AbsolutePath} found {item.Name}"); ShareFileClient file = dir.GetFileClient(item.Name); ShareFileDownloadInfo fileContents = file.Download(); string json; using (MemoryStream ms = new MemoryStream()) { fileContents.Content.CopyTo(ms); json = Encoding.UTF8.GetString(ms.ToArray()); } // Parse json string Metadata metadata = parser.Parse(json); // Sets the dataset path relative to the Uri and Path specified in constructor var filePath = Path.GetRelativePath(this.path, file.Path) .Replace(FILE_EXTENSION, ""); metadata.Dataset.DatasetPath = filePath; metadatas.Add(metadata); } } } Console.WriteLine($"Found a total of {metadatas.Count} files"); } catch (Exception e) { Console.WriteLine($"An error ocurred: {e}"); } return(metadatas); }
/// <summary> /// Traverse the files and directories in a share. /// </summary> /// <param name="connectionString"> /// A connection string to your Azure Storage account. /// </param> /// <param name="shareName"> /// The name of the share to traverse. /// </param> public static void Traverse(string connectionString, string shareName) { #region Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Traverse // Connect to the share //@@ string connectionString = "<connection_string>"; //@@ string shareName = "sample-share"; ShareClient share = new ShareClient(connectionString, shareName); // Track the remaining directories to walk, starting from the root var remaining = new Queue <ShareDirectoryClient>(); remaining.Enqueue(share.GetRootDirectoryClient()); while (remaining.Count > 0) { // Get all of the next directory's files and subdirectories ShareDirectoryClient dir = remaining.Dequeue(); foreach (ShareFileItem item in dir.GetFilesAndDirectories()) { // Print the name of the item Console.WriteLine(item.Name); // Keep walking down directories if (item.IsDirectory) { remaining.Enqueue(dir.GetSubdirectoryClient(item.Name)); } } } #endregion Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Traverse }
static void Main(string[] args) { string downloadDirectory = args[0]; // Create a BlobServiceClient object which will be used to create a container client BlobServiceClient blobServiceClient = new BlobServiceClient(AZURE_STORAGE_CONNECTION_STRING); // Create a unique name for the container string containerName = "startupfiles"; // Create the container and return a container client object BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); List <BlobItem> blobs = containerClient.GetBlobs().ToList(); // Download the blob files in parallel - maximum of 10 at a time } Parallel.ForEach(blobs, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, blob => { string downloadFilePath = Path.Combine(downloadDirectory, blob.Name); BlobClient blobClient = containerClient.GetBlobClient(blob.Name); BlobDownloadInfo download = blobClient.Download(); using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath)) { download.Content.CopyTo(downloadFileStream); downloadFileStream.Close(); } }); string shareName = "startupfiles"; ShareClient shareClient = new ShareClient(AZURE_FILES_CONNECTION_STRING, shareName); if (shareClient.Exists()) { ShareDirectoryClient shareDirectoryClient = shareClient.GetRootDirectoryClient(); List <ShareFileItem> items = shareDirectoryClient.GetFilesAndDirectories().ToList(); foreach (ShareFileItem item in items) { if (!item.IsDirectory) { string downloadFilePath = Path.Combine(downloadDirectory, item.Name); ShareFileClient shareFileClient = shareDirectoryClient.GetFileClient(item.Name); ShareFileDownloadInfo download = shareFileClient.Download(); using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath)) { download.Content.CopyTo(downloadFileStream); downloadFileStream.Close(); } } } } }
//------------------------------------------------- // Recursively list a directory tree //------------------------------------------------- public void ListDirTree(ShareDirectoryClient dir) { // List the files and directories in the snapshot foreach (ShareFileItem item in dir.GetFilesAndDirectories()) { if (item.IsDirectory) { Console.WriteLine($"Directory: {item.Name}"); ShareDirectoryClient subDir = dir.GetSubdirectoryClient(item.Name); ListDirTree(subDir); } else { Console.WriteLine($"File: {dir.Name}\\{item.Name}"); } } }
public async Task <List <string> > ListFiles(string fileShare) { List <string> files = new List <string>(); ShareClient share = new ShareClient(connectionString, fileShare); if (!share.Exists()) { Exception error = new Exception($"Fileshare {fileShare} does not exist "); throw error; } ShareDirectoryClient directory = share.GetRootDirectoryClient(); foreach (ShareFileItem item in directory.GetFilesAndDirectories()) { files.Add(item.Name); } return(files); }
/// <summary> /// Returns a list of file names in the current directory /// </summary> /// <returns>The list of directory entries. Any exceptions are thrown.</returns> public List <DirectoryEntry> FileList() { if (FileShare == null || Directory == null) { throw new Exception("Cannot list files (null share or directory)."); } List <DirectoryEntry> l = new List <DirectoryEntry>(); foreach (ShareFileItem i in Directory.GetFilesAndDirectories()) { DirectoryEntry de = new DirectoryEntry(); de.Name = i.Name; de.Length = this.FileLength(de.Name); l.Add(de); } return(l); }
public IActionResult GetFileNames(string userId) { // Get a reference to a share and then create it ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName); if (!share.Exists()) { share.Create(); } // Get a reference to a directory and create it ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder()); if (!directory.Exists()) { directory.Create(); } var files = directory.GetFilesAndDirectories(); return(Ok(files.ToList())); }
public void Iterate(string connectionString, string shareName, string directory = null) { ShareClient share = new ShareClient(connectionString, shareName); // Track the remaining directories to walk, starting from the root var remaining = new Queue <ShareDirectoryClient>(); if (directory == null) { remaining.Enqueue(share.GetRootDirectoryClient()); } else { remaining.Enqueue(share.GetDirectoryClient(directory)); } while (remaining.Count > 0) { // Get all of the next directory's files and subdirectories ShareDirectoryClient dir = remaining.Dequeue(); foreach (ShareFileItem item in dir.GetFilesAndDirectories()) { if (!File.Exists(@"C:\audio\sales\trusted\Sales\" + item.Name)) { string filepath = Download(connectionString, shareName, directory, item.Name); } //Upload(filepath); // Keep walking down directories if (item.IsDirectory) { remaining.Enqueue(dir.GetSubdirectoryClient(item.Name)); } } } }
// Gets a list of midden file names public List <string> GetFileNames() { var names = new List <string>(); try { var remaining = new Queue <ShareDirectoryClient>(); remaining.Enqueue(shareClient.GetDirectoryClient(this.path)); while (remaining.Count > 0) { ShareDirectoryClient dir = remaining.Dequeue(); foreach (ShareFileItem item in dir.GetFilesAndDirectories()) { if (item.IsDirectory) { remaining.Enqueue(dir.GetSubdirectoryClient(item.Name)); } else if (item.Name.Contains(FILE_EXTENSION)) { Console.WriteLine($" In {dir.Name} found {item.Name}"); names.Add(item.Name); } } } Console.WriteLine($"Found a total of {names.Count} files"); } catch (Exception e) { Console.WriteLine($"An error ocurred: {e}"); } return(names); }
public List <Project> GetProjects( ProjectReader reader) { List <Project> projects = new List <Project>(); try { var remaining = new Queue <ShareDirectoryClient>(); remaining.Enqueue(shareClient.GetDirectoryClient(this.path)); while (remaining.Count > 0) { ShareDirectoryClient dir = remaining.Dequeue(); foreach (ShareFileItem item in dir.GetFilesAndDirectories()) { if (item.IsDirectory) { remaining.Enqueue(dir.GetSubdirectoryClient(item.Name)); } else if (item.Name.Contains(MIPPEN_FILE_SEARCH_TERM)) { Console.WriteLine($" In {dir.Uri.AbsolutePath} found {item.Name}"); ShareFileClient file = dir.GetFileClient(item.Name); ShareFileDownloadInfo fileContents = file.Download(); Project project; using (var stream = fileContents.Content) { project = reader.Read(stream); } if (project is not null) { projects.Add(project); } //string fileString; //using (MemoryStream ms = new MemoryStream()) //{ // fileContents.Content.CopyTo(ms); // fileString = Encoding.UTF8.GetString(ms.ToArray()); //} // //Project project = new Project() //{ // Name = file.Name.Replace(MIPPEN_FILE_EXTENSION, ""), // Description = fileString //}; } } } Console.WriteLine($"Found a total of {projects.Count} files"); } catch (Exception e) { Console.WriteLine($"An error ocurred: {e}"); } return(projects); }
public override void ExecuteCmdlet() { CloudFileDirectory baseDirectory; switch (this.ParameterSetName) { case Constants.DirectoryParameterSetName: baseDirectory = this.Directory; break; case Constants.ShareNameParameterSetName: baseDirectory = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference(); break; case Constants.ShareParameterSetName: baseDirectory = this.Share.GetRootDirectoryReference(); break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. if (this.Context == null) { this.Context = GetStorageContextFromTrack1FileServiceClient(baseDirectory.ServiceClient, DefaultContext); } ShareDirectoryClient baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(baseDirectory, ClientOptions); if (string.IsNullOrEmpty(this.Path)) { ShareDirectoryGetFilesAndDirectoriesOptions listFileOptions = new ShareDirectoryGetFilesAndDirectoriesOptions(); if (!this.ExcludeExtendedInfo.IsPresent) { listFileOptions.Traits = ShareFileTraits.All; listFileOptions.IncludeExtendedInfo = true; } Pageable <ShareFileItem> fileItems = baseDirClient.GetFilesAndDirectories(listFileOptions, this.CmdletCancellationToken); IEnumerable <Page <ShareFileItem> > fileitempages = fileItems.AsPages(); foreach (var page in fileitempages) { foreach (var file in page.Values) { if (!file.IsDirectory) // is file { ShareFileClient shareFileClient = baseDirClient.GetFileClient(file.Name); WriteObject(new AzureStorageFile(shareFileClient, (AzureStorageContext)this.Context, file, ClientOptions)); } else // Dir { ShareDirectoryClient shareDirClient = baseDirClient.GetSubdirectoryClient(file.Name); WriteObject(new AzureStorageFileDirectory(shareDirClient, (AzureStorageContext)this.Context, file, ClientOptions)); } } } } else { if (ExcludeExtendedInfo.IsPresent) { WriteWarning("'-ExcludeExtendedInfo' will be omit, it only works when list file and directory without '-Path'."); } bool foundAFolder = true; ShareDirectoryClient targetDir = baseDirClient.GetSubdirectoryClient(this.Path); ShareDirectoryProperties dirProperties = null; try { dirProperties = targetDir.GetProperties(this.CmdletCancellationToken).Value; } catch (global::Azure.RequestFailedException e) when(e.Status == 404) { foundAFolder = false; } if (foundAFolder) { WriteObject(new AzureStorageFileDirectory(targetDir, (AzureStorageContext)this.Context, dirProperties)); return; } ShareFileClient targetFile = baseDirClient.GetFileClient(this.Path); ShareFileProperties fileProperties = targetFile.GetProperties(this.CmdletCancellationToken).Value; WriteObject(new AzureStorageFile(targetFile, (AzureStorageContext)this.Context, fileProperties, ClientOptions)); } }