public void SetBlobContentWithProperties(StorageBlob.BlobType blobType) { string filePath = FileUtil.GenerateOneTempTestFile(); CloudBlobContainer container = blobUtil.CreateContainer(); Hashtable properties = new Hashtable(); properties.Add("CacheControl", Utility.GenNameString(string.Empty)); properties.Add("ContentEncoding", Utility.GenNameString(string.Empty)); properties.Add("ContentLanguage", Utility.GenNameString(string.Empty)); properties.Add("ContentType", Utility.GenNameString(string.Empty)); try { Test.Assert(CommandAgent.SetAzureStorageBlobContent(filePath, container.Name, blobType, string.Empty, true, -1, properties), "set blob content with property should succeed"); CloudBlob blob = StorageExtensions.GetBlobReferenceFromServer(container, Path.GetFileName(filePath)); blob.FetchAttributes(); ExpectEqual(properties["CacheControl"].ToString(), blob.Properties.CacheControl, "Cache control"); ExpectEqual(properties["ContentEncoding"].ToString(), blob.Properties.ContentEncoding, "Content Encoding"); ExpectEqual(properties["ContentLanguage"].ToString(), blob.Properties.ContentLanguage, "Content Language"); ExpectEqual(properties["ContentType"].ToString(), blob.Properties.ContentType, "Content Type"); } finally { blobUtil.RemoveContainer(container.Name); FileUtil.RemoveFile(filePath); } }
/// <summary> /// Expect sas token has the Append permission for the specified container. /// </summary> internal void ValidateContainerAppendableWithSasToken(CloudBlobContainer container, string sastoken) { Test.Info("Verify container Append permission"); CloudStorageAccount sasAccount = TestBase.GetStorageAccountWithSasToken(container.ServiceClient.Credentials.AccountName, sastoken); CloudBlobClient sasBlobClient = sasAccount.CreateCloudBlobClient(); CloudBlobContainer sasContainer = sasBlobClient.GetContainerReference(container.Name); string blobName = Utility.GenNameString("sasAppendblob"); CloudAppendBlob appendblob = sasContainer.GetAppendBlobReference(blobName); container.GetAppendBlobReference(blobName).CreateOrReplace(); long buffSize = 1024 * 1024; byte[] buffer = new byte[buffSize]; random.NextBytes(buffer); MemoryStream ms = new MemoryStream(buffer); appendblob.AppendBlock(ms); CloudBlob retrievedBlob = StorageExtensions.GetBlobReferenceFromServer(container, blobName); Test.Assert(retrievedBlob != null, "Append blob should exist on server"); TestBase.ExpectEqual(StorageBlobType.AppendBlob.ToString(), retrievedBlob.BlobType.ToString(), "blob type"); TestBase.ExpectEqual(buffSize, retrievedBlob.Properties.Length, "blob size"); }
public void GetCopyStateFromCrossAccountCopyTest() { CloudStorageAccount secondaryAccount = TestBase.GetCloudStorageAccountFromConfig("Secondary"); object destContext; if (lang == Language.PowerShell) { destContext = PowerShellAgent.GetStorageContext(secondaryAccount.ToString(true)); } else { destContext = secondaryAccount; } CloudBlobUtil destBlobUtil = new CloudBlobUtil(secondaryAccount); string destContainerName = Utility.GenNameString("secondary"); CloudBlobContainer destContainer = destBlobUtil.CreateContainer(destContainerName); if (lang == Language.PowerShell) { blobUtil.SetupTestContainerAndBlob(); } else { blobUtil.SetupTestContainerAndBlob(blobNamePrefix: "blob"); } //remove the same name container in source storage account, so we could avoid some conflicts. if (!blobUtil.Blob.ServiceClient.BaseUri.Host.Equals(destContainer.ServiceClient.BaseUri.Host)) { blobUtil.RemoveContainer(destContainer.Name); } try { Test.Assert(CommandAgent.StartAzureStorageBlobCopy(blobUtil.Blob, destContainer.Name, string.Empty, destContext), "Start cross account copy should succeed"); int expectedBlobCount = 1; Test.Assert(CommandAgent.Output.Count == expectedBlobCount, String.Format("Expected get {0} copy blob, and actually it's {1}", expectedBlobCount, CommandAgent.Output.Count)); CloudBlob destBlob; object context; if (lang == Language.PowerShell) { destBlob = (CloudBlob)CommandAgent.Output[0]["ICloudBlob"]; //make sure this context is different from the PowerShell.Context context = CommandAgent.Output[0]["Context"]; Test.Assert(PowerShellAgent.Context != context, "make sure you are using different context for cross account copy"); } else { destBlob = StorageExtensions.GetBlobReferenceFromServer(destContainer, (string)CommandAgent.Output[0]["name"]); context = destContext; } Test.Assert(CommandAgent.GetAzureStorageBlobCopyState(destBlob, context, true), "Get copy state in dest container should succeed."); AssertFinishedCopyState(blobUtil.Blob.Uri); } finally { blobUtil.CleanupTestContainerAndBlob(); destBlobUtil.RemoveContainer(destContainer.Name); } }
public void SetBlobContentWithMetadata(StorageBlob.BlobType blobType) { string filePath = FileUtil.GenerateOneTempTestFile(); CloudBlobContainer container = blobUtil.CreateContainer(); Hashtable metadata = new Hashtable(); int metaCount = Utility.GetRandomTestCount(); for (int i = 0; i < metaCount; i++) { string key = Utility.GenRandomAlphabetString(); string value = Utility.GenNameString(string.Empty); if (!metadata.ContainsKey(key)) { Test.Info(string.Format("Add meta key: {0} value : {1}", key, value)); metadata.Add(key, value); } } try { Test.Assert(CommandAgent.SetAzureStorageBlobContent(filePath, container.Name, blobType, string.Empty, true, -1, null, metadata), "set blob content with meta should succeed"); CloudBlob blob = StorageExtensions.GetBlobReferenceFromServer(container, Path.GetFileName(filePath)); blob.FetchAttributes(); ExpectEqual(metadata.Count, blob.Metadata.Count, "meta data count"); foreach (string key in metadata.Keys) { if (blob.Metadata.ContainsKey(key)) { ExpectEqual(metadata[key].ToString(), blob.Metadata[key], "Meta data key " + key); } else if (blob.Metadata.ContainsKey(key.ToLower())) { // NodeJS stores key in lower case ExpectEqual(metadata[key].ToString().ToLower(), blob.Metadata[key.ToLower()], "Meta data key " + key); } else { Test.AssertFail("Could not find meta data key " + key + " in blob entity"); } } } finally { blobUtil.RemoveContainer(container.Name); FileUtil.RemoveFile(filePath); } }
/// <summary> /// Expect sas token has the read permission for the specified container. /// </summary> internal void ValidateContainerReadableWithSasToken(CloudBlobContainer container, string sastoken) { Test.Info("Verify container read permission"); List <CloudBlob> randomBlobs = CreateRandomBlob(container); CloudBlob blob = randomBlobs[0]; CloudStorageAccount sasAccount = TestBase.GetStorageAccountWithSasToken(container.ServiceClient.Credentials.AccountName, sastoken); CloudBlobClient sasBlobClient = sasAccount.CreateCloudBlobClient(); CloudBlobContainer sasContainer = sasBlobClient.GetContainerReference(container.Name); CloudBlob retrievedBlob = StorageExtensions.GetBlobReferenceFromServer(sasContainer, blob.Name); long buffSize = retrievedBlob.Properties.Length; byte[] buffer = new byte[buffSize]; MemoryStream ms = new MemoryStream(buffer); retrievedBlob.DownloadRangeToStream(ms, 0, buffSize); string md5 = Utility.GetBufferMD5(buffer); TestBase.ExpectEqual(blob.Properties.ContentMD5, md5, "content md5"); }
/// <summary> /// Expect sas token has the Create permission for the specified container. /// </summary> internal void ValidateContainerCreateableWithSasToken(CloudBlobContainer container, string sastoken) { Test.Info("Verify container Create permission"); CloudStorageAccount sasAccount = TestBase.GetStorageAccountWithSasToken(container.ServiceClient.Credentials.AccountName, sastoken); CloudBlobClient sasBlobClient = sasAccount.CreateCloudBlobClient(); CloudBlobContainer sasContainer = sasBlobClient.GetContainerReference(container.Name); if (!container.Exists()) { sasContainer.Create(); Test.Assert(sasContainer.Exists(), "The container should exist after Creating with sas token"); } string blobName = Utility.GenNameString("saspageblob"); CloudPageBlob pageblob = sasContainer.GetPageBlobReference(blobName); long blobSize = 1024 * 1024; pageblob.Create(blobSize); CloudBlob retrievedBlob = StorageExtensions.GetBlobReferenceFromServer(container, blobName); Test.Assert(retrievedBlob != null, "Page blob should exist on server"); TestBase.ExpectEqual(StorageBlobType.PageBlob.ToString(), retrievedBlob.BlobType.ToString(), "blob type"); TestBase.ExpectEqual(blobSize, retrievedBlob.Properties.Length, "blob size"); }