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 IDisposable GetNewFile(out FileClient file, DataLakeServiceClient service = default, string fileSystemName = default, string directoryName = default, string fileName = default) { IDisposable disposingFileSystem = GetNewFileSystem(out FileSystemClient fileSystem, service, fileSystemName); DirectoryClient directory = InstrumentClient(fileSystem.GetDirectoryClient(directoryName ?? GetNewDirectoryName())); _ = directory.CreateAsync().Result; file = InstrumentClient(directory.GetFileClient(fileName ?? GetNewFileName())); _ = file.CreateAsync().Result; return(disposingFileSystem); }
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(); } }
/// <summary> /// Performs the tasks needed to initialize and set up the environment for an instance /// of the test scenario. When multiple instances are run in parallel, setup will be /// run once for each prior to its execution. /// </summary> /// public async override Task SetupAsync() { await base.SetupAsync(); Payload = RandomStream.Create(Options.Size); // Create the test file that will be used as the basis for uploading. using var randomStream = RandomStream.Create(1024); await FileClient.CreateAsync(); await FileClient.UploadAsync(randomStream, true); }
public virtual async Task <Response <FileClient> > CreateFileAsync( string fileName, PathHttpHeaders?httpHeaders = default, Metadata metadata = default, string permissions = default, string umask = default, DataLakeRequestConditions conditions = default, CancellationToken cancellationToken = default) { FileClient fileClient = GetFileClient(fileName); Response <PathInfo> response = await fileClient.CreateAsync( httpHeaders, metadata, permissions, umask, conditions, cancellationToken) .ConfigureAwait(false); return(Response.FromValue( fileClient, response.GetRawResponse())); }
public async Task TraverseAsync() { // Create a temporary Lorem Ipsum file on disk that we can upload string originalPath = CreateTempFile(SampleFileContent); // 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 { // Create a bunch of directories DirectoryClient first = await share.CreateDirectoryAsync("first"); await first.CreateSubdirectoryAsync("a"); await first.CreateSubdirectoryAsync("b"); DirectoryClient second = await share.CreateDirectoryAsync("second"); await second.CreateSubdirectoryAsync("c"); await second.CreateSubdirectoryAsync("d"); await share.CreateDirectoryAsync("third"); DirectoryClient fourth = await share.CreateDirectoryAsync("fourth"); DirectoryClient deepest = await fourth.CreateSubdirectoryAsync("e"); // Upload a file named "file" FileClient file = deepest.GetFileClient("file"); using (FileStream stream = File.OpenRead(originalPath)) { await file.CreateAsync(stream.Length); await file.UploadRangeAsync( FileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } // Keep track of all the names we encounter List <string> names = new List <string>(); // Track the remaining directories to walk, starting from the root Queue <DirectoryClient> remaining = new Queue <DirectoryClient>(); remaining.Enqueue(share.GetRootDirectoryClient()); while (remaining.Count > 0) { // Get all of the next directory's files and subdirectories DirectoryClient dir = remaining.Dequeue(); await foreach (StorageFileItem item in dir.GetFilesAndDirectoriesAsync()) { // Track the name of the item names.Add(item.Name); // Keep walking down directories if (item.IsDirectory) { remaining.Enqueue(dir.GetSubdirectoryClient(item.Name)); } } } // Verify we've seen everything Assert.AreEqual(10, names.Count); Assert.Contains("first", names); Assert.Contains("second", names); Assert.Contains("third", names); Assert.Contains("fourth", names); Assert.Contains("a", names); Assert.Contains("b", names); Assert.Contains("c", names); Assert.Contains("d", names); Assert.Contains("e", names); Assert.Contains("file", names); } finally { // Clean up after the test when we're finished await share.DeleteAsync(); } }
public static async Task <DisposingFile> CreateAsync(DisposingDirectory test, FileClient file) { await file.CreateAsync(maxSize : Constants.MB); return(new DisposingFile(test, file)); }