public async Task CloudFileDirectoryCreateDirectoryUsingPrefixAsync()
        {
            CloudFileClient client = GenerateCloudFileClient();
            string          name   = GetRandomShareName();
            CloudFileShare  share  = client.GetShareReference(name);

            try
            {
                await share.CreateAsync();

                CloudFileDirectory dir1    = share.GetRootDirectoryReference().GetDirectoryReference("Dir1");
                CloudFileDirectory dir2    = share.GetRootDirectoryReference().GetDirectoryReference("Dir1/Dir2");
                OperationContext   context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await dir2.CreateAsync(null, context),
                    context,
                    "Try to create directory hierarchy by specifying prefix",
                    HttpStatusCode.NotFound);

                await dir1.CreateAsync();

                await dir2.CreateAsync();
            }
            finally
            {
                share.DeleteAsync().AsTask().Wait();
            }
        }
        public async Task CloudFileDirectoryFileCreateWithoutDirectoryAsync()
        {
            CloudFileClient    client        = GenerateCloudFileClient();
            string             name          = GetRandomShareName();
            CloudFileShare     share         = client.GetShareReference(name);
            CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();

            try
            {
                await share.CreateAsync();

                CloudFileDirectory dir     = rootDirectory.GetDirectoryReference("Dir1");
                CloudFile          file    = dir.GetFileReference("file1");
                OperationContext   context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await file.CreateAsync(0, null, null, context),
                    context,
                    "Creating a file when the directory has not been created should throw",
                    HttpStatusCode.NotFound,
                    "ParentNotFound");

                // File creation directly in the share should pass.
                CloudFile file2 = rootDirectory.GetFileReference("file2");
                await file2.CreateAsync(0);

                await dir.CreateAsync();

                await file.CreateAsync(0);
            }
            finally
            {
                share.DeleteAsync().AsTask().Wait();
            }
        }
Example #3
0
        public async Task CloudFileDirectoryApisInShareSnapshotAsync()
        {
            CloudFileShare share = GetRandomShareReference();
            await share.CreateAsync();

            CloudFileDirectory dir = share.GetRootDirectoryReference().GetDirectoryReference("dir1");
            await dir.CreateAsync();

            dir.Metadata["key1"] = "value1";
            await dir.SetMetadataAsync(null, null, null);

            CloudFileShare snapshot = await share.SnapshotAsync();

            CloudFileDirectory snapshotDir = snapshot.GetRootDirectoryReference().GetDirectoryReference("dir1");

            dir.Metadata["key2"] = "value2";
            await dir.SetMetadataAsync(null, null, null);

            await snapshotDir.FetchAttributesAsync();

            Assert.IsTrue(snapshotDir.Metadata.Count == 1 && snapshotDir.Metadata["key1"].Equals("value1"));
            Assert.IsNotNull(snapshotDir.Properties.ETag);

            await dir.FetchAttributesAsync();

            Assert.IsTrue(dir.Metadata.Count == 2 && dir.Metadata["key2"].Equals("value2"));
            Assert.IsNotNull(dir.Properties.ETag);
            Assert.AreNotEqual(dir.Properties.ETag, snapshotDir.Properties.ETag);

            await snapshot.DeleteAsync();

            await share.DeleteAsync();
        }
Example #4
0
        public async Task CloudFileDirectorySASAsync()
        {
            CloudFileDirectory dir  = this.testShare.GetRootDirectoryReference().GetDirectoryReference("dirfile");
            CloudFile          file = dir.GetFileReference("dirfile");

            await dir.CreateAsync();

            await file.CreateAsync(512);

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
            {
                SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                Permissions            = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.List
            };

            string             sasToken = file.GetSharedAccessSignature(policy);
            CloudFileDirectory sasDir   = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            OperationContext   context  = new OperationContext();
            await TestHelper.ExpectedExceptionAsync(
                async() => await sasDir.FetchAttributesAsync(null /* accessCondition */, null /* options */, context),
                context,
                "Fetching attributes of a directory using a file SAS should fail",
                HttpStatusCode.Forbidden,
                "");

            sasToken = this.testShare.GetSharedAccessSignature(policy);
            sasDir   = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            await sasDir.FetchAttributesAsync();
        }
