public void CloudBlobContainerCreate() { CloudBlobContainer container = GetRandomContainerReference(); container.Create(); TestHelper.ExpectedException( () => container.Create(), "Creating already exists container should fail", HttpStatusCode.Conflict); container.Delete(); }
public void CloudBlobLocaleParsing() { CloudBlobContainer container = GetRandomContainerReference(); var currCulture = Thread.CurrentThread.CurrentCulture; try { const string blobname = "tempfile"; container.Create(); var blockBlob = container.GetBlockBlobReference(blobname); OperationContext context = new OperationContext(); blockBlob.UploadText("placeholder", null, null, null, context); foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures)) { Thread.CurrentThread.CurrentCulture = culture; Assert.IsTrue(blockBlob.Exists()); // parses the header to ensure can be parsed across cultures } // failed assertion means we never tested the parser } finally { Thread.CurrentThread.CurrentCulture = currCulture; container.DeleteIfExists(); } }
public void CloudPageBlobCopyTest() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); CloudPageBlob copy = container.GetPageBlobReference("copy"); string copyId = copy.StartCopyFromBlob(TestHelper.Defiddler(source)); WaitForCopy(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))); TestHelper.ExpectedException( () => copy.AbortCopy(copyId), "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); source.FetchAttributes(); 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 = DownloadText(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not similar"); copy.FetchAttributes(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); 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.Delete(); } finally { container.DeleteIfExists(); } }
public void CloudBlobContainerGetBlobReferenceFromServer() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); blockBlob.PutBlockList(new string[] {}); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); pageBlob.Create(0); ICloudBlob blob1 = container.GetBlobReferenceFromServer("bb"); Assert.IsInstanceOfType(blob1, typeof(CloudBlockBlob)); ICloudBlob blob2 = container.GetBlobReferenceFromServer("pb"); Assert.IsInstanceOfType(blob2, typeof(CloudPageBlob)); ICloudBlob blob3 = container.ServiceClient.GetBlobReferenceFromServer(blockBlob.Uri); Assert.IsInstanceOfType(blob3, typeof(CloudBlockBlob)); ICloudBlob blob4 = container.ServiceClient.GetBlobReferenceFromServer(pageBlob.Uri); Assert.IsInstanceOfType(blob4, typeof(CloudPageBlob)); } finally { container.DeleteIfExists(); } }
public void CloudBlobDirectoryGetParent() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("Dir1" + delimiter + "Blob1"); blob.Create(0); Assert.IsTrue(blob.Exists()); Assert.AreEqual("Dir1" + delimiter + "Blob1", blob.Name); CloudBlobDirectory parent = blob.Parent; Assert.AreEqual(parent.Prefix, "Dir1" + delimiter); blob.Delete(); } finally { container.DeleteIfExists(); } } }
private void InstanciateStorageContainer(string formattedStorageContainerName) { if ((_blobContainer == null) || (_blobContainer.Name != formattedStorageContainerName)) { var cloudStorageAccount = CloudStorageAccount.Parse(StorageConnectionString); var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); _blobContainer = cloudBlobClient.GetContainerReference(formattedStorageContainerName); if (!_blobContainer.Exists()) { try { _blobContainer.Create(); while (!_blobContainer.Exists()) Thread.Sleep(100); } catch (StorageException storageException) { WriteDebugError(String.Format("NLog.AzureStorage - Failed to create Azure Storage Blob Container '{0}' - Storage Exception: {1} {2}", formattedStorageContainerName, storageException.Message, GetStorageExceptionHttpStatusMessage(storageException))); throw; } } } }
public void CloudBlobContainerListBlobsSegmented() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); List <string> blobNames = CreateBlobs(container, 3, BlobType.PageBlob); BlobContinuationToken token = null; do { BlobResultSegment results = container.ListBlobsSegmented(null, true, BlobListingDetails.None, 1, token, null, null); int count = 0; foreach (IListBlobItem blobItem in results.Results) { Assert.IsInstanceOfType(blobItem, typeof(CloudPageBlob)); Assert.IsTrue(blobNames.Remove(((CloudPageBlob)blobItem).Name)); count++; } Assert.IsTrue(count >= 1); token = results.ContinuationToken; }while (token != null); Assert.AreEqual(0, blobNames.Count); } finally { container.DeleteIfExists(); } }
public void BlobWriteWhenOpenRead() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); using (MemoryStream srcStream = new MemoryStream(buffer)) { blob.UploadFromStream(srcStream); Stream blobStream = blob.OpenRead(); byte[] testBuffer = new byte[2048]; TestHelper.ExpectedException <NotSupportedException>(() => blobStream.Write(testBuffer, 0, 2048), "Try writing to a stream opened for read"); } } finally { container.DeleteIfExists(); } }
/// <summary> /// Occurs when a storage provider operation has completed. /// </summary> //public event EventHandler<StorageProviderEventArgs> StorageProviderOperationCompleted; #endregion // Initialiser method private void Initialise(string storageAccount, string containerName) { if (String.IsNullOrEmpty(containerName)) throw new ArgumentException("You must provide the base Container Name", "containerName"); ContainerName = containerName; _account = CloudStorageAccount.Parse(storageAccount); _blobClient = _account.CreateCloudBlobClient(); _container = _blobClient.GetContainerReference(ContainerName); try { _container.FetchAttributes(); } catch (StorageException) { Trace.WriteLine(string.Format("Create new container: {0}", ContainerName), "Information"); _container.Create(); // set new container's permissions // Create a permission policy to set the public access setting for the container. BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); // The public access setting explicitly specifies that the container is private, // so that it can't be accessed anonymously. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off; //Set the permission policy on the container. _container.SetPermissions(containerPermissions); } }
public void PageBlobReadStreamSeekTest() { byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamMinimumReadSizeInBytes = 2 * 1024 * 1024; using (MemoryStream wholeBlob = new MemoryStream(buffer)) { blob.UploadFromStream(wholeBlob); } OperationContext opContext = new OperationContext(); using (Stream blobStream = blob.OpenRead(null, null, opContext)) { int attempts = BlobReadStreamSeekTest(blobStream, blob.StreamMinimumReadSizeInBytes, buffer, false); TestHelper.AssertNAttempts(opContext, attempts); } opContext = new OperationContext(); using (Stream blobStream = blob.OpenRead(null, null, opContext)) { int attempts = BlobReadStreamSeekTest(blobStream, blob.StreamMinimumReadSizeInBytes, buffer, true); TestHelper.AssertNAttempts(opContext, attempts); } } finally { container.DeleteIfExists(); } }
public BlobContainer(string name) { // hämta connectionsträngen från config // RoleEnviroment bestämmer settingvalue runtime //var connectionString = RoleEnvironment.GetConfigurationSettingValue("PhotoAppStorage"); //var connectionString = CloudConfigurationManager.GetSetting("CloudStorageApp"); // hämtar kontot utfrån connectionsträngens värde //var account = CloudStorageAccount.Parse(connectionString); //var account = CloudStorageAccount.DevelopmentStorageAccount; var cred = new StorageCredentials("jholm", "/bVipQ2JxjWwYrZQfHmzhaBx1p1s8BoD/wX6VWOmg4/gpVo/aALrjsDUKqzXsFtc9utepPqe65NposrXt9YsyA=="); var account = new CloudStorageAccount(cred, true); // skapar en blobclient _client = account.CreateCloudBlobClient(); m_BlobContainer = _client.GetContainerReference(name); // Om det inte finns någon container med det namnet if (!m_BlobContainer.Exists()) { // Skapa containern m_BlobContainer.Create(); var permissions = new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob }; // Sätter public access till blobs m_BlobContainer.SetPermissions(permissions); } }
public void CloudBlobDirectoryFlatListingWithPrefix() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { container.Create(); if (CloudBlobDirectorySetupWithDelimiter(container, delimiter)) { BlobContinuationToken token = null; CloudBlobDirectory directory = container.GetDirectoryReference("TopDir1"); List <IListBlobItem> list1 = new List <IListBlobItem>(); do { BlobResultSegment result1 = directory.ListBlobsSegmented(token); token = result1.ContinuationToken; list1.AddRange(result1.Results); }while (token != null); Assert.IsTrue(list1.Count == 3); IListBlobItem item11 = list1.ElementAt(0); Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1")); IListBlobItem item12 = list1.ElementAt(1); Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter)); IListBlobItem item13 = list1.ElementAt(2); Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter)); CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13; List <IListBlobItem> list2 = new List <IListBlobItem>(); do { BlobResultSegment result2 = midDir2.ListBlobsSegmented(true, BlobListingDetails.None, null, token, null, null); token = result2.ContinuationToken; list2.AddRange(result2.Results); }while (token != null); Assert.IsTrue(list2.Count == 2); IListBlobItem item41 = list2.ElementAt(0); Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item42 = list2.ElementAt(1); Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2")); } } finally { container.DeleteIfExists(); } } }
public void BlobEmptyHeaderSigningTest() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudBlobContainer container = GetRandomContainerReference(); OperationContext context = new OperationContext(); try { container.Create(null, context); CloudPageBlob blob = container.GetPageBlobReference("blob1"); context.UserHeaders = new Dictionary <string, string>(); context.UserHeaders.Add("x-ms-foo", String.Empty); using (MemoryStream srcStream = new MemoryStream(buffer)) { blob.UploadFromStream(srcStream, null, null, context); } byte[] testBuffer2 = new byte[2048]; MemoryStream dstStream2 = new MemoryStream(testBuffer2); blob.DownloadRangeToStream(dstStream2, null, null, null, null, context); } finally { container.DeleteIfExists(); } }
public void BlobOpenWriteTest() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); using (CloudBlobStream blobStream = blob.OpenWrite(2048)) { blobStream.Write(buffer, 0, 2048); blobStream.Flush(); byte[] testBuffer = new byte[2048]; MemoryStream dstStream = new MemoryStream(testBuffer); blob.DownloadRangeToStream(dstStream, null, null); MemoryStream memStream = new MemoryStream(buffer); TestHelper.AssertStreamsAreEqual(memStream, dstStream); } } finally { container.DeleteIfExists(); } }
public void BlobOpenWriteSeekReadTest() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); MemoryStream memoryStream = new MemoryStream(buffer); Stream blobStream = blob.OpenWrite(2048); blobStream.Write(buffer, 0, 2048); Assert.AreEqual(blobStream.Position, 2048); blobStream.Seek(1024, 0); memoryStream.Seek(1024, 0); Assert.AreEqual(blobStream.Position, 1024); byte[] testBuffer = GetRandomBuffer(1024); memoryStream.Write(testBuffer, 0, 1024); blobStream.Write(testBuffer, 0, 1024); Assert.AreEqual(blobStream.Position, memoryStream.Position); blobStream.Close(); Stream dstStream = blob.OpenRead(); TestHelper.AssertStreamsAreEqual(memoryStream, dstStream); } finally { container.DeleteIfExists(); } }
public void PageBlobReadStreamBasicTest() { byte[] buffer = GetRandomBuffer(5 * 1024 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); using (MemoryStream wholeBlob = new MemoryStream(buffer)) { blob.UploadFromStream(wholeBlob); } using (MemoryStream wholeBlob = new MemoryStream(buffer)) { using (Stream blobStream = blob.OpenRead()) { TestHelper.AssertStreamsAreEqual(wholeBlob, blobStream); } } } finally { container.DeleteIfExists(); } }
public void CloudBlobContainerExistsAPM() { CloudBlobContainer container = GetRandomContainerReference(); try { using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = container.BeginExists( ar => waitHandle.Set(), null); waitHandle.WaitOne(); Assert.IsFalse(container.EndExists(result)); container.Create(); result = container.BeginExists( ar => waitHandle.Set(), null); waitHandle.WaitOne(); Assert.IsTrue(container.EndExists(result)); } } finally { container.DeleteIfExists(); } Assert.IsFalse(container.Exists()); }
public void CloudPageBlobCopyWithSourceAccessCondition() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); UploadText(source, data, Encoding.UTF8); string validLeaseId = Guid.NewGuid().ToString(); string leaseId = source.AcquireLease(TimeSpan.FromSeconds(60), validLeaseId); string invalidLeaseId = Guid.NewGuid().ToString(); source.FetchAttributes(); AccessCondition sourceAccessCondition1 = AccessCondition.GenerateIfNotModifiedSinceCondition(source.Properties.LastModified.Value); CloudPageBlob copy1 = container.GetPageBlobReference("copy1"); copy1.StartCopyFromBlob(TestHelper.Defiddler(source), sourceAccessCondition1); WaitForCopy(copy1); Assert.AreEqual(CopyStatus.Success, copy1.CopyState.Status); AccessCondition sourceAccessCondition2 = AccessCondition.GenerateLeaseCondition(invalidLeaseId); CloudPageBlob copy2 = container.GetPageBlobReference("copy2"); TestHelper.ExpectedException <ArgumentException>(() => copy2.StartCopyFromBlob(TestHelper.Defiddler(source), sourceAccessCondition2), "A lease condition cannot be specified on the source of a copy."); } finally { container.DeleteIfExists(); } }
public void CloudBlobContainerSetMetadata() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlobContainer container2 = container.ServiceClient.GetContainerReference(container.Name); container2.FetchAttributes(); Assert.AreEqual(0, container2.Metadata.Count); container.Metadata.Add("key1", "value1"); container.SetMetadata(); container2.FetchAttributes(); Assert.AreEqual(1, container2.Metadata.Count); Assert.AreEqual("value1", container2.Metadata["key1"]); CloudBlobContainer container3 = container.ServiceClient.ListContainers(container.Name, ContainerListingDetails.Metadata).First(); Assert.AreEqual(1, container3.Metadata.Count); Assert.AreEqual("value1", container3.Metadata["key1"]); container.Metadata.Clear(); container.SetMetadata(); container2.FetchAttributes(); Assert.AreEqual(0, container2.Metadata.Count); } finally { container.DeleteIfExists(); } }
public void CloudBlobDirectoryFlatListing() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { container.Create(); if (CloudBlobDirectorySetupWithDelimiter(container, delimiter)) { IEnumerable <IListBlobItem> list1 = container.ListBlobs("TopDir1" + delimiter, false, BlobListingDetails.None, null, null); List <IListBlobItem> simpleList1 = list1.ToList(); ////Check if for 3 because if there were more than 3, the previous assert would have failed. ////So the only thing we need to make sure is that it is not less than 3. 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)); IEnumerable <IListBlobItem> list2 = container.ListBlobs("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, null); List <IListBlobItem> simpleList2 = list2.ToList(); 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")); IEnumerable <IListBlobItem> list3 = container.ListBlobs("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, null); List <IListBlobItem> simpleList3 = list3.ToList(); 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)); } } finally { container.DeleteIfExists(); } } }
public void CloudPageBlobCopyFromSnapshotTest() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); CloudPageBlob snapshot = source.CreateSnapshot(); //Modify source string newData = new string('b', 512); source.Metadata["Test"] = "newvalue"; source.SetMetadata(); source.Properties.ContentMD5 = null; UploadText(source, newData, Encoding.UTF8); Assert.AreEqual(newData, DownloadText(source, Encoding.UTF8), "Source is modified correctly"); Assert.AreEqual(data, DownloadText(snapshot, Encoding.UTF8), "Modifying source blob should not modify snapshot"); source.FetchAttributes(); snapshot.FetchAttributes(); Assert.AreNotEqual(source.Metadata["Test"], snapshot.Metadata["Test"], "Source and snapshot metadata should be independent"); CloudPageBlob copy = container.GetPageBlobReference("copy"); copy.StartCopyFromBlob(TestHelper.Defiddler(snapshot)); WaitForCopy(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(data, DownloadText(copy, Encoding.UTF8), "Data inside copy of blob not similar"); copy.FetchAttributes(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = snapshot.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); 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.Delete(); } finally { container.DeleteIfExists(); } }
public void CloudBlobContainerDeleteIfExists() { CloudBlobContainer container = GetRandomContainerReference(); Assert.IsFalse(container.DeleteIfExists()); container.Create(); Assert.IsTrue(container.DeleteIfExists()); Assert.IsFalse(container.DeleteIfExists()); }
public void PageBlobReadLockToETagTest() { byte[] outBuffer = new byte[1 * 1024 * 1024]; byte[] buffer = GetRandomBuffer(2 * outBuffer.Length); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamMinimumReadSizeInBytes = outBuffer.Length; using (MemoryStream wholeBlob = new MemoryStream(buffer)) { blob.UploadFromStream(wholeBlob); } using (Stream blobStream = blob.OpenRead()) { blob.SetMetadata(); blobStream.Read(outBuffer, 0, outBuffer.Length); blob.SetMetadata(); TestHelper.ExpectedException( () => blobStream.Read(outBuffer, 0, outBuffer.Length), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } using (Stream blobStream = blob.OpenRead()) { blob.SetMetadata(); long length = blobStream.Length; blob.SetMetadata(); TestHelper.ExpectedException( () => blobStream.Read(outBuffer, 0, outBuffer.Length), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now); using (Stream blobStream = blob.OpenRead(accessCondition)) { blob.SetMetadata(); blobStream.Read(outBuffer, 0, outBuffer.Length); blob.SetMetadata(); TestHelper.ExpectedException( () => blobStream.Read(outBuffer, 0, outBuffer.Length), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } } finally { container.DeleteIfExists(); } }
public void CloudBlobClientListBlobsSegmentedWithPrefix() { string name = "bb" + GetRandomContainerName(); CloudBlobClient blobClient = GenerateCloudBlobClient(); CloudBlobContainer rootContainer = blobClient.GetRootContainerReference(); CloudBlobContainer container = blobClient.GetContainerReference(name); try { rootContainer.CreateIfNotExists(); container.Create(); List <string> blobNames = CreateBlobs(container, 3, BlobType.BlockBlob); List <string> rootBlobNames = CreateBlobs(rootContainer, 2, BlobType.BlockBlob); BlobResultSegment results; BlobContinuationToken token = null; do { results = blobClient.ListBlobsSegmented("bb", token); token = results.ContinuationToken; foreach (CloudBlockBlob blob in results.Results) { blob.Delete(); rootBlobNames.Remove(blob.Name); } }while (token != null); Assert.AreEqual(0, rootBlobNames.Count); results = blobClient.ListBlobsSegmented("bb", token); Assert.AreEqual(0, results.Results.Count()); Assert.IsNull(results.ContinuationToken); results = blobClient.ListBlobsSegmented(name, token); Assert.AreEqual(0, results.Results.Count()); Assert.IsNull(results.ContinuationToken); token = null; do { results = blobClient.ListBlobsSegmented(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.DeleteIfExists(); } }
public void CloudBlobContainerConditionalAccess() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); container.FetchAttributes(); string currentETag = container.Properties.ETag; DateTimeOffset currentModifiedTime = container.Properties.LastModified.Value; // ETag conditional tests container.Metadata["ETagConditionalName"] = "ETagConditionalValue"; container.SetMetadata(); container.FetchAttributes(); string newETag = container.Properties.ETag; Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata"); // LastModifiedTime tests currentModifiedTime = container.Properties.LastModified.Value; container.Metadata["DateConditionalName"] = "DateConditionalValue"; TestHelper.ExpectedException( () => container.SetMetadata(AccessCondition.GenerateIfModifiedSinceCondition(currentModifiedTime), null), "IfModifiedSince conditional on current modified time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); container.Metadata["DateConditionalName"] = "DateConditionalValue2"; currentETag = container.Properties.ETag; DateTimeOffset pastTime = currentModifiedTime.Subtract(TimeSpan.FromMinutes(5)); container.SetMetadata(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null); pastTime = currentModifiedTime.Subtract(TimeSpan.FromHours(5)); container.SetMetadata(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null); pastTime = currentModifiedTime.Subtract(TimeSpan.FromDays(5)); container.SetMetadata(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null); container.FetchAttributes(); newETag = container.Properties.ETag; Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata"); } finally { container.DeleteIfExists(); } }
public void CloudPageBlobCopyTestWithMetadataOverride() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); CloudPageBlob copy = container.GetPageBlobReference("copy"); copy.Metadata["Test2"] = "value2"; string copyId = copy.StartCopyFromBlob(TestHelper.Defiddler(source)); WaitForCopy(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))); string copyData = DownloadText(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not similar"); copy.FetchAttributes(); source.FetchAttributes(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value2", copy.Metadata["Test2"], false, "Copied metadata not same"); Assert.IsFalse(copy.Metadata.ContainsKey("Test"), "Source Metadata should not appear in destination blob"); copy.Delete(); } finally { container.DeleteIfExists(); } }
public void CloudBlobContainerGetBlobReferenceFromServerAPM() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); blockBlob.PutBlockList(new string[] { }); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); pageBlob.Create(0); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = container.BeginGetBlobReferenceFromServer("bb", ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob1 = container.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob1, typeof(CloudBlockBlob)); result = container.BeginGetBlobReferenceFromServer("pb", ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob2 = container.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob2, typeof(CloudPageBlob)); result = container.ServiceClient.BeginGetBlobReferenceFromServer(blockBlob.Uri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob3 = container.ServiceClient.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob3, typeof(CloudBlockBlob)); result = container.ServiceClient.BeginGetBlobReferenceFromServer(pageBlob.Uri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob4 = container.ServiceClient.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob4, typeof(CloudPageBlob)); } } finally { container.DeleteIfExists(); } }
public void CloudBlockBlobDownloadToStreamAPMCancel() { byte[] buffer = GetRandomBuffer(1 * 1024 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); using (MemoryStream originalBlob = new MemoryStream(buffer)) { using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { ICancellableAsyncResult result = blob.BeginUploadFromStream(originalBlob, ar => waitHandle.Set(), null); waitHandle.WaitOne(); blob.EndUploadFromStream(result); using (MemoryStream downloadedBlob = new MemoryStream()) { OperationContext operationContext = new OperationContext(); result = blob.BeginDownloadToStream(downloadedBlob, null, null, operationContext, ar => waitHandle.Set(), null); Thread.Sleep(100); result.Cancel(); waitHandle.WaitOne(); try { blob.EndDownloadToStream(result); } catch (StorageException ex) { Assert.AreEqual(ex.Message, "Operation was canceled by user."); Assert.AreEqual(ex.RequestInformation.HttpStatusCode, 306); Assert.AreEqual(ex.RequestInformation.HttpStatusMessage, "Unused"); } TestHelper.AssertNAttempts(operationContext, 1); } } } } finally { container.DeleteIfExists(); } }
public void PageBlobWriteStreamRandomSeekTest() { byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); CloudBlobContainer container = GetRandomContainerReference(); container.ServiceClient.ParallelOperationThreadCount = 2; try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); using (MemoryStream wholeBlob = new MemoryStream()) { using (Stream blobStream = blob.OpenWrite(buffer.Length)) { TestHelper.ExpectedException <ArgumentOutOfRangeException>( () => blobStream.Seek(1, SeekOrigin.Begin), "Page blob stream should not allow unaligned seeks"); blobStream.Write(buffer, 0, buffer.Length); wholeBlob.Write(buffer, 0, buffer.Length); Random random = new Random(); for (int i = 0; i < 10; i++) { int offset = random.Next(buffer.Length / 512) * 512; SeekRandomly(blobStream, offset); blobStream.Write(buffer, 0, buffer.Length - offset); wholeBlob.Seek(offset, SeekOrigin.Begin); wholeBlob.Write(buffer, 0, buffer.Length - offset); } } wholeBlob.Seek(0, SeekOrigin.End); blob.FetchAttributes(); Assert.IsNull(blob.Properties.ContentMD5); using (MemoryStream downloadedBlob = new MemoryStream()) { blob.DownloadToStream(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } } } finally { container.DeleteIfExists(); } }
public void PageBlobReadStreamReadSizeTest() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); BlobReadStreamReadSizeTest(blob); } finally { container.DeleteIfExists(); } }
public void PageBlobWriteStreamOneByteTest() { byte buffer = 127; MD5 hasher = MD5.Create(); CloudBlobContainer container = GetRandomContainerReference(); container.ServiceClient.ParallelOperationThreadCount = 2; try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamWriteSizeInBytes = 16 * 1024; using (MemoryStream wholeBlob = new MemoryStream()) { BlobRequestOptions options = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; using (Stream blobStream = blob.OpenWrite(1 * 1024 * 1024, null, options)) { for (int i = 0; i < 1 * 1024 * 1024; i++) { blobStream.WriteByte(buffer); wholeBlob.WriteByte(buffer); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } } wholeBlob.Seek(0, SeekOrigin.Begin); string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeBlob)); blob.FetchAttributes(); Assert.AreEqual(md5, blob.Properties.ContentMD5); using (MemoryStream downloadedBlob = new MemoryStream()) { blob.DownloadToStream(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } } } finally { container.DeleteIfExists(); } }
public void CloudBlobDirectoryGetParent() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("Dir1" + delimiter + "Blob1"); blob.Create(0); Assert.AreEqual("Dir1" + delimiter + "Blob1", blob.Name); // get the blob's parent CloudBlobDirectory parent = blob.Parent; Assert.AreEqual(parent.Prefix, "Dir1" + delimiter); // get container as parent CloudBlobDirectory root = parent.Parent; Assert.AreEqual(root.Prefix, ""); // make sure the parent of the container dir is null CloudBlobDirectory empty = root.Parent; Assert.IsNull(empty); // from container, get directory reference to container root = container.GetDirectoryReference(""); Assert.AreEqual("", root.Prefix); Assert.AreEqual(container.Uri.AbsoluteUri, root.Uri.AbsoluteUri); List <IListBlobItem> list = root.ListBlobs().ToList(); Assert.AreEqual(1, list.Count); // make sure the parent of the container dir is null empty = root.Parent; Assert.IsNull(empty); blob.DeleteIfExists(); } finally { container.DeleteIfExists(); } } }
public BlobTriggerTests() { _timesProcessed = 0; RandomNameResolver nameResolver = new RandomNameResolver(); _hostConfiguration = new JobHostConfiguration() { NameResolver = nameResolver, TypeLocator = new FakeTypeLocator(typeof(BlobTriggerTests)), }; _storageAccount = CloudStorageAccount.Parse(_hostConfiguration.StorageConnectionString); CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient(); _testContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(ContainerName)); Assert.False(_testContainer.Exists()); _testContainer.Create(); }
public static void Create(string account, string key, string containerName, bool publicAccess) { if (string.IsNullOrEmpty(containerName)) return; CloudBlobClient blobClient = Client.GetBlobClient(account, key); Uri uri = new Uri(string.Format(blobClient.BaseUri + "{0}", containerName)); CloudBlobContainer cont = new CloudBlobContainer(uri, blobClient.Credentials); cont.Create(); if (publicAccess) { BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; cont.SetPermissions(permissions); } }
public void CloudBlobSnapshotMetadataAPM() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplace(); CloudBlob blob = container.GetBlobReference(BlobName); blob.Metadata["Hello"] = "World"; blob.Metadata["Marco"] = "Polo"; blob.SetMetadata(); IDictionary <string, string> snapshotMetadata = new Dictionary <string, string>(); snapshotMetadata["Hello"] = "Dolly"; snapshotMetadata["Yoyo"] = "Ma"; IAsyncResult result; using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { result = blob.BeginSnapshot(snapshotMetadata, null, null, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); CloudBlob snapshot = blob.EndSnapshot(result); // Test the client view against the expected metadata // None of the original metadata should be present Assert.AreEqual("Dolly", snapshot.Metadata["Hello"]); Assert.AreEqual("Ma", snapshot.Metadata["Yoyo"]); Assert.IsFalse(snapshot.Metadata.ContainsKey("Marco")); // Test the server view against the expected metadata snapshot.FetchAttributes(); Assert.AreEqual("Dolly", snapshot.Metadata["Hello"]); Assert.AreEqual("Ma", snapshot.Metadata["Yoyo"]); Assert.IsFalse(snapshot.Metadata.ContainsKey("Marco")); } } finally { container.DeleteIfExists(); } }
// Initialiser method private void Initialise(string containerName) { if (String.IsNullOrEmpty(containerName)) throw new ArgumentException("You must provide the base Container Name", "containerName"); ContainerName = containerName; if (StorageProviderConfiguration.Mode == Modes.Debug) { _account = CloudStorageAccount.DevelopmentStorageAccount; _blobClient = _account.CreateCloudBlobClient(); _blobClient.ServerTimeout = new TimeSpan(0, 0, 0, 5); } else { _account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("FTP2Azure.StorageAccount")); _blobClient = _account.CreateCloudBlobClient(); _blobClient.ServerTimeout = new TimeSpan(0, 0, 0, 5); } _container = _blobClient.GetContainerReference(ContainerName); try { _container.FetchAttributes(); } catch (StorageException) { Trace.WriteLine(string.Format("Create new container: {0}", ContainerName), "Information"); _container.Create(); // set new container's permissions // Create a permission policy to set the public access setting for the container. BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); // The public access setting explicitly specifies that the container is private, // so that it can't be accessed anonymously. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off; //Set the permission policy on the container. _container.SetPermissions(containerPermissions); } }