public async Task createStorageAccountAndGetBlobContainerContainer() { ArmClient armClient = new ArmClient(new DefaultAzureCredential()); Subscription subscription = armClient.DefaultSubscription; string rgName = "myRgName"; Location location = Location.WestUS2; ResourceGroupCreateOrUpdateOperation operation = await subscription.GetResourceGroups().CreateOrUpdateAsync(rgName, new ResourceGroupData(location)); ResourceGroup resourceGroup = operation.Value; this.resourceGroup = resourceGroup; Sku sku = new Sku(SkuName.StandardGRS); Kind kind = Kind.Storage; string locationStr = "westus2"; StorageAccountCreateParameters parameters = new StorageAccountCreateParameters(sku, kind, locationStr); StorageAccountContainer accountContainer = resourceGroup.GetStorageAccounts(); string accountName = "myAccount"; StorageAccountCreateOperation accountCreateOperation = await accountContainer.CreateOrUpdateAsync(accountName, parameters); storageAccount = await accountCreateOperation.WaitForCompletionAsync(); #region Snippet:Managing_BlobContainers_GetBlobService BlobServiceContainer blobServiceContainer = storageAccount.GetBlobServices(); BlobService blobService = await blobServiceContainer.GetAsync("default"); #endregion this.blobService = blobService; }
public async Task createStorageAccountAndGetBlobContainerContainer() { _resourceGroup = await CreateResourceGroupAsync(); string accountName = await CreateValidAccountNameAsync("teststoragemgmt"); StorageAccountContainer storageAccountContainer = _resourceGroup.GetStorageAccounts(); _storageAccount = (await storageAccountContainer.CreateOrUpdateAsync(accountName, GetDefaultStorageAccountParameters())).Value; _blobServiceContainer = _storageAccount.GetBlobServices(); _blobService = await _blobServiceContainer.GetAsync("default"); _blobContainerContainer = _blobService.GetBlobContainers(); }
public async Task CreateGetDeleteObjectReplicationPolicy() { //create 2 storage accounts string accountName1 = await CreateValidAccountNameAsync("teststoragemgmt"); string accountName2 = await CreateValidAccountNameAsync("teststoragemgmt"); StorageAccountCreateParameters createParameters = GetDefaultStorageAccountParameters(kind: Kind.StorageV2); StorageAccount sourceAccount = (await _resourceGroup.GetStorageAccounts().CreateOrUpdateAsync(accountName1, createParameters)).Value; StorageAccount destAccount = (await _resourceGroup.GetStorageAccounts().CreateOrUpdateAsync(accountName2, createParameters)).Value; //update 2 accounts properties var updateparameter = new StorageAccountUpdateParameters { AllowCrossTenantReplication = true, EnableHttpsTrafficOnly = true }; destAccount = await destAccount.UpdateAsync(updateparameter); sourceAccount = await sourceAccount.UpdateAsync(updateparameter); BlobService blobService1 = await destAccount.GetBlobServices().GetAsync("default"); BlobContainerContainer blobContainerContainer1 = blobService1.GetBlobContainers(); BlobService blobService2 = await destAccount.GetBlobServices().GetAsync("default"); BlobContainerContainer blobContainerContainer2 = blobService2.GetBlobContainers(); //enable changefeed and versoning blobService1.Data.IsVersioningEnabled = true; await blobService1.SetServicePropertiesAsync(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"); BlobContainer container1 = (await blobContainerContainer1.CreateOrUpdateAsync(containerName1, new BlobContainerData())).Value; BlobContainer container2 = (await blobContainerContainer2.CreateOrUpdateAsync(containerName2, new BlobContainerData())).Value; BlobContainer container3 = (await blobContainerContainer1.CreateOrUpdateAsync(containerName3, new BlobContainerData())).Value; BlobContainer container4 = (await blobContainerContainer2.CreateOrUpdateAsync(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 ObjectReplicationPolicyContainer objectReplicationPolicyContainer = destAccount.GetObjectReplicationPolicies(); ObjectReplicationPolicy objectReplicationPolicy = (await objectReplicationPolicyContainer.CreateOrUpdateAsync("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 <ObjectReplicationPolicy> policies = await objectReplicationPolicyContainer.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(); }
public async Task SetGetDeleteManagementPolicy() { //create resource group and storage account string accountName = await CreateValidAccountNameAsync(namePrefix); _resourceGroup = await CreateResourceGroupAsync(); StorageAccountContainer storageAccountContainer = _resourceGroup.GetStorageAccounts(); StorageAccountCreateParameters parameters = GetDefaultStorageAccountParameters(kind: Kind.StorageV2); StorageAccount account = (await storageAccountContainer.CreateOrUpdateAsync(accountName, parameters)).Value; ManagementPolicyContainer managementPolicyContainer = account.GetManagementPolicies(); //Enable LAT BlobServiceContainer blobServiceContainer = account.GetBlobServices(); BlobService blobService = await blobServiceContainer.GetAsync("Default"); blobService.Data.LastAccessTimeTrackingPolicy = new LastAccessTimeTrackingPolicy(true); _ = await blobService.SetServicePropertiesAsync(blobService.Data); // create ManagementPolicy to set, the type of policy rule should always be Lifecycle List <ManagementPolicyRule> rules = new List <ManagementPolicyRule>(); ManagementPolicyAction action = new ManagementPolicyAction() { BaseBlob = new ManagementPolicyBaseBlob() { Delete = new DateAfterModification(1000, null) } }; ManagementPolicyDefinition definition1 = new ManagementPolicyDefinition(action) { Filters = new ManagementPolicyFilter(blobTypes: new List <string>() { "blockBlob", "appendBlob" }), }; ManagementPolicyRule rule1 = new ManagementPolicyRule("rule1", "Lifecycle", definition1); rules.Add(rule1); ManagementPolicyDefinition definition2 = new ManagementPolicyDefinition(action) { Filters = new ManagementPolicyFilter(blobTypes: new List <string>() { "appendBlob" }), }; ManagementPolicyRule rule2 = new ManagementPolicyRule("rule2", "Lifecycle", definition2); rules.Add(rule2); ManagementPolicyDefinition definition3 = new ManagementPolicyDefinition(action) { Filters = new ManagementPolicyFilter(blobTypes: new List <string>() { "blockBlob" }), }; ManagementPolicyRule rule3 = new ManagementPolicyRule("rule3", "Lifecycle", definition3); rules.Add(rule3); ManagementPolicyData parameter = new ManagementPolicyData() { Policy = new ManagementPolicySchema(rules) }; //set management policy, the policy name should always be default ManagementPolicy managementPolicy = (await managementPolicyContainer.CreateOrUpdateAsync("default", parameter)).Value; Assert.NotNull(managementPolicy); Assert.AreEqual(managementPolicy.Data.Policy.Rules.Count, 3); //delete namagement policy await managementPolicy.DeleteAsync(); }
public async Task CreateUpdateGetDeleteBlobInventoryPolicy() { //create resource group and storage account string accountName = await CreateValidAccountNameAsync(namePrefix); _resourceGroup = await CreateResourceGroupAsync(); StorageAccountContainer storageAccountContainer = _resourceGroup.GetStorageAccounts(); StorageAccountCreateParameters parameters = GetDefaultStorageAccountParameters(kind: Kind.StorageV2); StorageAccount account = (await storageAccountContainer.CreateOrUpdateAsync(accountName, parameters)).Value; BlobInventoryPolicyContainer blobInventoryPolicyContainer = account.GetBlobInventoryPolicies(); //create a blob container string containerName = Recording.GenerateAssetName("testblob"); BlobContainerData data = new BlobContainerData(); BlobServiceContainer blobServiceContainer = account.GetBlobServices(); BlobService blobService = await blobServiceContainer.GetAsync("default"); BlobContainerContainer blobContainerContainer = blobService.GetBlobContainers(); BlobContainer container = (await blobContainerContainer.CreateOrUpdateAsync(containerName, new BlobContainerData())).Value; //prepare schema fields string[] BlobSchemaField = new string[] { "Name", "Creation-Time", "Last-Modified", "Content-Length", "Content-MD5", "BlobType", "AccessTier", "AccessTierChangeTime", "Snapshot", "VersionId", "IsCurrentVersion", "Metadata", "LastAccessTime" }; string[] ContainerSchemaField = new string[] { "Name", "Last-Modified", "Metadata", "LeaseStatus", "LeaseState", "LeaseDuration", "PublicAccess", "HasImmutabilityPolicy", "HasLegalHold" }; List <string> blobSchemaFields1 = new List <string>(BlobSchemaField); List <string> blobSchemaFields2 = new List <string>(); blobSchemaFields2.Add("Name"); List <string> containerSchemaFields1 = new List <string>(ContainerSchemaField); List <string> containerSchemaFields2 = new List <string>(); containerSchemaFields2.Add("Name"); // prepare policy objects,the type of policy rule should always be Inventory List <BlobInventoryPolicyRule> ruleList = new List <BlobInventoryPolicyRule>(); BlobInventoryPolicyRule rule1 = new BlobInventoryPolicyRule(true, "rule1", containerName, new BlobInventoryPolicyDefinition( filters: new BlobInventoryPolicyFilter( blobTypes: new List <string>(new string[] { "blockBlob" }), prefixMatch: new List <string>(new string[] { "prefix1", "prefix2" }), includeBlobVersions: true, includeSnapshots: true), format: Format.Csv, schedule: Schedule.Weekly, objectType: ObjectType.Blob, schemaFields: blobSchemaFields1)); BlobInventoryPolicyRule rule2 = new BlobInventoryPolicyRule(true, "rule2", containerName, new BlobInventoryPolicyDefinition( format: Format.Csv, schedule: Schedule.Daily, objectType: ObjectType.Container, schemaFields: containerSchemaFields1)); BlobInventoryPolicyRule rule3 = new BlobInventoryPolicyRule(true, "rule3", containerName, new BlobInventoryPolicyDefinition( format: Format.Parquet, schedule: Schedule.Weekly, objectType: ObjectType.Container, schemaFields: containerSchemaFields2)); ruleList.Add(rule1); ruleList.Add(rule2); BlobInventoryPolicySchema policy = new BlobInventoryPolicySchema(true, "Inventory", ruleList); BlobInventoryPolicyData parameter = new BlobInventoryPolicyData() { Policy = policy }; //create and get policy, the name of blob inventory policy should always be default BlobInventoryPolicy blobInventoryPolicy = (await blobInventoryPolicyContainer.CreateOrUpdateAsync("default", parameter)).Value; blobInventoryPolicy = await blobInventoryPolicyContainer.GetAsync("default"); Assert.AreEqual(blobInventoryPolicy.Data.Policy.Rules.Count, 2); //update policy ruleList.Add(rule3); BlobInventoryPolicySchema policy2 = new BlobInventoryPolicySchema(true, "Inventory", ruleList); BlobInventoryPolicyData parameter2 = new BlobInventoryPolicyData() { Policy = policy2 }; blobInventoryPolicy = (await blobInventoryPolicyContainer.CreateOrUpdateAsync("default", parameter2)).Value; Assert.AreEqual(blobInventoryPolicy.Data.Policy.Rules.Count, 3); //delete policy await blobInventoryPolicy.DeleteAsync(); }