Example #5
0
        public async Task CloudFileDirectorySetMetadataAsync()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFileDirectory directory = share.GetRootDirectoryReference().GetDirectoryReference("directory1");
                await directory.CreateAsync();

                CloudFileDirectory directory2 = share.GetRootDirectoryReference().GetDirectoryReference("directory1");
                await directory2.FetchAttributesAsync();

                Assert.AreEqual(0, directory2.Metadata.Count);

                directory.Metadata["key1"] = null;
                OperationContext context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await directory.SetMetadataAsync(null /* accessConditions */, null /* options */, context),
                    context,
                    "Metadata keys should have a non-null value",
                    HttpStatusCode.Unused);

                directory.Metadata["key1"] = "";
                await TestHelper.ExpectedExceptionAsync(
                    async() => await directory.SetMetadataAsync(null /* accessConditions */, null /* options */, context),
                    context,
                    "Metadata keys should have a non-empty value",
                    HttpStatusCode.Unused);

                directory.Metadata["key1"] = "value1";
                await directory.SetMetadataAsync(null /* accessConditions */, null /* options */, context);

                await directory2.FetchAttributesAsync();

                Assert.AreEqual(1, directory2.Metadata.Count);
                Assert.AreEqual("value1", directory2.Metadata["key1"]);

                directory.Metadata.Clear();
                await directory.SetMetadataAsync(null /* accessConditions */, null /* options */, context);

                await directory2.FetchAttributesAsync();

                Assert.AreEqual(0, directory2.Metadata.Count);
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Example #6
0
        public async Task CloudFileDirectoryDeleteIfExistsAsync()
        {
            CloudFileShare share = GetRandomShareReference();
            await share.CreateAsync();

            try
            {
                CloudFileDirectory directory = share.GetRootDirectoryReference().GetDirectoryReference("directory1");
                Assert.IsFalse(await directory.DeleteIfExistsAsync());
                await directory.CreateAsync();

                Assert.IsTrue(await directory.DeleteIfExistsAsync());
                Assert.IsFalse(await directory.DeleteIfExistsAsync());
            }
            finally
            {
                share.DeleteAsync().Wait();
            }
        }
        public async Task CloudFileApisInShareSnapshotAsync()
        {
            CloudFileClient client = GenerateCloudFileClient();
            string          name   = GetRandomShareName();
            CloudFileShare  share  = client.GetShareReference(name);
            await share.CreateAsync();

            CloudFileDirectory dir = share.GetRootDirectoryReference().GetDirectoryReference("dir1");
            await dir.CreateAsync();

            CloudFile file = dir.GetFileReference("file");
            await file.CreateAsync(1024);

            file.Metadata["key1"] = "value1";
            await file.SetMetadataAsync();

            CloudFileShare snapshot     = share.SnapshotAsync().Result;
            CloudFile      snapshotFile = snapshot.GetRootDirectoryReference().GetDirectoryReference("dir1").GetFileReference("file");

            file.Metadata["key2"] = "value2";
            await file.SetMetadataAsync();

            await snapshotFile.FetchAttributesAsync();

            Assert.IsTrue(snapshotFile.Metadata.Count == 1 && snapshotFile.Metadata["key1"].Equals("value1"));
            Assert.IsNotNull(snapshotFile.Properties.ETag);

            await file.FetchAttributesAsync();

            Assert.IsTrue(file.Metadata.Count == 2 && file.Metadata["key2"].Equals("value2"));
            Assert.IsNotNull(file.Properties.ETag);
            Assert.AreNotEqual(file.Properties.ETag, snapshotFile.Properties.ETag);

            CloudFile snapshotFile2 = new CloudFile(snapshotFile.SnapshotQualifiedStorageUri, client.Credentials);

            Assert.IsTrue(snapshotFile2.ExistsAsync().Result);
            Assert.IsTrue(snapshotFile2.Share.SnapshotTime.HasValue);

            await snapshot.DeleteAsync();

            await share.DeleteAsync();
        }
Example #8
0
        public async Task CloudFileDirectoryApisInvalidApisInShareSnapshotAsync()
        {
            CloudFileShare share = GetRandomShareReference();
            await share.CreateAsync();

            CloudFileShare snapshot = await share.SnapshotAsync();

            CloudFileDirectory dir = snapshot.GetRootDirectoryReference().GetDirectoryReference("dir1");

            try
            {
                dir.CreateAsync().Wait();
                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }
            try
            {
                dir.DeleteAsync().Wait();
                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }
            try
            {
                dir.SetMetadataAsync(null, null, null).Wait();
                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }

            snapshot.DeleteAsync().Wait();
            share.DeleteAsync().Wait();
        }
Example #9
0
        private async Task <bool> CloudFileDirectorySetupAsync(CloudFileShare share)
        {
            try
            {
                CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();
                for (int i = 1; i < 3; i++)
                {
                    CloudFileDirectory topDirectory = rootDirectory.GetDirectoryReference("TopDir" + i);
                    await topDirectory.CreateAsync();

                    for (int j = 1; j < 3; j++)
                    {
                        CloudFileDirectory midDirectory = topDirectory.GetDirectoryReference("MidDir" + j);
                        await midDirectory.CreateAsync();

                        for (int k = 1; k < 3; k++)
                        {
                            CloudFileDirectory endDirectory = midDirectory.GetDirectoryReference("EndDir" + k);
                            await endDirectory.CreateAsync();

                            CloudFile file1 = endDirectory.GetFileReference("EndFile" + k);
                            await file1.CreateAsync(0);
                        }
                    }

                    CloudFile file2 = topDirectory.GetFileReference("File" + i);
                    await file2.CreateAsync(0);
                }

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
 public Task CreateDirectoryAsync(CloudFileDirectory directory, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return directory.CreateAsync(options, operationContext, cancellationToken);
 }