public async Task BlockBlobWriteStreamOpenWithAccessConditionAsync() { CloudBlobContainer container = GetRandomContainerReference(); await container.CreateAsync(); try { OperationContext context = new OperationContext(); CloudBlockBlob existingBlob = container.GetBlockBlobReference("blob"); await existingBlob.PutBlockListAsync(new List <string>()); CloudBlockBlob blob = container.GetBlockBlobReference("blob2"); AccessCondition accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await blob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.NotFound); blob = container.GetBlockBlobReference("blob3"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(existingBlob.Properties.ETag); IOutputStream blobStream = await blob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); blob = container.GetBlockBlobReference("blob4"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); blobStream = await blob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); blob = container.GetBlockBlobReference("blob5"); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); blobStream = await blob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); blob = container.GetBlockBlobReference("blob6"); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); blobStream = await blob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfMatchCondition(blob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(blob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(existingBlob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.NotModified); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); await TestHelper.ExpectedExceptionAsync( () => { blobStream.Dispose(); return(Task.FromResult(true)); }, context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.Conflict); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.NotModified); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); await existingBlob.SetPropertiesAsync(); await TestHelper.ExpectedExceptionAsync( () => { blobStream.Dispose(); return(Task.FromResult(true)); }, context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.PreconditionFailed); blob = container.GetBlockBlobReference("blob7"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); blobStream = await blob.OpenWriteAsync(accessCondition, null, context); await blob.PutBlockListAsync(new List <string>()); await TestHelper.ExpectedExceptionAsync( () => { blobStream.Dispose(); return(Task.FromResult(true)); }, context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.Conflict); blob = container.GetBlockBlobReference("blob8"); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); await existingBlob.SetPropertiesAsync(); await TestHelper.ExpectedExceptionAsync( () => { blobStream.Dispose(); return(Task.FromResult(true)); }, context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.PreconditionFailed); } finally { container.DeleteAsync().AsTask().Wait(); } }
public async Task CloudBlobDirectoryFlatListingAsync() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { await container.CreateAsync(); if (await CloudBlobDirectorySetupWithDelimiterAsync(container, delimiter)) { BlobResultSegment segment = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter, false, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList1 = new List <IListBlobItem>(); simpleList1.AddRange(segment.Results); while (segment.ContinuationToken != null) { segment = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter, false, BlobListingDetails.None, null, segment.ContinuationToken, null, null); simpleList1.AddRange(segment.Results); } Assert.IsTrue(simpleList1.Count == 3); IListBlobItem item11 = simpleList1.ElementAt(0); Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1")); IListBlobItem item12 = simpleList1.ElementAt(1); Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter)); IListBlobItem item13 = simpleList1.ElementAt(2); Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter)); CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13; BlobResultSegment segment2 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList2 = new List <IListBlobItem>(); simpleList2.AddRange(segment2.Results); while (segment2.ContinuationToken != null) { segment2 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, segment2.ContinuationToken, null, null); simpleList2.AddRange(segment2.Results); } Assert.IsTrue(simpleList2.Count == 2); IListBlobItem item21 = simpleList2.ElementAt(0); Assert.IsTrue(item21.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item22 = simpleList2.ElementAt(1); Assert.IsTrue(item22.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter + "EndBlob2")); BlobResultSegment segment3 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList3 = new List <IListBlobItem>(); simpleList3.AddRange(segment3.Results); while (segment3.ContinuationToken != null) { segment3 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, segment3.ContinuationToken, null, null); simpleList3.AddRange(segment3.Results); } Assert.IsTrue(simpleList3.Count == 2); IListBlobItem item31 = simpleList3.ElementAt(0); Assert.IsTrue(item31.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter)); IListBlobItem item32 = simpleList3.ElementAt(1); Assert.IsTrue(item32.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter)); BlobResultSegment segment4 = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList4 = new List <IListBlobItem>(); simpleList4.AddRange(segment4.Results); while (segment4.ContinuationToken != null) { segment4 = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, segment4.ContinuationToken, null, null); simpleList4.AddRange(segment4.Results); } Assert.IsTrue(simpleList4.Count == 2); IListBlobItem item41 = simpleList4.ElementAt(0); Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item42 = simpleList4.ElementAt(1); Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2")); } } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } } }
public async Task CloudBlobContainerWebContainerOperationsAsync() { // Test operations with shard key CloudBlobClient blobClient = GenerateCloudBlobClient(); CloudBlobContainer webContainer = blobClient.GetContainerReference("$web"); try { await webContainer.DeleteIfExistsAsync(); Assert.IsFalse(await webContainer.ExistsAsync()); await TestHelper.SpinUpToNSecondsIgnoringFailuresAsync(async() => await webContainer.CreateAsync(), 120); Assert.IsTrue(await webContainer.ExistsAsync()); Assert.IsTrue( (await blobClient.ListContainersSegmentedAsync("$", null)) .Results .Any(container => container.Name == webContainer.Name) ); await ValidateWebContainerAsync(webContainer); // Clear out the old data, faster than deleting / re-creating the container. foreach (CloudBlob blob in (await webContainer.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.All, default(int?), default(BlobContinuationToken), default(BlobRequestOptions), default(OperationContext))).Results) { await blob.DeleteAsync(); } // Test relevant operations with a service SAS. string webContainerSAS = webContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy() { SharedAccessExpiryTime = DateTime.Now + TimeSpan.FromDays(30), Permissions = SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Delete | SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Write }); await ValidateWebContainerAsync(new CloudBlobContainer(new Uri(webContainer.Uri + webContainerSAS))); await webContainer.DeleteAsync(); Assert.IsFalse( (await blobClient.ListContainersSegmentedAsync("$", null)) .Results .Any(container => container.Name == webContainer.Name) ); } finally { webContainer.DeleteIfExistsAsync().Wait(); } }
public async Task PageBlobWriteStreamBasicTestAsync() { byte[] buffer = GetRandomBuffer(6 * 512); CloudBlobContainer container = GetRandomContainerReference(); container.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2; try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamWriteSizeInBytes = 8 * 512; using (MemoryStream wholeBlob = new MemoryStream()) { using (Stream writeStream = await blob.OpenWriteAsync(buffer.Length * 3)) { Stream blobStream = writeStream; for (int i = 0; i < 3; i++) { await blobStream.WriteAsync(buffer, 0, buffer.Length); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } await blobStream.FlushAsync(); } using (MemoryStream downloadedBlob = new MemoryStream()) { await blob.DownloadToStreamAsync(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } using (Stream writeStream = await blob.OpenWriteAsync(null)) { Stream blobStream = writeStream; blobStream.Seek(buffer.Length / 2, SeekOrigin.Begin); wholeBlob.Seek(buffer.Length / 2, SeekOrigin.Begin); for (int i = 0; i < 2; i++) { blobStream.Write(buffer, 0, buffer.Length); wholeBlob.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } await blobStream.FlushAsync(); } using (MemoryStream downloadedBlob = new MemoryStream()) { await blob.DownloadToStreamAsync(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } } } finally { container.DeleteAsync().Wait(); } }
public async Task CloudBlobClientListBlobsSegmentedWithPrefixAsync() { string name = "bb" + GetRandomContainerName(); CloudBlobClient blobClient = GenerateCloudBlobClient(); CloudBlobContainer rootContainer = blobClient.GetRootContainerReference(); CloudBlobContainer container = blobClient.GetContainerReference(name); try { await rootContainer.CreateIfNotExistsAsync(); await container.CreateAsync(); List <string> blobNames = await CreateBlobsAsync(container, 3, BlobType.BlockBlob); List <string> rootBlobNames = await CreateBlobsAsync(rootContainer, 2, BlobType.BlockBlob); BlobResultSegment results; BlobContinuationToken token = null; do { results = await blobClient.ListBlobsSegmentedAsync("bb", token); token = results.ContinuationToken; foreach (CloudBlockBlob blob in results.Results) { await blob.DeleteAsync(); rootBlobNames.Remove(blob.Name); } }while (token != null); Assert.AreEqual(0, rootBlobNames.Count); results = await blobClient.ListBlobsSegmentedAsync("bb", token); Assert.AreEqual(0, results.Results.Count()); Assert.IsNull(results.ContinuationToken); results = await blobClient.ListBlobsSegmentedAsync(name, token); Assert.AreEqual(0, results.Results.Count()); Assert.IsNull(results.ContinuationToken); token = null; do { results = await blobClient.ListBlobsSegmentedAsync(name + "/", token); token = results.ContinuationToken; foreach (CloudBlockBlob blob in results.Results) { Assert.IsTrue(blobNames.Remove(blob.Name)); } }while (token != null); Assert.AreEqual(0, blobNames.Count); } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task CloudBlobClientMaximumExecutionTimeoutShouldNotBeHonoredForStreamsAsync() { CloudBlobClient blobClient = GenerateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(Guid.NewGuid().ToString("N")); byte[] buffer = BlobTestBase.GetRandomBuffer(1024 * 1024); try { await container.CreateAsync(); blobClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); blockBlob.StreamWriteSizeInBytes = 1024 * 1024; blockBlob.StreamMinimumReadSizeInBytes = 1024 * 1024; pageBlob.StreamWriteSizeInBytes = 1024 * 1024; pageBlob.StreamMinimumReadSizeInBytes = 1024 * 1024; using (var bos = await blockBlob.OpenWriteAsync()) { DateTime start = DateTime.Now; for (int i = 0; i < 7; i++) { await bos.WriteAsync(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last write int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { await Task.Delay(msRemaining); } await bos.WriteAsync(buffer, 0, buffer.Length); await bos.CommitAsync(); } using (Stream bis = (await blockBlob.OpenReadAsync())) { DateTime start = DateTime.Now; int total = 0; while (total < 7 * 1024 * 1024) { total += await bis.ReadAsync(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last read int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { await Task.Delay(msRemaining); } while (true) { int count = await bis.ReadAsync(buffer, 0, buffer.Length); total += count; if (count == 0) { break; } } } using (var bos = await pageBlob.OpenWriteAsync(8 * 1024 * 1024)) { DateTime start = DateTime.Now; for (int i = 0; i < 7; i++) { await bos.WriteAsync(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last write int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { await Task.Delay(msRemaining); } await bos.WriteAsync(buffer, 0, buffer.Length); await bos.CommitAsync(); } using (Stream bis = (await pageBlob.OpenReadAsync())) { DateTime start = DateTime.Now; int total = 0; while (total < 7 * 1024 * 1024) { total += await bis.ReadAsync(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last read int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { await Task.Delay(msRemaining); } while (true) { int count = await bis.ReadAsync(buffer, 0, buffer.Length); total += count; if (count == 0) { break; } } } } finally { blobClient.DefaultRequestOptions.MaximumExecutionTime = null; container.DeleteIfExistsAsync().Wait(); } }
public async Task CloudBlobContainerGetBlobReferenceFromServerAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), }; CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); await blockBlob.PutBlockListAsync(new List <string>()); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); await pageBlob.CreateAsync(0); ICloudBlob blob1 = await container.GetBlobReferenceFromServerAsync("bb"); Assert.IsInstanceOfType(blob1, typeof(CloudBlockBlob)); CloudBlockBlob blob1Snapshot = await((CloudBlockBlob)blob1).CreateSnapshotAsync(); await blob1.SetPropertiesAsync(); Uri blob1SnapshotUri = new Uri(blob1Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob1Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); ICloudBlob blob1SnapshotReference = await container.ServiceClient.GetBlobReferenceFromServerAsync(blob1SnapshotUri); AssertAreEqual(blob1Snapshot.Properties, blob1SnapshotReference.Properties); ICloudBlob blob2 = await container.GetBlobReferenceFromServerAsync("pb"); Assert.IsInstanceOfType(blob2, typeof(CloudPageBlob)); CloudPageBlob blob2Snapshot = await((CloudPageBlob)blob2).CreateSnapshotAsync(); await blob2.SetPropertiesAsync(); Uri blob2SnapshotUri = new Uri(blob2Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob2Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); ICloudBlob blob2SnapshotReference = await container.ServiceClient.GetBlobReferenceFromServerAsync(blob2SnapshotUri); AssertAreEqual(blob2Snapshot.Properties, blob2SnapshotReference.Properties); ICloudBlob blob3 = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlob.Uri); Assert.IsInstanceOfType(blob3, typeof(CloudBlockBlob)); ICloudBlob blob4 = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlob.Uri); Assert.IsInstanceOfType(blob4, typeof(CloudPageBlob)); string blockBlobToken = blockBlob.GetSharedAccessSignature(policy); StorageCredentials blockBlobSAS = new StorageCredentials(blockBlobToken); Uri blockBlobSASUri = blockBlobSAS.TransformUri(blockBlob.Uri); string pageBlobToken = pageBlob.GetSharedAccessSignature(policy); StorageCredentials pageBlobSAS = new StorageCredentials(pageBlobToken); Uri pageBlobSASUri = pageBlobSAS.TransformUri(pageBlob.Uri); ICloudBlob blob5 = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSASUri); Assert.IsInstanceOfType(blob5, typeof(CloudBlockBlob)); ICloudBlob blob6 = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSASUri); Assert.IsInstanceOfType(blob6, typeof(CloudPageBlob)); CloudBlobClient client7 = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS); ICloudBlob blob7 = await client7.GetBlobReferenceFromServerAsync(blockBlobSASUri); Assert.IsInstanceOfType(blob7, typeof(CloudBlockBlob)); CloudBlobClient client8 = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS); ICloudBlob blob8 = await client8.GetBlobReferenceFromServerAsync(pageBlobSASUri); Assert.IsInstanceOfType(blob8, typeof(CloudPageBlob)); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudPageBlobCopyTestAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); CloudPageBlob copy = container.GetPageBlobReference("copy"); string copyId = await copy.StartCopyAsync(TestHelper.Defiddler(source)); Assert.AreEqual(BlobType.PageBlob, copy.BlobType); 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))); 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"); await source.FetchAttributesAsync(); 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 = await DownloadTextAsync(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not similar"); await copy.FetchAttributesAsync(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); 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"); await copy.DeleteAsync(); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public void CloudBlobSoftDeleteSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { //Enables a delete retention policy on the blob with 1 day of default retention days container.ServiceClient.EnableSoftDelete(); container.CreateAsync().Wait(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.UploadFromStreamAsync(originalData).Wait(); CloudBlob blob = container.GetBlobReference(BlobName); //create snapshot via api CloudBlob snapshot = blob.SnapshotAsync().Result; //create snapshot via write protection appendBlob.UploadFromStreamAsync(originalData).Wait(); //we should have 2 snapshots 1 regular and 1 deleted: there is no way to get only the deleted snapshots but the below listing will get both snapshot types int blobCount = 0; int deletedSnapshotCount = 0; int snapShotCount = 0; BlobContinuationToken ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.Snapshots | BlobListingDetails.Deleted, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); if (blobItem.IsSnapshot) { snapShotCount++; } if (blobItem.IsDeleted) { Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); deletedSnapshotCount++; } blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 3); Assert.AreEqual(deletedSnapshotCount, 1); Assert.AreEqual(snapShotCount, 2); blobCount = 0; ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.Snapshots, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 2); //Delete Blob and snapshots blob.DeleteAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null).Wait(); Assert.IsFalse(blob.ExistsAsync().Result); Assert.IsFalse(snapshot.ExistsAsync().Result); blobCount = 0; ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsTrue(blobItem.IsDeleted); Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 3); blob.UndeleteAsync().Wait(); blob.FetchAttributesAsync().Wait(); Assert.IsFalse(blob.IsDeleted); Assert.IsNull(blob.Properties.DeletedTime); Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete); blobCount = 0; ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 3); } finally { container.ServiceClient.DisableSoftDelete(); container.DeleteIfExistsAsync().Wait(); } }
public async Task CloudBlobSnapshotAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName); await blockBlob.UploadFromStreamAsync(originalData); CloudBlob blob = container.GetBlobReference(BlobName); await blob.FetchAttributesAsync(); Assert.IsFalse(blob.IsSnapshot); Assert.IsNull(blob.SnapshotTime, "Root blob has SnapshotTime set"); Assert.IsFalse(blob.SnapshotQualifiedUri.Query.Contains("snapshot")); Assert.AreEqual(blob.Uri, blob.SnapshotQualifiedUri); CloudBlob snapshot1 = await blob.SnapshotAsync(); Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsTrue(snapshot1.IsSnapshot); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); Assert.AreEqual(blob.Uri, snapshot1.Uri); Assert.AreNotEqual(blob.SnapshotQualifiedUri, snapshot1.SnapshotQualifiedUri); Assert.AreNotEqual(snapshot1.Uri, snapshot1.SnapshotQualifiedUri); Assert.IsTrue(snapshot1.SnapshotQualifiedUri.Query.Contains("snapshot")); CloudBlob snapshot2 = await blob.SnapshotAsync(); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); await snapshot1.FetchAttributesAsync(); await snapshot2.FetchAttributesAsync(); await blob.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlob snapshot1Clone = new CloudBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value); await snapshot1Clone.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties); // The query parser should not be case sensitive in detecting a snapshot in the query string CloudBlob snapshotParseVerifier = new CloudBlob(new Uri(blob.Uri + "?sNapshOt=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshotParseVerifier.SnapshotTime, "Snapshot parse verifier did not successfully detect the snapshot time"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshotParseVerifier.SnapshotTime.Value); CloudBlob snapshotCopy = container.GetBlobReference("blob2"); await snapshotCopy.StartCopyAsync(TestHelper.Defiddler(snapshot1.Uri)); await WaitForCopyAsync(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); using (Stream snapshotStream = (await snapshot1.OpenReadAsync())) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } await blockBlob.PutBlockListAsync(new List <string>()); await blob.FetchAttributesAsync(); using (Stream snapshotStream = (await snapshot1.OpenReadAsync())) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null); List <IListBlobItem> blobs = resultSegment.Results.ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshot1, (CloudBlob)blobs[0]); AssertAreEqual(snapshot2, (CloudBlob)blobs[1]); AssertAreEqual(blob, (CloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (CloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().Wait(); } }
public void CloudBlobSoftDeleteNoSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { //Enables a delete retention policy on the blob with 1 day of default retention days container.ServiceClient.EnableSoftDelete(); container.CreateAsync().Wait(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplaceAsync().Wait(); appendBlob.AppendBlockAsync(originalData, null).Wait(); CloudBlob blob = container.GetBlobReference(BlobName); Assert.IsTrue(blob.ExistsAsync().Result); Assert.IsFalse(blob.IsDeleted); blob.DeleteAsync().Wait(); Assert.IsFalse(blob.ExistsAsync().Result); int blobCount = 0; BlobContinuationToken ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsTrue(blobItem.IsDeleted); Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 1); blob.UndeleteAsync().Wait(); blob.FetchAttributesAsync().Wait(); Assert.IsFalse(blob.IsDeleted); Assert.IsNull(blob.Properties.DeletedTime); Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete); blobCount = 0; ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 1); } finally { container.ServiceClient.DisableSoftDelete(); container.DeleteIfExistsAsync().Wait(); } }
public async Task PageBlobWriteStreamFlushTestAsync() { byte[] buffer = GetRandomBuffer(512); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamWriteSizeInBytes = 1024; using (MemoryStream wholeBlob = new MemoryStream()) { BlobRequestOptions options = new BlobRequestOptions() { StoreBlobContentMD5 = true }; OperationContext opContext = new OperationContext(); using (ICloudBlobStream blobStream = await blob.OpenWriteAsync(4 * 512, null, options, opContext)) { for (int i = 0; i < 3; i++) { await blobStream.WriteAsync(buffer.AsBuffer()); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); } Assert.AreEqual(2, opContext.RequestResults.Count); await blobStream.FlushAsync(); Assert.AreEqual(3, opContext.RequestResults.Count); await blobStream.FlushAsync(); Assert.AreEqual(3, opContext.RequestResults.Count); await blobStream.WriteAsync(buffer.AsBuffer()); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(3, opContext.RequestResults.Count); await blobStream.CommitAsync(); Assert.AreEqual(5, opContext.RequestResults.Count); } Assert.AreEqual(5, opContext.RequestResults.Count); using (MemoryOutputStream downloadedBlob = new MemoryOutputStream()) { await blob.DownloadToStreamAsync(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob.UnderlyingStream); } } } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public void CloudBlobSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { container.CreateAsync().Wait(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplaceAsync().Wait(); appendBlob.AppendBlockAsync(originalData, null).Wait(); CloudBlob blob = container.GetBlobReference(BlobName); blob.FetchAttributesAsync().Wait(); CloudBlob snapshot1 = blob.SnapshotAsync().Result; Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsTrue(snapshot1.IsSnapshot); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); Assert.AreEqual(blob.Uri, snapshot1.Uri); Assert.AreNotEqual(blob.SnapshotQualifiedUri, snapshot1.SnapshotQualifiedUri); Assert.AreNotEqual(snapshot1.Uri, snapshot1.SnapshotQualifiedUri); Assert.IsTrue(snapshot1.SnapshotQualifiedUri.Query.Contains("snapshot")); CloudBlob snapshot2 = blob.SnapshotAsync().Result; Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); snapshot1.FetchAttributesAsync().Wait(); snapshot2.FetchAttributesAsync().Wait(); blob.FetchAttributesAsync().Wait(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlob snapshot1Clone = new CloudBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value); snapshot1Clone.FetchAttributesAsync().Wait(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties); CloudBlob snapshotCopy = container.GetBlobReference("blob2"); snapshotCopy.StartCopyAsync(snapshot1.Uri, null, null, null, null).Wait(); WaitForCopy(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); using (Stream snapshotStream = snapshot1.OpenReadAsync().Result) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } //overwriting will create another snapshot appendBlob.CreateOrReplaceAsync().Wait(); blob.FetchAttributesAsync().Wait(); using (Stream snapshotStream = snapshot1.OpenReadAsync().Result) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } List <IListBlobItem> blobs = container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null) .Result .Results .ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshot1, (CloudBlob)blobs[0]); AssertAreEqual(snapshot2, (CloudBlob)blobs[1]); AssertAreEqual(blob, (CloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (CloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().Wait(); } }
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(); } }
public async Task DisableContentMD5ValidationTestAsync() { byte[] buffer = new byte[1024]; Random random = new Random(); random.NextBytes(buffer); BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions() { DisableContentMD5Validation = true, StoreBlobContentMD5 = true, }; BlobRequestOptions optionsWithMD5 = new BlobRequestOptions() { DisableContentMD5Validation = false, StoreBlobContentMD5 = true, }; CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); using (Stream stream = new NonSeekableMemoryStream(buffer)) { await blockBlob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null); } using (Stream stream = new MemoryStream()) { await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, null); await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null); using (IRandomAccessStreamWithContentType blobStream = await blockBlob.OpenReadAsync(null, optionsWithMD5, null)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } using (IRandomAccessStreamWithContentType blobStream = await blockBlob.OpenReadAsync(null, optionsWithNoMD5, null)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } blockBlob.Properties.ContentMD5 = "MDAwMDAwMDA="; await blockBlob.SetPropertiesAsync(); OperationContext opContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, opContext), opContext, "Downloading a blob with invalid MD5 should fail", HttpStatusCode.OK); await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null); using (IRandomAccessStreamWithContentType blobStream = await blockBlob.OpenReadAsync(null, optionsWithMD5, null)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); TestHelper.ExpectedException <IOException>( () => { int read; do { read = blobStreamForRead.Read(buffer, 0, buffer.Length); }while (read > 0); }, "Downloading a blob with invalid MD5 should fail"); } using (IRandomAccessStreamWithContentType blobStream = await blockBlob.OpenReadAsync(null, optionsWithNoMD5, null)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } } CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); using (Stream stream = new MemoryStream(buffer)) { await pageBlob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null); } using (Stream stream = new MemoryStream()) { await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, null); await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null); using (IRandomAccessStreamWithContentType blobStream = await pageBlob.OpenReadAsync(null, optionsWithMD5, null)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } using (IRandomAccessStreamWithContentType blobStream = await pageBlob.OpenReadAsync(null, optionsWithNoMD5, null)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } pageBlob.Properties.ContentMD5 = "MDAwMDAwMDA="; await pageBlob.SetPropertiesAsync(); OperationContext opContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, opContext), opContext, "Downloading a blob with invalid MD5 should fail", HttpStatusCode.OK); await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null); using (IRandomAccessStreamWithContentType blobStream = await pageBlob.OpenReadAsync(null, optionsWithMD5, null)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); TestHelper.ExpectedException <IOException>( () => { int read; do { read = blobStreamForRead.Read(buffer, 0, buffer.Length); }while (read > 0); }, "Downloading a blob with invalid MD5 should fail"); } using (IRandomAccessStreamWithContentType blobStream = await pageBlob.OpenReadAsync(null, optionsWithNoMD5, null)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } } } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
private async Task IncrementalCopyAsyncImpl(int overload) { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob source = container.GetPageBlobReference("source"); await source.CreateAsync(1024); string data = new string('a', 512); await UploadTextAsync(source, data, Encoding.UTF8); CloudPageBlob sourceSnapshot = await source.CreateSnapshotAsync(null, null, null, null); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write, }; string sasToken = sourceSnapshot.GetSharedAccessSignature(policy); StorageCredentials blobSAS = new StorageCredentials(sasToken); Uri sourceSnapshotUri = blobSAS.TransformUri(TestHelper.Defiddler(sourceSnapshot).SnapshotQualifiedUri); StorageCredentials accountSAS = new StorageCredentials(sasToken); CloudStorageAccount accountWithSAS = new CloudStorageAccount(accountSAS, source.ServiceClient.StorageUri, null, null, null); CloudPageBlob snapshotWithSas = await accountWithSAS.CreateCloudBlobClient().GetBlobReferenceFromServerAsync(sourceSnapshot.SnapshotQualifiedUri) as CloudPageBlob; CloudPageBlob copy = container.GetPageBlobReference("copy"); string copyId = null; if (overload == 0) { #if !FACADE_NETCORE copyId = await copy.StartIncrementalCopyAsync(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); copyId = await copy.StartIncrementalCopyAsync(new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null)); #endif } else if (overload == 1) { #if !FACADE_NETCORE CloudPageBlob blob = new CloudPageBlob(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); #endif copyId = await copy.StartIncrementalCopyAsync(blob); } else if (overload == 2) { #if !FACADE_NETCORE CloudPageBlob blob = new CloudPageBlob(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); #endif copyId = await copy.StartIncrementalCopyAsync(blob, null, null, null, CancellationToken.None); } else { #if !FACADE_NETCORE copyId = await copy.StartIncrementalCopyAsync(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri), null, null, null, CancellationToken.None); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); copyId = await copy.StartIncrementalCopyAsync(blob, null, null, null, CancellationToken.None); #endif } await WaitForCopyAsync(copy); Assert.AreEqual(BlobType.PageBlob, copy.BlobType); 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.Properties.IsIncrementalCopy); Assert.IsTrue(copy.CopyState.DestinationSnapshotTime.HasValue); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task StoreBlobContentMD5TestAsync() { BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions() { StoreBlobContentMD5 = false, }; BlobRequestOptions optionsWithMD5 = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); ICloudBlob blob = container.GetBlockBlobReference("blob1"); using (Stream stream = new NonSeekableMemoryStream()) { await blob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null); } await blob.FetchAttributesAsync(); Assert.IsNotNull(blob.Properties.ContentMD5); blob = container.GetBlockBlobReference("blob2"); using (Stream stream = new NonSeekableMemoryStream()) { await blob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithNoMD5, null); } await blob.FetchAttributesAsync(); Assert.IsNull(blob.Properties.ContentMD5); blob = container.GetBlockBlobReference("blob3"); using (Stream stream = new NonSeekableMemoryStream()) { await blob.UploadFromStreamAsync(stream.AsInputStream()); } await blob.FetchAttributesAsync(); Assert.IsNotNull(blob.Properties.ContentMD5); blob = container.GetPageBlobReference("blob4"); using (Stream stream = new MemoryStream()) { await blob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null); } await blob.FetchAttributesAsync(); Assert.IsNotNull(blob.Properties.ContentMD5); blob = container.GetPageBlobReference("blob5"); using (Stream stream = new MemoryStream()) { await blob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithNoMD5, null); } await blob.FetchAttributesAsync(); Assert.IsNull(blob.Properties.ContentMD5); blob = container.GetPageBlobReference("blob6"); using (Stream stream = new MemoryStream()) { await blob.UploadFromStreamAsync(stream.AsInputStream()); } await blob.FetchAttributesAsync(); Assert.IsNull(blob.Properties.ContentMD5); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudPageBlobCopyFromSnapshotTestAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); CloudPageBlob snapshot = await source.CreateSnapshotAsync(); //Modify source string newData = new string('b', 512); source.Metadata["Test"] = "newvalue"; await source.SetMetadataAsync(); source.Properties.ContentMD5 = null; await UploadTextAsync(source, newData, Encoding.UTF8); Assert.AreEqual(newData, await DownloadTextAsync(source, Encoding.UTF8), "Source is modified correctly"); Assert.AreEqual(data, await DownloadTextAsync(snapshot, Encoding.UTF8), "Modifying source blob should not modify snapshot"); await source.FetchAttributesAsync(); await snapshot.FetchAttributesAsync(); Assert.AreNotEqual(source.Metadata["Test"], snapshot.Metadata["Test"], "Source and snapshot metadata should be independent"); CloudPageBlob copy = container.GetPageBlobReference("copy"); await copy.StartCopyAsync(TestHelper.Defiddler(snapshot)); await WaitForCopyAsync(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(data, await DownloadTextAsync(copy, Encoding.UTF8), "Data inside copy of blob not similar"); await copy.FetchAttributesAsync(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = snapshot.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); 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"); await copy.DeleteAsync(); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudPageBlobGetPageRangesAsync() { byte[] buffer = GetRandomBuffer(1024); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); await blob.CreateAsync(4 * 1024); using (MemoryStream memoryStream = new MemoryStream(buffer)) { await blob.WritePagesAsync(memoryStream.AsInputStream(), 512, null); } using (MemoryStream memoryStream = new MemoryStream(buffer)) { await blob.WritePagesAsync(memoryStream.AsInputStream(), 3 * 1024, null); } await blob.ClearPagesAsync(1024, 1024); await blob.ClearPagesAsync(0, 512); IEnumerable <PageRange> pageRanges = await blob.GetPageRangesAsync(); List <string> expectedPageRanges = new List <string>() { new PageRange(512, 1023).ToString(), new PageRange(3 * 1024, 4 * 1024 - 1).ToString(), }; foreach (PageRange pageRange in pageRanges) { Assert.IsTrue(expectedPageRanges.Remove(pageRange.ToString())); } Assert.AreEqual(0, expectedPageRanges.Count); pageRanges = await blob.GetPageRangesAsync(1024, 1024, null, null, null); Assert.AreEqual(0, pageRanges.Count()); pageRanges = await blob.GetPageRangesAsync(512, 3 * 1024, null, null, null); expectedPageRanges = new List <string>() { new PageRange(512, 1023).ToString(), new PageRange(3 * 1024, 7 * 512 - 1).ToString(), }; foreach (PageRange pageRange in pageRanges) { Assert.IsTrue(expectedPageRanges.Remove(pageRange.ToString())); } Assert.AreEqual(0, expectedPageRanges.Count); OperationContext opContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await blob.GetPageRangesAsync(1024, null, null, null, opContext), opContext, "Get Page Ranges with an offset but no count should fail", HttpStatusCode.Unused); Assert.IsInstanceOfType(opContext.LastResult.Exception.InnerException, typeof(ArgumentNullException)); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudBlobClientMaximumExecutionTimeoutAsync() { CloudBlobClient blobClient = GenerateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(Guid.NewGuid().ToString("N")); byte[] buffer = BlobTestBase.GetRandomBuffer(80 * 1024 * 1024); try { await container.CreateAsync(); blobClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(5); blobClient.DefaultRequestOptions.SingleBlobUploadThresholdInBytes = 2 * 1024 * 1024; CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); blockBlob.StreamWriteSizeInBytes = 1 * 1024 * 1024; using (MemoryStream ms = new MemoryStream(buffer)) { try { await blockBlob.UploadFromStreamAsync(ms); Assert.Fail(); } catch (AggregateException ex) { #if !FACADE_NETCORE Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).ExceptionInfo.Message); #else Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).Exception.Message); #endif } catch (TaskCanceledException) { } } CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); pageBlob.StreamWriteSizeInBytes = 1 * 1024 * 1024; using (MemoryStream ms = new MemoryStream(buffer)) { try { await pageBlob.UploadFromStreamAsync(ms); Assert.Fail(); } catch (AggregateException ex) { #if !FACADE_NETCORE Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).ExceptionInfo.Message); #else Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).Exception.Message); #endif } catch (TaskCanceledException) { } } } finally { blobClient.DefaultRequestOptions.MaximumExecutionTime = null; container.DeleteIfExistsAsync().Wait(); } }
public async Task CloudPageBlobConditionalAccessAsync() { OperationContext operationContext = new OperationContext(); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); await blob.CreateAsync(1024); await blob.FetchAttributesAsync(); string currentETag = blob.Properties.ETag; DateTimeOffset currentModifiedTime = blob.Properties.LastModified.Value; // ETag conditional tests blob.Metadata["ETagConditionalName"] = "ETagConditionalValue"; await blob.SetMetadataAsync(AccessCondition.GenerateIfMatchCondition(currentETag), null, null); await blob.FetchAttributesAsync(); string newETag = blob.Properties.ETag; Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata"); blob.Metadata["ETagConditionalName"] = "ETagConditionalValue2"; await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfNoneMatchCondition(newETag), null, operationContext), operationContext, "If none match on conditional test should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); string invalidETag = "\"0x10101010\""; await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfMatchCondition(invalidETag), null, operationContext), operationContext, "Invalid ETag on conditional test should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); currentETag = blob.Properties.ETag; await blob.SetMetadataAsync(AccessCondition.GenerateIfNoneMatchCondition(invalidETag), null, null); await blob.FetchAttributesAsync(); newETag = blob.Properties.ETag; // LastModifiedTime tests currentModifiedTime = blob.Properties.LastModified.Value; blob.Metadata["DateConditionalName"] = "DateConditionalValue"; await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(currentModifiedTime), null, operationContext), operationContext, "IfModifiedSince conditional on current modified time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); DateTimeOffset pastTime = currentModifiedTime.Subtract(TimeSpan.FromMinutes(5)); await blob.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null); pastTime = currentModifiedTime.Subtract(TimeSpan.FromHours(5)); await blob.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null); pastTime = currentModifiedTime.Subtract(TimeSpan.FromDays(5)); await blob.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null); currentModifiedTime = blob.Properties.LastModified.Value; pastTime = currentModifiedTime.Subtract(TimeSpan.FromMinutes(5)); await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfNotModifiedSinceCondition(pastTime), null, operationContext), operationContext, "IfNotModifiedSince conditional on past time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); pastTime = currentModifiedTime.Subtract(TimeSpan.FromHours(5)); await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfNotModifiedSinceCondition(pastTime), null, operationContext), operationContext, "IfNotModifiedSince conditional on past time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); pastTime = currentModifiedTime.Subtract(TimeSpan.FromDays(5)); await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfNotModifiedSinceCondition(pastTime), null, operationContext), operationContext, "IfNotModifiedSince conditional on past time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); blob.Metadata["DateConditionalName"] = "DateConditionalValue2"; currentETag = blob.Properties.ETag; await blob.SetMetadataAsync(AccessCondition.GenerateIfNotModifiedSinceCondition(currentModifiedTime), null, null); await blob.FetchAttributesAsync(); newETag = blob.Properties.ETag; Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata"); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudBlobContainerGetBlobReferenceFromServerAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), }; CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); await blockBlob.PutBlockListAsync(new List <string>()); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); await pageBlob.CreateAsync(0); CloudAppendBlob appendBlob = container.GetAppendBlobReference("ab"); await appendBlob.CreateOrReplaceAsync(); CloudBlobClient client; ICloudBlob blob; blob = await container.GetBlobReferenceFromServerAsync("bb"); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); CloudBlockBlob blockBlobSnapshot = await((CloudBlockBlob)blob).CreateSnapshotAsync(); await blob.SetPropertiesAsync(); Uri blockBlobSnapshotUri = new Uri(blockBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + blockBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSnapshotUri); AssertAreEqual(blockBlobSnapshot.Properties, blob.Properties); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlobSnapshot.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.GetBlobReferenceFromServerAsync("pb"); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); CloudPageBlob pageBlobSnapshot = await((CloudPageBlob)blob).CreateSnapshotAsync(); await blob.SetPropertiesAsync(); Uri pageBlobSnapshotUri = new Uri(pageBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + pageBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSnapshotUri); AssertAreEqual(pageBlobSnapshot.Properties, blob.Properties); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlobSnapshot.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.GetBlobReferenceFromServerAsync("ab"); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri)); CloudAppendBlob appendBlobSnapshot = await((CloudAppendBlob)blob).CreateSnapshotAsync(); await blob.SetPropertiesAsync(); Uri appendBlobSnapshotUri = new Uri(appendBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + appendBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSnapshotUri); AssertAreEqual(appendBlobSnapshot.Properties, blob.Properties); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlobSnapshot.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlob.Uri); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlob.Uri); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlob.Uri); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlob.StorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlob.StorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlob.StorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri)); string blockBlobToken = blockBlob.GetSharedAccessSignature(policy); StorageCredentials blockBlobSAS = new StorageCredentials(blockBlobToken); Uri blockBlobSASUri = blockBlobSAS.TransformUri(blockBlob.Uri); StorageUri blockBlobSASStorageUri = blockBlobSAS.TransformUri(blockBlob.StorageUri); string appendBlobToken = appendBlob.GetSharedAccessSignature(policy); StorageCredentials appendBlobSAS = new StorageCredentials(appendBlobToken); Uri appendBlobSASUri = appendBlobSAS.TransformUri(appendBlob.Uri); StorageUri appendBlobSASStorageUri = appendBlobSAS.TransformUri(appendBlob.StorageUri); string pageBlobToken = pageBlob.GetSharedAccessSignature(policy); StorageCredentials pageBlobSAS = new StorageCredentials(pageBlobToken); Uri pageBlobSASUri = pageBlobSAS.TransformUri(pageBlob.Uri); StorageUri pageBlobSASStorageUri = pageBlobSAS.TransformUri(pageBlob.StorageUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri)); client = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(blockBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); client = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(pageBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); client = new CloudBlobClient(container.ServiceClient.BaseUri, appendBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(appendBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); client = new CloudBlobClient(container.ServiceClient.StorageUri, blockBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(blockBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); client = new CloudBlobClient(container.ServiceClient.StorageUri, pageBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(pageBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); client = new CloudBlobClient(container.ServiceClient.StorageUri, appendBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(appendBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri)); } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task PageBlobWriteStreamBasicTestAsync() { byte[] buffer = GetRandomBuffer(6 * 512); CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash(); CloudBlobContainer container = GetRandomContainerReference(); container.ServiceClient.ParallelOperationThreadCount = 2; try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamWriteSizeInBytes = 8 * 512; using (MemoryStream wholeBlob = new MemoryStream()) { BlobRequestOptions options = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; using (IOutputStream writeStream = await blob.OpenWriteAsync(buffer.Length * 3, null, options, null)) { Stream blobStream = writeStream.AsStreamForWrite(); for (int i = 0; i < 3; i++) { await blobStream.WriteAsync(buffer, 0, buffer.Length); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); hasher.Append(buffer.AsBuffer()); } } string md5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset()); await blob.FetchAttributesAsync(); Assert.AreEqual(md5, blob.Properties.ContentMD5); using (MemoryOutputStream downloadedBlob = new MemoryOutputStream()) { await blob.DownloadToStreamAsync(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob.UnderlyingStream); } await TestHelper.ExpectedExceptionAsync <ArgumentException>( async() => await blob.OpenWriteAsync(null, null, options, null), "OpenWrite with StoreBlobContentMD5 on an existing page blob should fail"); using (IOutputStream writeStream = await blob.OpenWriteAsync(null)) { Stream blobStream = writeStream.AsStreamForWrite(); blobStream.Seek(buffer.Length / 2, SeekOrigin.Begin); wholeBlob.Seek(buffer.Length / 2, SeekOrigin.Begin); for (int i = 0; i < 2; i++) { blobStream.Write(buffer, 0, buffer.Length); wholeBlob.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } wholeBlob.Seek(0, SeekOrigin.End); } await blob.FetchAttributesAsync(); Assert.AreEqual(md5, blob.Properties.ContentMD5); using (MemoryOutputStream downloadedBlob = new MemoryOutputStream()) { options.DisableContentMD5Validation = true; await blob.DownloadToStreamAsync(downloadedBlob, null, options, null); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob.UnderlyingStream); } } } finally { container.DeleteAsync().AsTask().Wait(); } }
public async Task AppendBlobReadLockToETagTestAsync() { byte[] outBuffer = new byte[1 * 1024 * 1024]; byte[] buffer = GetRandomBuffer(2 * outBuffer.Length); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudAppendBlob blob = container.GetAppendBlobReference("blob1"); await blob.CreateOrReplaceAsync(); blob.StreamMinimumReadSizeInBytes = outBuffer.Length; using (MemoryStream wholeBlob = new MemoryStream(buffer)) { await blob.AppendBlockAsync(wholeBlob); } OperationContext opContext = new OperationContext(); using (var blobStream = await blob.OpenReadAsync(null, null, opContext)) { Stream blobStreamForRead = blobStream; await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length); await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } opContext = new OperationContext(); using (var blobStream = await blob.OpenReadAsync(null, null, opContext)) { Stream blobStreamForRead = blobStream; long length = blobStreamForRead.Length; await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } opContext = new OperationContext(); AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1))); await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blob.OpenReadAsync(accessCondition, null, opContext), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task AppendBlobWriteStreamOpenWithAccessConditionAsync() { CloudBlobContainer container = GetRandomContainerReference(); await container.CreateAsync(); try { OperationContext context = new OperationContext(); CloudAppendBlob existingBlob = container.GetAppendBlobReference("blob"); await existingBlob.CreateOrReplaceAsync(); CloudAppendBlob blob = container.GetAppendBlobReference("blob2"); AccessCondition accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await blob.OpenWriteAsync(true, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); blob = container.GetAppendBlobReference("blob3"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(existingBlob.Properties.ETag); var blobStream = await blob.OpenWriteAsync(true, accessCondition, null, context); blobStream.Dispose(); blob = container.GetAppendBlobReference("blob4"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); blobStream = await blob.OpenWriteAsync(true, accessCondition, null, context); blobStream.Dispose(); blob = container.GetAppendBlobReference("blob5"); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); blobStream = await blob.OpenWriteAsync(true, accessCondition, null, context); blobStream.Dispose(); blob = container.GetAppendBlobReference("blob6"); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); blobStream = await blob.OpenWriteAsync(true, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(true, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfMatchCondition(blob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(true, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(blob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(true, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(existingBlob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(true, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(true, accessCondition, null, context), context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.Conflict); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); blobStream = await existingBlob.OpenWriteAsync(true, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(true, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); blobStream = await existingBlob.OpenWriteAsync(true, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(true, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); } finally { container.DeleteAsync().Wait(); } }