public static CloudBlob GetCorrespondingTypeBlobReference(CloudBlob blob) { CloudBlob targetBlob; switch (blob.Properties.BlobType) { case BlobType.BlockBlob: targetBlob = new CloudBlockBlob(blob.SnapshotQualifiedUri, blob.ServiceClient.Credentials); break; case BlobType.PageBlob: targetBlob = new CloudPageBlob(blob.SnapshotQualifiedUri, blob.ServiceClient.Credentials); break; case BlobType.AppendBlob: targetBlob = new CloudAppendBlob(blob.SnapshotQualifiedUri, blob.ServiceClient.Credentials); break; default: throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, Resources.InvalidBlobType, blob.Properties.BlobType, blob.Name)); } targetBlob.FetchAttributes(); return(targetBlob); }
public void RequestResultErrorCode() { Uri baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint); CloudBlobClient client = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials); CloudBlobContainer container = client.GetContainerReference(Guid.NewGuid().ToString("N")); byte[] buffer = TestBase.GetRandomBuffer(4 * 1024 * 1024); MD5 md5 = MD5.Create(); string contentMD5 = Convert.ToBase64String(md5.ComputeHash(buffer)); try { RequestResult requestResult; XmlWriterSettings settings; StringBuilder sb; container.Create(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); List <string> blocks = new List <string>(); for (int i = 0; i < 2; i++) { blocks.Add(Convert.ToBase64String(Guid.NewGuid().ToByteArray())); } // Verify the ErrorCode property is set and that it is serialized correctly using (MemoryStream memoryStream = new MemoryStream(buffer)) { memoryStream.Seek(0, SeekOrigin.Begin); blob.PutBlock(blocks[0], memoryStream, contentMD5); int offset = buffer.Length - 1024; memoryStream.Seek(offset, SeekOrigin.Begin); StorageException e = TestHelper.ExpectedException <StorageException>( () => blob.PutBlock(blocks[1], memoryStream, contentMD5), "Invalid MD5 should fail with mismatch"); Assert.AreEqual(e.RequestInformation.ErrorCode, StorageErrorCodeStrings.Md5Mismatch); requestResult = new RequestResult(); settings = new XmlWriterSettings(); settings.Indent = true; sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, settings)) { e.RequestInformation.WriteXml(writer); } using (XmlReader reader = XmlReader.Create(new StringReader(sb.ToString()))) { requestResult.ReadXml(reader); } // ExtendedErrorInformation.ErrorCode will be depricated, but it should still match on a non HEAD request Assert.AreEqual(e.RequestInformation.ErrorCode, requestResult.ErrorCode); Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, requestResult.ErrorCode); } // Verify the ErrorCode property is set on a HEAD request CloudAppendBlob blob2 = container.GetAppendBlobReference("blob2"); blob2.CreateOrReplace(); StorageException e2 = TestHelper.ExpectedException <StorageException>( () => blob2.FetchAttributes(AccessCondition.GenerateIfMatchCondition("garbage")), "Mismatched etag should fail"); Assert.AreEqual(e2.RequestInformation.ErrorCode, StorageErrorCodeStrings.ConditionNotMet); // Verify the ErrorCode property is not set on a successful request and that it is serialized correctly OperationContext ctx = new OperationContext(); blob2.FetchAttributes(operationContext: ctx); Assert.AreEqual(ctx.RequestResults[0].ErrorCode, null); requestResult = new RequestResult(); settings = new XmlWriterSettings(); settings.Indent = true; sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, settings)) { ctx.RequestResults[0].WriteXml(writer); } using (XmlReader reader = XmlReader.Create(new StringReader(sb.ToString()))) { requestResult.ReadXml(reader); } Assert.AreEqual(ctx.RequestResults[0].ErrorCode, requestResult.ErrorCode); } finally { container.DeleteIfExists(); } }
private async Task FlagAsCopiedAysnc(CloudAppendBlob blob) { blob.FetchAttributes(); blob.Metadata["copied"] = DateTime.Now.ToString(); await blob.SetMetadataAsync(); }
public async Task AppendBlobCleanupAsync() { BlobContinuationToken continuationToken = null; // work through the Blob Segments cleaning up as we go... // (NOTE: there's currently no ListBlobsAsync method ;-) ) do { var listResult = await _tmpContainer.ListBlobsSegmentedAsync( prefix : null, useFlatBlobListing : true, blobListingDetails : BlobListingDetails.None, maxResults : null, currentToken : continuationToken, options : null, operationContext : null); foreach (IListBlobItem b in listResult.Results) { // list all blob in temp container, filter the ones that have "copied" metadata if (b.GetType() == typeof(CloudAppendBlob)) { CloudAppendBlob item = (CloudAppendBlob)b; item.FetchAttributes(); if (item.Metadata.Keys.Contains("copied")) { Console.WriteLine($"Delete blob {item.Uri.ToString()}"); await item.DeleteAsync(DeleteSnapshotsOption.IncludeSnapshots, accessCondition : null, options : null, operationContext : null); } else { if (item.Metadata.Keys.Contains("full")) { var targetBlob = await GetTargetBlockBlobAsync(item); await CopyBlobAsync(item, targetBlob); await FlagAsCopiedAysnc(item); } } } } continuationToken = listResult.ContinuationToken; } while (continuationToken != null); foreach (IListBlobItem b in _tmpContainer.ListBlobs(null, true)) { // list all blob in temp container, filter the ones that have "copied" metadata if (b.GetType() == typeof(CloudAppendBlob)) { CloudAppendBlob item = (CloudAppendBlob)b; item.FetchAttributes(); if (item.Metadata.Keys.Contains("copied")) { Console.WriteLine($"Delete blob {item.Uri.ToString()}"); await item.DeleteAsync(DeleteSnapshotsOption.IncludeSnapshots, accessCondition : null, options : null, operationContext : null); } else { if (item.Metadata.Keys.Contains("full")) { var targetBlob = await GetTargetBlockBlobAsync(item); await CopyBlobAsync(item, targetBlob); await FlagAsCopiedAysnc(item); } } } } }
private async Task FlagsAsFullAsync(CloudAppendBlob fullBlob) { fullBlob.FetchAttributes(); fullBlob.Metadata["full"] = DateTime.Now.ToString(); await fullBlob.SetMetadataAsync(); }
public void GetSnapshotBlobs() { string containerName = Utility.GenNameString("container"); string pageBlobName = Utility.GenNameString("page"); string blockBlobName = Utility.GenNameString("block"); string appendBlobName = Utility.GenNameString("append"); Test.Info("Create test container and blobs"); CloudBlobContainer container = blobUtil.CreateContainer(containerName); try { CloudBlob pageBlob = blobUtil.CreatePageBlob(container, pageBlobName); CloudBlob blockBlob = blobUtil.CreateBlockBlob(container, blockBlobName); CloudBlob appendBlob = blobUtil.CreateAppendBlob(container, appendBlobName); List <CloudBlob> blobs = new List <CloudBlob>(); pageBlob.FetchAttributes(); blockBlob.FetchAttributes(); appendBlob.FetchAttributes(); int minSnapshot = 1; int maxSnapshot = 5; int count = random.Next(minSnapshot, maxSnapshot); int snapshotInterval = 1 * 1000; Test.Info("Create random snapshot for specified blobs"); for (int i = 0; i < count; i++) { CloudAppendBlob snapshot = ((CloudAppendBlob)appendBlob).CreateSnapshot(); snapshot.FetchAttributes(); blobs.Add(snapshot); Thread.Sleep(snapshotInterval); } blobs.Add(appendBlob); count = random.Next(minSnapshot, maxSnapshot); for (int i = 0; i < count; i++) { CloudBlockBlob snapshot = ((CloudBlockBlob)blockBlob).CreateSnapshot(); snapshot.FetchAttributes(); blobs.Add(snapshot); Thread.Sleep(snapshotInterval); } blobs.Add(blockBlob); count = random.Next(minSnapshot, maxSnapshot); for (int i = 0; i < count; i++) { CloudPageBlob snapshot = ((CloudPageBlob)pageBlob).CreateSnapshot(); snapshot.FetchAttributes(); blobs.Add(snapshot); Thread.Sleep(snapshotInterval); } blobs.Add(pageBlob); Test.Assert(CommandAgent.GetAzureStorageBlob(string.Empty, containerName), Utility.GenComparisonData("Get-AzureStorageBlob with snapshot blobs", true)); Test.Assert(CommandAgent.Output.Count == blobs.Count, String.Format("Expect to retrieve {0} blobs, actually retrieved {1} blobs", blobs.Count, CommandAgent.Output.Count)); CommandAgent.OutputValidation(blobs); } finally { blobUtil.RemoveContainer(containerName); } }