Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        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();
            }
        }