예제 #1
0
        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();
        }
예제 #3
0
        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();
        }
예제 #4
0
        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();
        }