public void CloudFileCopyTestTask()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.CreateAsync().Wait();

                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadTextTask(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                source.SetMetadataAsync().Wait();

                CloudFile copy   = share.GetRootDirectoryReference().GetFileReference("copy");
                string    copyId = copy.StartCopyAsync(TestHelper.Defiddler(source)).Result;
                WaitForCopyTask(copy);
                Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, copy.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, copy.CopyState.BytesCopied);
                Assert.AreEqual(copyId, copy.CopyState.CopyId);
                Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                TestHelper.ExpectedExceptionTask(
                    copy.AbortCopyAsync(copyId),
                    "Aborting a copy operation after completion should fail",
                    HttpStatusCode.Conflict,
                    "NoPendingCopyOperation");

                source.FetchAttributesAsync().Wait();
                Assert.IsNotNull(copy.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag);
                Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = DownloadTextTask(copy, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of file not similar");

                copy.FetchAttributesAsync().Wait();
                FileProperties prop1 = copy.Properties;
                FileProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                copy.DeleteAsync().Wait();
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Beispiel #2
0
        public async Task CloudFileCopyTestWithMetadataOverrideAsync()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                await UploadTextAsync(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                await source.SetMetadataAsync();

                CloudFile copy = share.GetRootDirectoryReference().GetFileReference("copy");
                copy.Metadata["Test2"] = "value2";
                string copyId = await copy.StartCopyAsync(TestHelper.Defiddler(source));
                await WaitForCopyAsync(copy);

                Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, copy.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, copy.CopyState.BytesCopied);
                Assert.AreEqual(copyId, copy.CopyState.CopyId);
                Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = await DownloadTextAsync(copy, Encoding.UTF8);

                Assert.AreEqual(data, copyData, "Data inside copy of file not similar");

                await copy.FetchAttributesAsync();

                await source.FetchAttributesAsync();

                FileProperties prop1 = copy.Properties;
                FileProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value2", copy.Metadata["Test2"], false, "Copied metadata not same");
                Assert.IsFalse(copy.Metadata.ContainsKey("Test"), "Source Metadata should not appear in destination file");

                await copy.DeleteAsync();
            }
            finally
            {
                await share.DeleteAsync();
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FileProperties"/> class based on an existing instance.
        /// </summary>
        /// <param name="other">The set of file properties to clone.</param>
        public FileProperties(FileProperties other)
        {
            CommonUtility.AssertNotNull("other", other);

            this.ContentType        = other.ContentType;
            this.ContentDisposition = other.ContentDisposition;
            this.ContentEncoding    = other.ContentEncoding;
            this.ContentLanguage    = other.ContentLanguage;
            this.CacheControl       = other.CacheControl;
            this.ContentMD5         = other.ContentMD5;
            this.Length             = other.Length;
            this.ETag              = other.ETag;
            this.LastModified      = other.LastModified;
            this.IsServerEncrypted = other.IsServerEncrypted;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FileReadStreamBase"/> class.
        /// </summary>
        /// <param name="file">File reference to read from</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the file. If <c>null</c>, no condition is used.</param>
        /// <param name="options">An <see cref="FileRequestOptions"/> object that specifies additional options for the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
        protected FileReadStreamBase(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext)
        {
            if (options.UseTransactionalMD5.Value)
            {
                CommonUtility.AssertInBounds("StreamMinimumReadSizeInBytes", file.StreamMinimumReadSizeInBytes, 1, Constants.MaxRangeGetContentMD5Size);
            }

            this.file           = file;
            this.fileProperties = new FileProperties(file.Properties);
            this.currentOffset  = 0;
            this.streamMinimumReadSizeInBytes = this.file.StreamMinimumReadSizeInBytes;
            this.internalBuffer   = new MultiBufferMemoryStream(file.ServiceClient.BufferManager);
            this.accessCondition  = accessCondition;
            this.options          = options;
            this.operationContext = operationContext;
            this.fileMD5          = (this.options.DisableContentMD5Validation.Value || string.IsNullOrEmpty(this.fileProperties.ContentMD5)) ? null : new MD5Wrapper();
            this.lastException    = null;
        }
Beispiel #5
0
 public static void AssertAreEqual(FileProperties prop1, FileProperties prop2)
 {
     if (prop1 == null)
     {
         Assert.IsNull(prop2);
     }
     else
     {
         Assert.IsNotNull(prop2);
         Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
         Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
         Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
         Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
         Assert.AreEqual(prop1.ContentType, prop2.ContentType);
         Assert.AreEqual(prop1.ETag, prop2.ETag);
         Assert.AreEqual(prop1.LastModified, prop2.LastModified);
         Assert.AreEqual(prop1.Length, prop2.Length);
     }
 }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FileProperties"/> class based on an existing instance.
        /// </summary>
        /// <param name="other">The set of file properties to clone.</param>
        public FileProperties(FileProperties other)
        {
            CommonUtility.AssertNotNull("other", other);

            this.ContentType           = other.ContentType;
            this.ContentDisposition    = other.ContentDisposition;
            this.ContentEncoding       = other.ContentEncoding;
            this.ContentLanguage       = other.ContentLanguage;
            this.CacheControl          = other.CacheControl;
            this.ContentChecksum.MD5   = other.ContentChecksum.MD5;
            this.ContentChecksum.CRC64 = other.ContentChecksum.CRC64;
            this.Length                 = other.Length;
            this.ETag                   = other.ETag;
            this.LastModified           = other.LastModified;
            this.IsServerEncrypted      = other.IsServerEncrypted;
            this.filePermissionKey      = other.filePermissionKey;
            this.ntfsAttributes         = other.ntfsAttributes;
            this.creationTime           = other.creationTime;
            this.lastWriteTime          = other.lastWriteTime;
            this.filePermissionKeyToSet = other.filePermissionKeyToSet;
            this.ntfsAttributesToSet    = other.ntfsAttributesToSet;
            this.creationTimeToSet      = other.creationTimeToSet;
            this.lastWriteTimeToSet     = other.lastWriteTimeToSet;
        }
        private static void CloudFileCopy(bool sourceIsSas, bool destinationIsSas)
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                // Create Source on server
                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                source.SetMetadata();

                // Create Destination on server
                CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination");
                destination.Create(1);

                CloudFile copySource      = source;
                CloudFile copyDestination = destination;

                if (sourceIsSas)
                {
                    // Source SAS must have read permissions
                    SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Read;
                    SharedAccessFilePolicy      policy      = new SharedAccessFilePolicy()
                    {
                        SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                        Permissions            = permissions,
                    };
                    string sasToken = source.GetSharedAccessSignature(policy);

                    // Get source
                    StorageCredentials credentials = new StorageCredentials(sasToken);
                    copySource = new CloudFile(credentials.TransformUri(source.Uri));
                }

                if (destinationIsSas)
                {
                    Assert.IsTrue(sourceIsSas);

                    // Destination SAS must have write permissions
                    SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Write;
                    SharedAccessFilePolicy      policy      = new SharedAccessFilePolicy()
                    {
                        SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                        Permissions            = permissions,
                    };
                    string sasToken = destination.GetSharedAccessSignature(policy);

                    // Get destination
                    StorageCredentials credentials = new StorageCredentials(sasToken);
                    copyDestination = new CloudFile(credentials.TransformUri(destination.Uri));
                }

                // Start copy and wait for completion
                string copyId = copyDestination.StartCopy(TestHelper.Defiddler(copySource));
                WaitForCopy(destination);

                // Check original file references for equality
                Assert.AreEqual(CopyStatus.Success, destination.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, destination.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, destination.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, destination.CopyState.BytesCopied);
                Assert.AreEqual(copyId, destination.CopyState.CopyId);
                Assert.IsTrue(destination.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                if (!destinationIsSas)
                {
                    // Abort Copy is not supported for SAS destination
                    TestHelper.ExpectedException(
                        () => copyDestination.AbortCopy(copyId),
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");
                }

                source.FetchAttributes();
                Assert.IsNotNull(destination.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, destination.Properties.ETag);
                Assert.IsTrue(destination.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = DownloadText(destination, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of file not equal.");

                destination.FetchAttributes();
                FileProperties prop1 = destination.Properties;
                FileProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", destination.Metadata["Test"], false, "Copied metadata not same");

                destination.Delete();
                source.Delete();
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
        private static void CloudFileCopy(bool sourceIsSas, bool destinationIsSas)
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                // Create Source on server
                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                source.SetMetadata();

                string permission    = "O:S-1-5-21-2127521184-1604012920-1887927527-21560751G:S-1-5-21-2127521184-1604012920-1887927527-513D:AI(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200a9;;;S-1-5-21-397955417-626881126-188441444-3053964)";
                string permissionKey = share.CreateFilePermission(permission);
                source.Properties.FilePermissionKey = permissionKey;

                var attributes   = CloudFileNtfsAttributes.NoScrubData | CloudFileNtfsAttributes.Temporary;
                var creationTime = DateTimeOffset.UtcNow.AddDays(-1);

                source.Properties.NtfsAttributes = attributes;
                DateTimeOffset lastWriteTime = DateTimeOffset.UtcNow;
                source.Properties.LastWriteTime = lastWriteTime;

                source.Properties.CreationTime = creationTime;
                source.SetProperties();

                // Create Destination on server
                CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination");
                destination.Create(1);

                CloudFile copySource      = source;
                CloudFile copyDestination = destination;

                if (sourceIsSas)
                {
                    // Source SAS must have read permissions
                    SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Read;
                    SharedAccessFilePolicy      policy      = new SharedAccessFilePolicy()
                    {
                        SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                        Permissions            = permissions,
                    };

                    string sasToken = share.GetSharedAccessSignature(policy);

                    // Get source
                    StorageCredentials credentials = new StorageCredentials(sasToken);
                    copySource = new CloudFile(credentials.TransformUri(source.Uri));
                }

                if (destinationIsSas)
                {
                    Assert.IsTrue(sourceIsSas);

                    // Destination SAS must have write permissions
                    SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Write;
                    SharedAccessFilePolicy      policy      = new SharedAccessFilePolicy()
                    {
                        SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                        Permissions            = permissions,
                    };
                    string sasToken = destination.GetSharedAccessSignature(policy);

                    // Get destination
                    StorageCredentials credentials = new StorageCredentials(sasToken);
                    copyDestination = new CloudFile(credentials.TransformUri(destination.Uri));
                }

                // Start copy and wait for completion
                string copyId = copyDestination.StartCopy(TestHelper.Defiddler(copySource),
                                                          null,
                                                          null,
                                                          new FileCopyOptions()
                {
                    PreservePermissions    = true,
                    PreserveCreationTime   = true,
                    PreserveLastWriteTime  = true,
                    PreserveNtfsAttributes = true,
                    SetArchive             = false
                },
                                                          null, null);

                WaitForCopy(destination);

                // Check original file references for equality
                Assert.AreEqual(CopyStatus.Success, destination.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, destination.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, destination.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, destination.CopyState.BytesCopied);
                Assert.AreEqual(copyId, destination.CopyState.CopyId);
                Assert.IsTrue(destination.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                if (!destinationIsSas)
                {
                    // Abort Copy is not supported for SAS destination
                    TestHelper.ExpectedException(
                        () => copyDestination.AbortCopy(copyId),
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");
                }

                source.FetchAttributes();
                Assert.IsNotNull(destination.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, destination.Properties.ETag);
                Assert.IsTrue(destination.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = DownloadText(destination, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of file not equal.");

                destination.FetchAttributes();
                FileProperties prop1 = destination.Properties;
                FileProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);
                Assert.AreEqual(lastWriteTime, destination.Properties.LastWriteTime.Value);
                Assert.AreEqual(creationTime, destination.Properties.CreationTime.Value);
                Assert.AreEqual(attributes, destination.Properties.NtfsAttributes.Value);
                Assert.IsNotNull(destination.Properties.FilePermissionKey);
                Assert.IsNull(destination.FilePermission);

                Assert.AreEqual("value", destination.Metadata["Test"], false, "Copied metadata not same");

                destination.Delete();
                source.Delete();
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
        public void CloudFileCopyIgnoreReadOnlyAndSetArchiveTest()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                // Create Source on server
                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                // Create Destination on server
                CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination");
                destination.Create(1);

                destination.Properties.NtfsAttributes = CloudFileNtfsAttributes.Hidden | CloudFileNtfsAttributes.ReadOnly;
                destination.SetProperties();

                string permission    = "O:S-1-5-21-2127521184-1604012920-1887927527-21560751G:S-1-5-21-2127521184-1604012920-1887927527-513D:AI(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200a9;;;S-1-5-21-397955417-626881126-188441444-3053964)";
                string permissionKey = share.CreateFilePermission(permission);
                destination.Properties.FilePermissionKey = permissionKey;

                CloudFile copySource      = source;
                CloudFile copyDestination = destination;

                // Start copy and wait for completion
                string copyId = copyDestination.StartCopy(TestHelper.Defiddler(copySource),
                                                          null,
                                                          null,
                                                          new FileCopyOptions()
                {
                    PreservePermissions = false,
                    IgnoreReadOnly      = true,
                    SetArchive          = true
                },
                                                          null,
                                                          null);

                WaitForCopy(destination);

                // Check original file references for equality
                Assert.AreEqual(CopyStatus.Success, destination.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, destination.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, destination.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, destination.CopyState.BytesCopied);
                Assert.AreEqual(copyId, destination.CopyState.CopyId);
                Assert.IsTrue(destination.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                TestHelper.ExpectedException(
                    () => copyDestination.AbortCopy(copyId),
                    "Aborting a copy operation after completion should fail",
                    HttpStatusCode.Conflict,
                    "NoPendingCopyOperation");

                source.FetchAttributes();
                Assert.IsNotNull(destination.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, destination.Properties.ETag);
                Assert.IsTrue(destination.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = DownloadText(destination, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of file not equal.");

                destination.FetchAttributes();
                FileProperties prop1 = destination.Properties;
                FileProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);
                Assert.AreEqual(CloudFileNtfsAttributes.Archive, destination.Properties.NtfsAttributes.Value);
                Assert.IsNotNull(destination.Properties.FilePermissionKey);
                Assert.IsNull(destination.FilePermission);

                destination.Delete();
                source.Delete();
            }
            finally
            {
                share.DeleteIfExists();
            }
        }