public IDisposable GetNewDirectory(out DirectoryClient directory, DataLakeServiceClient service = default, string fileSystemName = default, string directoryName = default) { IDisposable disposingFileSystem = GetNewFileSystem(out FileSystemClient fileSystem, service, fileSystemName); directory = InstrumentClient(fileSystem.GetDirectoryClient(directoryName ?? GetNewDirectoryName())); _ = directory.CreateAsync().Result; return(disposingFileSystem); }
public async Task FileSample() { // Instantiate a new FileServiceClient using a connection string. FileServiceClient fileServiceClient = new FileServiceClient(TestConfigurations.DefaultTargetTenant.ConnectionString); // Instantiate new ShareClient ShareClient shareClient = fileServiceClient.GetShareClient("myshare3"); try { // Create Share in the Service await shareClient.CreateAsync(); // Instantiate new DirectoryClient DirectoryClient directoryClient = shareClient.GetDirectoryClient("mydirectory"); // Create Directory in the Service await directoryClient.CreateAsync(); // Instantiate new FileClient FileClient fileClient = directoryClient.GetFileClient("myfile"); // Create File in the Service await fileClient.CreateAsync(maxSize : 1024); // Upload data to File using (FileStream fileStream = File.OpenRead("Samples/SampleSource.txt")) { await fileClient.UploadRangeAsync( writeType : FileRangeWriteType.Update, range : new HttpRange(0, 1024), content : fileStream); } // Download file using (FileStream fileStream = File.Create("SampleDestination.txt")) { Response <StorageFileDownloadInfo> downloadResponse = await fileClient.DownloadAsync(); await downloadResponse.Value.Content.CopyToAsync(fileStream); } // Delete File in the Service await fileClient.DeleteAsync(); // Delete Directory in the Service await directoryClient.DeleteAsync(); } finally { // Delete Share in the service await shareClient.DeleteAsync(); } }
public async Task DownloadAsync() { // Create a temporary Lorem Ipsum file on disk that we can upload string originalPath = CreateTempFile(SampleFileContent); // Get a temporary path on disk where we can download the file string downloadPath = CreateTempPath(); // Get a connection string to our Azure Storage account. string connectionString = ConnectionString; // Get a reference to a share named "sample-share" and then create it ShareClient share = new ShareClient(connectionString, Randomize("sample-share")); await share.CreateAsync(); try { // Get a reference to a directory named "sample-dir" and then create it DirectoryClient directory = share.GetDirectoryClient(Randomize("sample-dir")); await directory.CreateAsync(); // Get a reference to a file named "sample-file" in directory "sample-dir" FileClient file = directory.GetFileClient(Randomize("sample-file")); // Upload the file using (FileStream stream = File.OpenRead(originalPath)) { await file.CreateAsync(stream.Length); await file.UploadRangeAsync( FileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } // Download the file StorageFileDownloadInfo download = await file.DownloadAsync(); using (FileStream stream = File.OpenWrite(downloadPath)) { await download.Content.CopyToAsync(stream); } // Verify the contents Assert.AreEqual(SampleFileContent, File.ReadAllText(downloadPath)); } finally { // Clean up after the test when we're finished await share.DeleteAsync(); } }
public async Task UploadAsync() { // Create a temporary Lorem Ipsum file on disk that we can upload string path = CreateTempFile(SampleFileContent); // Get a connection string to our Azure Storage account. You can // obtain your connection string from the Azure Portal (click // Access Keys under Settings in the Portal Storage account blade) // or using the Azure CLI with: // // az storage account show-connection-string --name <account_name> --resource-group <resource_group> // // And you can provide the connection string to your application // using an environment variable. string connectionString = ConnectionString; // Get a reference to a share named "sample-share" and then create it ShareClient share = new ShareClient(connectionString, Randomize("sample-share")); await share.CreateAsync(); try { // Get a reference to a directory named "sample-dir" and then create it DirectoryClient directory = share.GetDirectoryClient(Randomize("sample-dir")); await directory.CreateAsync(); // Get a reference to a file named "sample-file" in directory "sample-dir" FileClient file = directory.GetFileClient(Randomize("sample-file")); // Upload the file using (FileStream stream = File.OpenRead(path)) { await file.CreateAsync(stream.Length); await file.UploadRangeAsync( FileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } // Verify the file exists StorageFileProperties properties = await file.GetPropertiesAsync(); Assert.AreEqual(SampleFileContent.Length, properties.ContentLength); } finally { // Clean up after the test when we're finished await share.DeleteAsync(); } }
public async Task DirectorySample() { // Instantiate a new FileServiceClient using a connection string. FileServiceClient fileServiceClient = new FileServiceClient(TestConfigurations.DefaultTargetTenant.ConnectionString); // Instantiate new ShareClient ShareClient shareClient = fileServiceClient.GetShareClient($"myshare2-{Guid.NewGuid()}"); try { // Create Share in the Service await shareClient.CreateAsync(); // Instantiate new DirectoryClient DirectoryClient directoryClient = shareClient.GetDirectoryClient("mydirectory"); // Create Directory in the Service await directoryClient.CreateAsync(); // Instantiate new DirectoryClient DirectoryClient subDirectoryClient = directoryClient.GetSubdirectoryClient("mysubdirectory"); // Create sub directory await subDirectoryClient.CreateAsync(); // List Files and Directories await foreach (StorageFileItem item in directoryClient.GetFilesAndDirectoriesAsync()) { var type = item.IsDirectory ? "dir" : "file"; Console.WriteLine($"{type}: {item.Name}"); } // Delete sub directory in the Service await subDirectoryClient.DeleteAsync(); // Delete Directory in the Service await directoryClient.DeleteAsync(); } finally { // Delete Share in the service await shareClient.DeleteAsync(); } }
public async Task DirectorySample() { // Instantiate a new FileServiceClient using a connection string. FileServiceClient fileServiceClient = new FileServiceClient(TestConfigurations.DefaultTargetTenant.ConnectionString); // Instantiate new ShareClient ShareClient shareClient = fileServiceClient.GetShareClient("myshare2"); try { // Create Share in the Service await shareClient.CreateAsync(); // Instantiate new DirectoryClient DirectoryClient directoryClient = shareClient.GetDirectoryClient("mydirectory"); // Create Directory in the Service await directoryClient.CreateAsync(); // Instantiate new DirectoryClient DirectoryClient subDirectoryClient = directoryClient.GetDirectoryClient("mysubdirectory"); // Create sub directory await subDirectoryClient.CreateAsync(); // List Files and Directories Response <FilesAndDirectoriesSegment> listResponse = await directoryClient.ListFilesAndDirectoriesSegmentAsync(); // Delete sub directory in the Service await subDirectoryClient.DeleteAsync(); // Delete Directory in the Service await directoryClient.DeleteAsync(); } finally { // Delete Share in the service await shareClient.DeleteAsync(); } }
public static async Task <DisposingDirectory> CreateAsync(DisposingShare test, DirectoryClient directory) { await directory.CreateAsync(); return(new DisposingDirectory(test, directory)); }