private async Task BasicStoragePageBlobOperationsAsync() { WriteLine("-- Testing PageBlob --"); const string PageBlobName = "samplepageblob"; // Create a blob client for interacting with the blob service. CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient(); // Create a container for organizing blobs within the storage account. WriteLine("1. Creating Container"); CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob"); await container.CreateIfNotExistsAsync(); // Create a page blob in the newly created container. WriteLine("2. Creating Page Blob"); CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName); await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes // Write to a page blob WriteLine("2. Write to a Page Blob"); byte[] samplePagedata = new byte[512]; Random random = new Random(); random.NextBytes(samplePagedata); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); // List all blobs in this container. Because a container can contain a large number of blobs the results // are returned in segments (pages) with a maximum of 5000 blobs per segment. You can define a smaller size // using the maxResults parameter on ListBlobsSegmentedAsync. WriteLine("3. List Blobs in Container"); BlobContinuationToken token = null; do { BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token); token = resultSegment.ContinuationToken; foreach (IListBlobItem blob in resultSegment.Results) { // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory WriteLine(string.Format("{0} (type: {1}", blob.Uri, blob.GetType())); } } while (token != null); // Read from a page blob WriteLine("4. Read from a Page Blob"); int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Length); // Clean up after the demo WriteLine("5. Delete page Blob"); await pageBlob.DeleteAsync(); // When you delete a container it could take several seconds before you can recreate a container with the same // name - hence to enable you to run the demo in quick succession the container is not deleted. If you want // to delete the container uncomment the line of code below. WriteLine("6. Delete Container"); await container.DeleteAsync(); WriteLine("-- Test Complete --"); }
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); } }
private async Task Initialize() { RandomNameResolver nameResolver = new RandomNameResolver(); JobHostConfiguration hostConfiguration = new JobHostConfiguration() { NameResolver = nameResolver, TypeLocator = new FakeTypeLocator(typeof(BlobBindingEndToEndTests)), }; hostConfiguration.AddService <IWebJobsExceptionHandler>(new TestExceptionHandler()); Config = hostConfiguration; StorageAccount = CloudStorageAccount.Parse(hostConfiguration.StorageConnectionString); CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient(); BlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(ContainerName)); Assert.False(await BlobContainer.ExistsAsync()); await BlobContainer.CreateAsync(); OutputBlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(OutputContainerName)); CloudBlobContainer pageBlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(PageBlobContainerName)); Assert.False(await pageBlobContainer.ExistsAsync()); await pageBlobContainer.CreateAsync(); CloudBlobContainer hierarchicalBlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(HierarchicalBlobContainerName)); Assert.False(await hierarchicalBlobContainer.ExistsAsync()); await hierarchicalBlobContainer.CreateAsync(); CloudBlobContainer appendBlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(AppendBlobContainerName)); Assert.False(await appendBlobContainer.ExistsAsync()); await appendBlobContainer.CreateAsync(); Host = new JobHost(hostConfiguration); Host.Start(); // upload some test blobs CloudBlockBlob blob = BlobContainer.GetBlockBlobReference("blob1"); await blob.UploadTextAsync(TestData); blob = BlobContainer.GetBlockBlobReference("blob2"); await blob.UploadTextAsync(TestData); blob = BlobContainer.GetBlockBlobReference("blob3"); await blob.UploadTextAsync(TestData); blob = BlobContainer.GetBlockBlobReference("file1"); await blob.UploadTextAsync(TestData); blob = BlobContainer.GetBlockBlobReference("file2"); await blob.UploadTextAsync(TestData); blob = BlobContainer.GetBlockBlobReference("overwrite"); await blob.UploadTextAsync(TestData); // add a couple hierarchical blob paths blob = hierarchicalBlobContainer.GetBlockBlobReference("sub/blob1"); await blob.UploadTextAsync(TestData); blob = hierarchicalBlobContainer.GetBlockBlobReference("sub/blob2"); await blob.UploadTextAsync(TestData); blob = hierarchicalBlobContainer.GetBlockBlobReference("sub/sub/blob3"); await blob.UploadTextAsync(TestData); blob = hierarchicalBlobContainer.GetBlockBlobReference("blob4"); await blob.UploadTextAsync(TestData); byte[] bytes = new byte[512]; byte[] testBytes = Encoding.UTF8.GetBytes(TestData); for (int i = 0; i < testBytes.Length; i++) { bytes[i] = testBytes[i]; } CloudPageBlob pageBlob = pageBlobContainer.GetPageBlobReference("blob1"); await pageBlob.UploadFromByteArrayAsync(bytes, 0, bytes.Length); pageBlob = pageBlobContainer.GetPageBlobReference("blob2"); await pageBlob.UploadFromByteArrayAsync(bytes, 0, bytes.Length); CloudAppendBlob appendBlob = appendBlobContainer.GetAppendBlobReference("blob1"); await appendBlob.UploadTextAsync(TestData); appendBlob = appendBlobContainer.GetAppendBlobReference("blob2"); await appendBlob.UploadTextAsync(TestData); appendBlob = appendBlobContainer.GetAppendBlobReference("blob3"); await appendBlob.UploadTextAsync(TestData); }