public async Task createStorageAccountAndGetBlobContainerCollection() { ArmClient armClient = new ArmClient(new DefaultAzureCredential()); SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync(); string rgName = "myRgName"; AzureLocation location = AzureLocation.WestUS2; ArmOperation <ResourceGroupResource> operation = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(location)); ResourceGroupResource resourceGroup = operation.Value; this.resourceGroup = resourceGroup; StorageSku sku = new StorageSku(StorageSkuName.StandardGRS); StorageKind kind = StorageKind.Storage; string locationStr = "westus2"; StorageAccountCreateParameters parameters = new StorageAccountCreateParameters(sku, kind, locationStr); StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts(); string accountName = "myAccount"; ArmOperation <StorageAccountResource> accountCreateOperation = await accountCollection.CreateOrUpdateAsync(WaitUntil.Started, accountName, parameters); storageAccount = await accountCreateOperation.WaitForCompletionAsync(); #region Snippet:Managing_BlobContainers_GetBlobService BlobServiceResource blobService = storageAccount.GetBlobService(); #endregion this.blobService = blobService; }
public async Task BlobContainerSoftDelete() { //update storage account to v2 PatchableStorageAccountData updateParameters = new PatchableStorageAccountData() { Kind = StorageKind.StorageV2 }; await _storageAccount.UpdateAsync(updateParameters); _blobService = await _blobService.GetAsync(); BlobServiceData properties = _blobService.Data; //enable container softdelete properties.ContainerDeleteRetentionPolicy = new DeleteRetentionPolicy(); properties.ContainerDeleteRetentionPolicy.Enabled = true; properties.ContainerDeleteRetentionPolicy.Days = 30; _blobService = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, properties)).Value; //create two blob containers and delete 1 string containerName1 = Recording.GenerateAssetName("testblob1"); string containerName2 = Recording.GenerateAssetName("testblob2"); BlobContainerResource container1 = (await _blobContainerCollection.CreateOrUpdateAsync(WaitUntil.Completed, containerName1, new BlobContainerData())).Value; BlobContainerResource container2 = (await _blobContainerCollection.CreateOrUpdateAsync(WaitUntil.Completed, containerName2, new BlobContainerData())).Value; await container2.DeleteAsync(WaitUntil.Completed); //list delete included List <BlobContainerResource> blobContainers = await _blobContainerCollection.GetAllAsync(include : ListContainersInclude.Deleted).ToEnumerableAsync(); Assert.AreEqual(2, blobContainers.Count); foreach (BlobContainerResource con in blobContainers) { if (con.Data.Name == containerName1) { Assert.IsFalse(con.Data.Deleted); } else { Assert.IsTrue(con.Data.Deleted); Assert.NotNull(con.Data.RemainingRetentionDays); } } //list without delete included blobContainers = await _blobContainerCollection.GetAllAsync().ToEnumerableAsync(); Assert.AreEqual(1, blobContainers.Count); //disable container softdelete properties = _blobService.Data; properties.ContainerDeleteRetentionPolicy = new DeleteRetentionPolicy(); properties.DeleteRetentionPolicy.Enabled = false; _blobService = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, properties)).Value; properties = _blobService.Data; Assert.IsFalse(properties.ContainerDeleteRetentionPolicy.Enabled); }
public async Task ExtendImmutabilityPolicy() { //update storage account to v2 PatchableStorageAccountData updateParameters = new PatchableStorageAccountData() { Kind = StorageKind.StorageV2 }; _storageAccount = await _storageAccount.UpdateAsync(updateParameters); _blobService = await _blobService.GetAsync(); // create a blob container string containerName = Recording.GenerateAssetName("testblob"); BlobContainerData data = new BlobContainerData(); BlobContainerResource container = (await _blobContainerCollection.CreateOrUpdateAsync(WaitUntil.Completed, containerName, new BlobContainerData())).Value; //create immutability policy ImmutabilityPolicyData immutabilityPolicyModel = new ImmutabilityPolicyData() { ImmutabilityPeriodSinceCreationInDays = 3 }; ImmutabilityPolicyResource immutabilityPolicy = (await container.GetImmutabilityPolicy().CreateOrUpdateAsync(WaitUntil.Completed, data: immutabilityPolicyModel)).Value; //validate Assert.NotNull(immutabilityPolicy.Data.Id); Assert.NotNull(immutabilityPolicy.Data.ResourceType); Assert.NotNull(immutabilityPolicy.Data.Name); Assert.AreEqual(3, immutabilityPolicy.Data.ImmutabilityPeriodSinceCreationInDays); Assert.AreEqual(ImmutabilityPolicyState.Unlocked, immutabilityPolicy.Data.State); //lock immutability policy immutabilityPolicy = await container.GetImmutabilityPolicy().LockImmutabilityPolicyAsync(ifMatch: immutabilityPolicy.Data.Etag); Assert.NotNull(immutabilityPolicy.Data.Id); Assert.NotNull(immutabilityPolicy.Data.ResourceType); Assert.NotNull(immutabilityPolicy.Data.Name); Assert.AreEqual(3, immutabilityPolicy.Data.ImmutabilityPeriodSinceCreationInDays); Assert.AreEqual(ImmutabilityPolicyState.Locked, immutabilityPolicy.Data.State); //extend immutability policy immutabilityPolicyModel = new ImmutabilityPolicyData() { ImmutabilityPeriodSinceCreationInDays = 100 }; immutabilityPolicy = await container.GetImmutabilityPolicy().ExtendImmutabilityPolicyAsync(ifMatch: immutabilityPolicy.Data.Etag, data: immutabilityPolicyModel); Assert.NotNull(immutabilityPolicy.Data.Id); Assert.NotNull(immutabilityPolicy.Data.ResourceType); Assert.NotNull(immutabilityPolicy.Data.Name); Assert.AreEqual(100, immutabilityPolicy.Data.ImmutabilityPeriodSinceCreationInDays); Assert.AreEqual(ImmutabilityPolicyState.Locked, immutabilityPolicy.Data.State); await container.DeleteAsync(WaitUntil.Completed); }
public async Task createStorageAccountAndGetBlobContainerContainer() { _resourceGroup = await CreateResourceGroupAsync(); string accountName = await CreateValidAccountNameAsync("teststoragemgmt"); StorageAccountCollection storageAccountCollection = _resourceGroup.GetStorageAccounts(); _storageAccount = (await storageAccountCollection.CreateOrUpdateAsync(WaitUntil.Completed, accountName, GetDefaultStorageAccountParameters())).Value; _blobService = _storageAccount.GetBlobService(); _blobService = await _blobService.GetAsync(); _blobContainerCollection = _blobService.GetBlobContainers(); }
public async Task BlobContainersVLW() { //update storage account to v2 PatchableStorageAccountData updateParameters = new PatchableStorageAccountData() { Kind = StorageKind.StorageV2 }; _storageAccount = await _storageAccount.UpdateAsync(updateParameters); _blobService = await _blobService.GetAsync(); //enable blob versioning BlobServiceData properties = _blobService.Data; properties.IsVersioningEnabled = true; _blobService = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, properties)).Value; Assert.IsTrue(properties.IsVersioningEnabled); //create container with VLW string containerName1 = Recording.GenerateAssetName("testblob1"); BlobContainerData parameters1 = new BlobContainerData() { ImmutableStorageWithVersioning = new ImmutableStorageWithVersioning() { Enabled = true } }; BlobContainerResource container1 = (await _blobContainerCollection.CreateOrUpdateAsync(WaitUntil.Completed, containerName1, parameters1)).Value; Assert.IsTrue(container1.Data.ImmutableStorageWithVersioning.Enabled); Assert.IsNull(container1.Data.ImmutableStorageWithVersioning.MigrationState); //update container to enabled Immutability Policy string containerName2 = Recording.GenerateAssetName("testblob2"); BlobContainerData parameters2 = new BlobContainerData(); BlobContainerResource container2 = (await _blobContainerCollection.CreateOrUpdateAsync(WaitUntil.Completed, containerName2, parameters2)).Value; await container2.GetImmutabilityPolicy().CreateOrUpdateAsync(WaitUntil.Completed, data: new ImmutabilityPolicyData() { ImmutabilityPeriodSinceCreationInDays = 1 }); await container2.ObjectLevelWormAsync(WaitUntil.Completed); container2 = await container2.GetAsync(); Assert.IsTrue(container2.Data.ImmutableStorageWithVersioning.Enabled); Assert.AreEqual("Completed", container2.Data.ImmutableStorageWithVersioning.MigrationState); }
public async Task PITR() { //update storage account to v2 StorageAccountPatch updateParameters = new StorageAccountPatch() { Kind = StorageKind.StorageV2 }; _storageAccount = await _storageAccount.UpdateAsync(updateParameters); _blobService = await _blobService.GetAsync(); BlobServiceData properties = _blobService.Data; properties.DeleteRetentionPolicy = new DeleteRetentionPolicy(); properties.DeleteRetentionPolicy.Enabled = true; properties.DeleteRetentionPolicy.Days = 30; properties.ChangeFeed = new ChangeFeed(); properties.ChangeFeed.Enabled = true; properties.IsVersioningEnabled = true; properties.RestorePolicy = new RestorePolicyProperties(true) { Days = 5 }; _blobService = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, properties)).Value; if (Mode != RecordedTestMode.Playback) { await Task.Delay(10000); } //create restore ranges //start restore BlobRestoreContent restoreContent = new BlobRestoreContent( Recording.Now.AddSeconds(-1).ToUniversalTime(), new List <BlobRestoreRange>() { new BlobRestoreRange("", "container1/blob1"), new BlobRestoreRange("container1/blob2", "container2/blob3"), new BlobRestoreRange("container3/blob3", "") }); ArmOperation <BlobRestoreStatus> restoreOperation = await _storageAccount.RestoreBlobRangesAsync(WaitUntil.Started, restoreContent); //wait for restore completion BlobRestoreStatus restoreStatus = await restoreOperation.WaitForCompletionAsync(); Assert.IsTrue(restoreStatus.Status == BlobRestoreProgressStatus.Complete || restoreStatus.Status == BlobRestoreProgressStatus.InProgress); }
public async Task UpdateBlobService() { //validate current file service properties Assert.False(_blobService.Data.DeleteRetentionPolicy.Enabled); Assert.Null(_blobService.Data.DeleteRetentionPolicy.Days); //update delete retention policy BlobServiceData serviceData = _blobService.Data; serviceData.DeleteRetentionPolicy = new DeleteRetentionPolicy { Enabled = true, Days = 100 }; BlobServiceResource service = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, serviceData)).Value; //validate update Assert.True(service.Data.DeleteRetentionPolicy.Enabled); Assert.AreEqual(service.Data.DeleteRetentionPolicy.Days, 100); }
public async Task BlobServiceCors() { BlobServiceData properties2 = new BlobServiceData(); properties2.DeleteRetentionPolicy = new DeleteRetentionPolicy(); properties2.DeleteRetentionPolicy.Enabled = true; properties2.DeleteRetentionPolicy.Days = 300; properties2.DefaultServiceVersion = "2017-04-17"; properties2.Cors = new CorsRules(); properties2.Cors.CorsRulesValue.Add(new CorsRule(new string[] { "http://www.contoso.com", "http://www.fabrikam.com" }, new CorsRuleAllowedMethodsItem[] { CorsRuleAllowedMethodsItem.GET, CorsRuleAllowedMethodsItem.PUT }, 100, new string[] { "x-ms-meta-*" }, new string[] { "x-ms-meta-abc", "x-ms-meta-data*", "x-ms-meta-target*" } )); properties2.Cors.CorsRulesValue.Add(new CorsRule(new string[] { "*" }, new CorsRuleAllowedMethodsItem[] { CorsRuleAllowedMethodsItem.GET }, 2, new string[] { "*" }, new string[] { "*" } )); properties2.Cors.CorsRulesValue.Add(new CorsRule(new string[] { "http://www.abc23.com", "https://www.fabrikam.com/*" }, new CorsRuleAllowedMethodsItem[] { CorsRuleAllowedMethodsItem.GET, CorsRuleAllowedMethodsItem.PUT, CorsRuleAllowedMethodsItem.Post }, 2000, new string[] { "x-ms-meta-12345675754564*" }, new string[] { "x-ms-meta-abc", "x-ms-meta-data*", "x-ms-meta-target*" } )); _blobService = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, properties2)).Value; BlobServiceData properties3 = _blobService.Data; Assert.IsTrue(properties3.DeleteRetentionPolicy.Enabled); Assert.AreEqual(300, properties3.DeleteRetentionPolicy.Days); Assert.AreEqual("2017-04-17", properties3.DefaultServiceVersion); //validate CORS rules Assert.AreEqual(properties2.Cors.CorsRulesValue.Count, properties3.Cors.CorsRulesValue.Count); for (int i = 0; i < properties2.Cors.CorsRulesValue.Count; i++) { CorsRule putRule = properties2.Cors.CorsRulesValue[i]; CorsRule getRule = properties3.Cors.CorsRulesValue[i]; Assert.AreEqual(putRule.AllowedHeaders, getRule.AllowedHeaders); Assert.AreEqual(putRule.AllowedMethods, getRule.AllowedMethods); Assert.AreEqual(putRule.AllowedOrigins, getRule.AllowedOrigins); Assert.AreEqual(putRule.ExposedHeaders, getRule.ExposedHeaders); Assert.AreEqual(putRule.MaxAgeInSeconds, getRule.MaxAgeInSeconds); } _blobService = await _blobService.GetAsync(); BlobServiceData properties4 = _blobService.Data; Assert.IsTrue(properties4.DeleteRetentionPolicy.Enabled); Assert.AreEqual(300, properties4.DeleteRetentionPolicy.Days); Assert.AreEqual("2017-04-17", properties4.DefaultServiceVersion); //validate CORS rules Assert.AreEqual(properties2.Cors.CorsRulesValue.Count, properties4.Cors.CorsRulesValue.Count); for (int i = 0; i < properties2.Cors.CorsRulesValue.Count; i++) { CorsRule putRule = properties2.Cors.CorsRulesValue[i]; CorsRule getRule = properties4.Cors.CorsRulesValue[i]; Assert.AreEqual(putRule.AllowedHeaders, getRule.AllowedHeaders); Assert.AreEqual(putRule.AllowedMethods, getRule.AllowedMethods); Assert.AreEqual(putRule.AllowedOrigins, getRule.AllowedOrigins); Assert.AreEqual(putRule.ExposedHeaders, getRule.ExposedHeaders); Assert.AreEqual(putRule.MaxAgeInSeconds, getRule.MaxAgeInSeconds); } }
public async Task CreateGetDeleteObjectReplicationPolicy() { //create 2 storage accounts string accountName1 = await CreateValidAccountNameAsync("teststoragemgmt"); string accountName2 = await CreateValidAccountNameAsync("teststoragemgmt"); StorageAccountCreateParameters createParameters = GetDefaultStorageAccountParameters(kind: StorageKind.StorageV2); StorageAccountResource sourceAccount = (await _resourceGroup.GetStorageAccounts().CreateOrUpdateAsync(WaitUntil.Completed, accountName1, createParameters)).Value; StorageAccountResource destAccount = (await _resourceGroup.GetStorageAccounts().CreateOrUpdateAsync(WaitUntil.Completed, accountName2, createParameters)).Value; //update 2 accounts properties var updateparameter = new PatchableStorageAccountData { AllowCrossTenantReplication = true, EnableHttpsTrafficOnly = true }; destAccount = await destAccount.UpdateAsync(updateparameter); sourceAccount = await sourceAccount.UpdateAsync(updateparameter); BlobServiceResource blobService1 = await destAccount.GetBlobService().GetAsync(); BlobContainerCollection blobContainerCollection1 = blobService1.GetBlobContainers(); BlobServiceResource blobService2 = await destAccount.GetBlobService().GetAsync(); BlobContainerCollection blobContainerCollection2 = blobService2.GetBlobContainers(); //enable changefeed and versoning blobService1.Data.IsVersioningEnabled = true; await blobService1.CreateOrUpdateAsync(WaitUntil.Completed, blobService1.Data); //create 2 pairs of source and dest blob containers string containerName1 = Recording.GenerateAssetName("testblob1"); string containerName2 = Recording.GenerateAssetName("testblob2"); string containerName3 = Recording.GenerateAssetName("testblob3"); string containerName4 = Recording.GenerateAssetName("testblob4"); BlobContainerResource container1 = (await blobContainerCollection1.CreateOrUpdateAsync(WaitUntil.Completed, containerName1, new BlobContainerData())).Value; BlobContainerResource container2 = (await blobContainerCollection2.CreateOrUpdateAsync(WaitUntil.Completed, containerName2, new BlobContainerData())).Value; BlobContainerResource container3 = (await blobContainerCollection1.CreateOrUpdateAsync(WaitUntil.Completed, containerName3, new BlobContainerData())).Value; BlobContainerResource container4 = (await blobContainerCollection2.CreateOrUpdateAsync(WaitUntil.Completed, containerName4, new BlobContainerData())).Value; //prepare rules and policy ObjectReplicationPolicyData parameter = new ObjectReplicationPolicyData() { SourceAccount = sourceAccount.Id.Name, DestinationAccount = destAccount.Id.Name }; List <string> prefix = new List <string>(); prefix.Add("aa"); prefix.Add("bc d"); prefix.Add("123"); string minCreationTime = "2021-03-19T16:06:00Z"; List <ObjectReplicationPolicyRule> rules = new List <ObjectReplicationPolicyRule>(); parameter.Rules.Add( new ObjectReplicationPolicyRule(containerName1, containerName2) { Filters = new ObjectReplicationPolicyFilter(prefix, minCreationTime), } ); parameter.Rules.Add( new ObjectReplicationPolicyRule(containerName3, containerName4) ); //create policy ObjectReplicationPolicyCollection objectReplicationPolicyCollection = destAccount.GetObjectReplicationPolicies(); ObjectReplicationPolicyResource objectReplicationPolicy = (await objectReplicationPolicyCollection.CreateOrUpdateAsync(WaitUntil.Completed, "default", parameter)).Value; Assert.NotNull(objectReplicationPolicy); Assert.AreEqual(objectReplicationPolicy.Data.DestinationAccount, destAccount.Id.Name); Assert.AreEqual(objectReplicationPolicy.Data.SourceAccount, sourceAccount.Id.Name); //get policy List <ObjectReplicationPolicyResource> policies = await objectReplicationPolicyCollection.GetAllAsync().ToEnumerableAsync(); objectReplicationPolicy = policies[0]; Assert.NotNull(objectReplicationPolicy); Assert.AreEqual(objectReplicationPolicy.Data.DestinationAccount, destAccount.Id.Name); Assert.AreEqual(objectReplicationPolicy.Data.SourceAccount, sourceAccount.Id.Name); //delete policy await objectReplicationPolicy.DeleteAsync(WaitUntil.Completed); }
public async Task BlobServiceCors() { BlobServiceData blobServiceData = new BlobServiceData() { DeleteRetentionPolicy = new DeleteRetentionPolicy() { Enabled = true, Days = 300, }, DefaultServiceVersion = "2017-04-17", Cors = new CorsRules() { CorsRulesValue = { new CorsRule(new[] { "http://www.contoso.com", "http://www.fabrikam.com" }, new[] { CorsRuleAllowedMethodsItem.GET, CorsRuleAllowedMethodsItem.PUT }, 100, new[] { "x-ms-meta-*" }, new[] { "x-ms-meta-abc", "x-ms-meta-data*", "x-ms-meta-target*"}), new CorsRule(new[] { "*" }, new[] { CorsRuleAllowedMethodsItem.GET }, 2, new[] { "*" }, new[] { "*" }), new CorsRule(new[] { "http://www.abc23.com", "https://www.fabrikam.com/*" }, new[] { CorsRuleAllowedMethodsItem.GET, CorsRuleAllowedMethodsItem.PUT, CorsRuleAllowedMethodsItem.Post}, 2000, new[] { "x-ms-meta-12345675754564*" }, new[] { "x-ms-meta-abc", "x-ms-meta-data*", "x-ms-meta-target*"}) } } }; _blobService = (await _blobService.CreateOrUpdateAsync(WaitUntil.Completed, blobServiceData)).Value; Assert.AreEqual(blobServiceData.DeleteRetentionPolicy.Enabled, _blobService.Data.DeleteRetentionPolicy.Enabled); Assert.AreEqual(blobServiceData.DeleteRetentionPolicy.Days, _blobService.Data.DeleteRetentionPolicy.Days); Assert.AreEqual(blobServiceData.DefaultServiceVersion, _blobService.Data.DefaultServiceVersion); //validate CORS rules Assert.AreEqual(blobServiceData.Cors.CorsRulesValue.Count, _blobService.Data.Cors.CorsRulesValue.Count); for (int i = 0; i < blobServiceData.Cors.CorsRulesValue.Count; i++) { CorsRule putRule = blobServiceData.Cors.CorsRulesValue[i]; CorsRule getRule = _blobService.Data.Cors.CorsRulesValue[i]; Assert.AreEqual(putRule.AllowedHeaders, getRule.AllowedHeaders); Assert.AreEqual(putRule.AllowedMethods, getRule.AllowedMethods); Assert.AreEqual(putRule.AllowedOrigins, getRule.AllowedOrigins); Assert.AreEqual(putRule.ExposedHeaders, getRule.ExposedHeaders); Assert.AreEqual(putRule.MaxAgeInSeconds, getRule.MaxAgeInSeconds); } _blobService = await _blobService.GetAsync(); Assert.AreEqual(blobServiceData.DeleteRetentionPolicy.Enabled, _blobService.Data.DeleteRetentionPolicy.Enabled); Assert.AreEqual(blobServiceData.DeleteRetentionPolicy.Days, _blobService.Data.DeleteRetentionPolicy.Days); Assert.AreEqual(blobServiceData.DefaultServiceVersion, _blobService.Data.DefaultServiceVersion); //validate CORS rules Assert.AreEqual(blobServiceData.Cors.CorsRulesValue.Count, _blobService.Data.Cors.CorsRulesValue.Count); for (int i = 0; i < blobServiceData.Cors.CorsRulesValue.Count; i++) { CorsRule putRule = blobServiceData.Cors.CorsRulesValue[i]; CorsRule getRule = _blobService.Data.Cors.CorsRulesValue[i]; Assert.AreEqual(putRule.AllowedHeaders, getRule.AllowedHeaders); Assert.AreEqual(putRule.AllowedMethods, getRule.AllowedMethods); Assert.AreEqual(putRule.AllowedOrigins, getRule.AllowedOrigins); Assert.AreEqual(putRule.ExposedHeaders, getRule.ExposedHeaders); Assert.AreEqual(putRule.MaxAgeInSeconds, getRule.MaxAgeInSeconds); } }