Пример #1
0
        private static async Task CloudBlockBlobCopyFromCloudFileImpl(Func <CloudFile, CloudBlockBlob, string> copyFunc)
        {
            CloudFileClient fileClient = GenerateCloudFileClient();

            string         name  = GetRandomContainerName();
            CloudFileShare share = fileClient.GetShareReference(name);

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                try
                {
                    await container.CreateAsync();

                    await share.CreateAsync();

                    CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");
                    byte[]    data   = GetRandomBuffer(1024);

                    await source.UploadFromByteArrayAsync(data, 0, data.Length);

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

                    var sasToken = source.GetSharedAccessSignature(new SharedAccessFilePolicy
                    {
                        Permissions            = SharedAccessFilePermissions.Read,
                        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
                    });

                    Uri fileSasUri = new Uri(source.StorageUri.PrimaryUri.ToString() + sasToken);

                    source = new CloudFile(fileSasUri);

                    CloudBlockBlob copy   = container.GetBlockBlobReference("copy");
                    string         copyId = copyFunc(source, copy);
                    Assert.AreEqual(BlobType.BlockBlob, copy.BlobType);

                    try
                    {
                        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.IsFalse(copy.Properties.IsIncrementalCopy);
                        Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));
                    }
                    catch (NullReferenceException)
                    {
                        // potential null ref in the WaitForCopyTask and CopyState check implementation
                    }

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await copy.AbortCopyAsync(copyId, null, null, opContext),
                        opContext,
                        "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)));

                    byte[] copyData = new byte[source.Properties.Length];

                    await copy.DownloadToByteArrayAsync(copyData, 0);

                    Assert.IsTrue(data.SequenceEqual(copyData), "Data inside copy of blob not similar");

                    copy.FetchAttributesAsync().Wait();
                    BlobProperties 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.DeleteIfExistsAsync().Wait();
                }
                finally
                {
                    share.DeleteIfExistsAsync().Wait();
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }