public static void UploadTextTask(CloudBlob blob, string text, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null) { byte[] textAsBytes = encoding.GetBytes(text); using (MemoryStream stream = new MemoryStream()) { stream.Write(textAsBytes, 0, textAsBytes.Length); if (blob.BlobType == BlobType.PageBlob) { int lastPageSize = (int)(stream.Length % 512); if (lastPageSize != 0) { byte[] padding = new byte[512 - lastPageSize]; stream.Write(padding, 0, padding.Length); } } stream.Seek(0, SeekOrigin.Begin); blob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2; try { if (blob.BlobType == BlobType.AppendBlob) { CloudAppendBlob blob1 = blob as CloudAppendBlob; blob1.CreateOrReplaceAsync().Wait(); blob1.AppendBlock(stream, null); } else if (blob.BlobType == BlobType.PageBlob) { CloudPageBlob pageBlob = blob as CloudPageBlob; pageBlob.UploadFromStreamAsync(stream, accessCondition, options, operationContext).Wait(); } else { CloudBlockBlob blockBlob = blob as CloudBlockBlob; blockBlob.UploadFromStreamAsync(stream, accessCondition, options, operationContext).Wait(); } } catch (AggregateException ex) { if (ex.InnerException != null) { throw ex.InnerException; } throw; } } }
private static void TestAccess(string sasToken, SharedAccessBlobPermissions permissions, SharedAccessBlobHeaders headers, CloudBlobContainer container, CloudBlob blob) { CloudBlob SASblob; StorageCredentials credentials = string.IsNullOrEmpty(sasToken) ? new StorageCredentials() : new StorageCredentials(sasToken); if (container != null) { container = new CloudBlobContainer(credentials.TransformUri(container.Uri)); if (blob.BlobType == BlobType.BlockBlob) { SASblob = container.GetBlockBlobReference(blob.Name); } else if (blob.BlobType == BlobType.PageBlob) { SASblob = container.GetPageBlobReference(blob.Name); } else { SASblob = container.GetAppendBlobReference(blob.Name); } } else { if (blob.BlobType == BlobType.BlockBlob) { SASblob = new CloudBlockBlob(credentials.TransformUri(blob.Uri)); } else if (blob.BlobType == BlobType.PageBlob) { SASblob = new CloudPageBlob(credentials.TransformUri(blob.Uri)); } else { SASblob = new CloudAppendBlob(credentials.TransformUri(blob.Uri)); } } HttpStatusCode failureCode = sasToken == null ? HttpStatusCode.NotFound : HttpStatusCode.Forbidden; // We want to ensure that 'create', 'add', and 'write' permissions all allow for correct writing of blobs, as is reasonable. if (((permissions & SharedAccessBlobPermissions.Create) == SharedAccessBlobPermissions.Create) || ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write)) { if (blob.BlobType == BlobType.PageBlob) { CloudPageBlob SASpageBlob = (CloudPageBlob)SASblob; SASpageBlob.Create(512); CloudPageBlob pageBlob = (CloudPageBlob)blob; byte[] buffer = new byte[512]; buffer[0] = 2; // random data if (((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write)) { SASpageBlob.UploadFromByteArray(buffer, 0, 512); } else { TestHelper.ExpectedException( () => SASpageBlob.UploadFromByteArray(buffer, 0, 512), "pageBlob SAS token without Write perms should not allow for writing/adding", failureCode); pageBlob.UploadFromByteArray(buffer, 0, 512); } } else if (blob.BlobType == BlobType.BlockBlob) { if ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write) { UploadText(SASblob, "blob", Encoding.UTF8); } else { TestHelper.ExpectedException( () => UploadText(SASblob, "blob", Encoding.UTF8), "Block blob SAS token without Write or perms should not allow for writing", failureCode); UploadText(blob, "blob", Encoding.UTF8); } } else // append blob { // If the sas token contains Feb 2012, append won't be accepted if (sasToken.Contains(Constants.VersionConstants.February2012)) { UploadText(blob, "blob", Encoding.UTF8); } else { CloudAppendBlob SASAppendBlob = SASblob as CloudAppendBlob; SASAppendBlob.CreateOrReplace(); byte[] textAsBytes = Encoding.UTF8.GetBytes("blob"); using (MemoryStream stream = new MemoryStream()) { stream.Write(textAsBytes, 0, textAsBytes.Length); stream.Seek(0, SeekOrigin.Begin); if (((permissions & SharedAccessBlobPermissions.Add) == SharedAccessBlobPermissions.Add) || ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write)) { SASAppendBlob.AppendBlock(stream, null); } else { TestHelper.ExpectedException( () => SASAppendBlob.AppendBlock(stream, null), "Append blob SAS token without Write or Add perms should not allow for writing/adding", failureCode); stream.Seek(0, SeekOrigin.Begin); ((CloudAppendBlob)blob).AppendBlock(stream, null); } } } } } else { TestHelper.ExpectedException( () => UploadText(SASblob, "blob", Encoding.UTF8), "UploadText SAS does not allow for writing/adding", ((blob.BlobType == BlobType.AppendBlob) && (sasToken != null) && (sasToken.Contains(Constants.VersionConstants.February2012))) ? HttpStatusCode.BadRequest : failureCode); UploadText(blob, "blob", Encoding.UTF8); } if (container != null) { if ((permissions & SharedAccessBlobPermissions.List) == SharedAccessBlobPermissions.List) { container.ListBlobs().ToArray(); } else { TestHelper.ExpectedException( () => container.ListBlobs().ToArray(), "List blobs while SAS does not allow for listing", failureCode); } } // need to have written to the blob to read from it. if (((permissions & SharedAccessBlobPermissions.Read) == SharedAccessBlobPermissions.Read)) { SASblob.FetchAttributes(); // Test headers if (headers != null) { if (headers.CacheControl != null) { Assert.AreEqual(headers.CacheControl, SASblob.Properties.CacheControl); } if (headers.ContentDisposition != null) { Assert.AreEqual(headers.ContentDisposition, SASblob.Properties.ContentDisposition); } if (headers.ContentEncoding != null) { Assert.AreEqual(headers.ContentEncoding, SASblob.Properties.ContentEncoding); } if (headers.ContentLanguage != null) { Assert.AreEqual(headers.ContentLanguage, SASblob.Properties.ContentLanguage); } if (headers.ContentType != null) { Assert.AreEqual(headers.ContentType, SASblob.Properties.ContentType); } } } else { TestHelper.ExpectedException( () => SASblob.FetchAttributes(), "Fetch blob attributes while SAS does not allow for reading", failureCode); } if ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write) { SASblob.SetMetadata(); } else { TestHelper.ExpectedException( () => SASblob.SetMetadata(), "Set blob metadata while SAS does not allow for writing", failureCode); } if ((permissions & SharedAccessBlobPermissions.Delete) == SharedAccessBlobPermissions.Delete) { SASblob.Delete(); } else { TestHelper.ExpectedException( () => SASblob.Delete(), "Delete blob while SAS does not allow for deleting", failureCode); } }
public void CloudBlobSnapshot() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplace(); appendBlob.AppendBlock(originalData, null); CloudBlob blob = container.GetBlobReference(BlobName); blob.FetchAttributes(); 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 = blob.Snapshot(); 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.Snapshot(); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); snapshot1.FetchAttributes(); snapshot2.FetchAttributes(); blob.FetchAttributes(); AssertAreEqual(snapshot1.Properties, blob.Properties, false); 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.FetchAttributes(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties, false); CloudBlob snapshotCopy = container.GetBlobReference("blob2"); snapshotCopy.StartCopy(TestHelper.Defiddler(snapshot1.Uri)); WaitForCopy(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); using (Stream snapshotStream = snapshot1.OpenRead()) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } appendBlob.CreateOrReplace(); blob.FetchAttributes(); using (Stream snapshotStream = snapshot1.OpenRead()) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } List <IListBlobItem> blobs = container.ListBlobs(null, true, BlobListingDetails.All, null, null).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.DeleteIfExists(); } }
public void CloudBlobSnapshotAPM() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplace(); appendBlob.AppendBlock(originalData, null); CloudBlob blob = container.GetBlobReference(BlobName); blob.FetchAttributes(); IAsyncResult result; using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { result = blob.BeginSnapshot(ar => waitHandle.Set(), null); waitHandle.WaitOne(); CloudBlob snapshot1 = blob.EndSnapshot(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")); result = blob.BeginSnapshot(ar => waitHandle.Set(), null); waitHandle.WaitOne(); CloudBlob snapshot2 = blob.EndSnapshot(result); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); snapshot1.FetchAttributes(); snapshot2.FetchAttributes(); blob.FetchAttributes(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlob snapshotCopy = container.GetBlobReference("blob2"); result = snapshotCopy.BeginStartCopy(snapshot1.Uri, null, null, null, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); snapshotCopy.EndStartCopy(result); WaitForCopy(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); result = snapshot1.BeginOpenRead(ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream snapshotStream = snapshot1.EndOpenRead(result)) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } result = appendBlob.BeginCreateOrReplace(ar => waitHandle.Set(), null); waitHandle.WaitOne(); appendBlob.EndCreateOrReplace(result); result = blob.BeginFetchAttributes(ar => waitHandle.Set(), null); waitHandle.WaitOne(); blob.EndFetchAttributes(result); result = snapshot1.BeginOpenRead(ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream snapshotStream = snapshot1.EndOpenRead(result)) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } List <IListBlobItem> blobs = container.ListBlobs(null, true, BlobListingDetails.All, null, null).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.DeleteIfExists(); } }
public void CloudBlobSoftDeleteNoSnapshotAPM() { CloudBlobContainer container = GetRandomContainerReference(); try { //Enables a delete retention policy on the blob with 1 day of default retention days container.ServiceClient.EnableSoftDelete(); container.Create(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplace(); appendBlob.AppendBlock(originalData, null); CloudBlob blob = container.GetBlobReference(BlobName); Assert.IsFalse(blob.IsDeleted); IAsyncResult result; using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { result = blob.BeginDelete(DeleteSnapshotsOption.None, null, null, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); blob.EndDelete(result); } Assert.IsFalse(blob.Exists()); int blobCount = 0; foreach (IListBlobItem item in container.ListBlobs(null, true, BlobListingDetails.All)) { 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++; } Assert.AreEqual(blobCount, 1); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { result = blob.BeginUndelete(ar => waitHandle.Set(), null); waitHandle.WaitOne(); blob.EndUndelete(result); } blob.FetchAttributes(); Assert.IsFalse(blob.IsDeleted); Assert.IsNull(blob.Properties.DeletedTime); Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete); blobCount = 0; foreach (IListBlobItem item in container.ListBlobs(null, true, BlobListingDetails.All)) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } Assert.AreEqual(blobCount, 1); } finally { container.ServiceClient.DisableSoftDelete(); container.DeleteIfExists(); } }
public void CloudBlobSoftDeleteNoSnapshot() { CloudBlobContainer container = GetRandomContainerReference(); try { //Enables a delete retention policy on the blob with 1 day of default retention days container.ServiceClient.EnableSoftDelete(); Thread.Sleep(15000); container.Create(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplace(); appendBlob.AppendBlock(originalData, null); CloudBlob blob = container.GetBlobReference(BlobName); Assert.IsTrue(blob.Exists()); Assert.IsFalse(blob.IsDeleted); blob.Delete(); Assert.IsFalse(blob.Exists()); int blobCount = 0; foreach (IListBlobItem item in container.ListBlobs(null, true, BlobListingDetails.All)) { 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++; } Assert.AreEqual(blobCount, 1); blob.Undelete(); blob.FetchAttributes(); Assert.IsFalse(blob.IsDeleted); Assert.IsNull(blob.Properties.DeletedTime); Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete); blobCount = 0; foreach (IListBlobItem item in container.ListBlobs(null, true, BlobListingDetails.All)) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } Assert.AreEqual(blobCount, 1); } finally { container.ServiceClient.DisableSoftDelete(); container.DeleteIfExists(); } }