0

我正在尝试使用 boto3 和 ResourceGroupsTaggingAPI 下载所有带有特定标签的 ARN。当我应该获得超过 80 个 ARN 时,我正在过滤两个标签并获得 8 个不同的 ARN。

**编辑:**我发现这是因为 API 限制。根据文档,API 允许您从当前使用的区域获取标签。有没有办法从所有地区获取标签?

import boto3
import csv


class TagEditor:
    
    def writeToCsv(self,writer, tag_list):
        for resource in tag_list:
            for tag in resource['Tags']:
                row = dict(
                    ResourceArn=resource['ResourceARN'], TagKey=tag['Key'], TagValue=tag['Value'])
                writer.writerow(row)
            
    
    def main(self):
        
        field_names = ['ResourceArn', 'TagKey', 'TagValue']
        s3 = boto3.client('s3')
        restag = boto3.client('resourcegroupstaggingapi')
        
        
        with open('/tmp/tags.csv', 'w') as csvfile:
            writer = csv.DictWriter(csvfile, quoting=csv.QUOTE_ALL,
                                    delimiter=',', dialect='excel', fieldnames=field_names)
            writer.writeheader()
            
            # Filter for tag1
            response_deployment = restag.get_resources(TagFilters=[{'Key':'tag1'}],ResourcesPerPage=100)
            self.writeToCsv(writer, response_deployment['ResourceTagMappingList'])
            
            while 'PaginationToken' in response_deployment and response_deployment['PaginationToken']:
                token = response_deployment['PaginationToken']
                response_deployment = restag.get_resources(TagFilters=[{'Key':'tag1'}], ResourcesPerPage=100, PaginationToken=token)
                self.writeToCsv(writer, response_deployment['ResourceTagMappingList'])
    
            # Filter for tag2
            response_deployment = restag.get_resources(TagFilters=[{'Key':'tag2'}],ResourcesPerPage=100)
            self.writeToCsv(writer, response_deployment['ResourceTagMappingList'])
            
            while 'PaginationToken' in response_deployment and response_deployment['PaginationToken']:
                token = response_deployment['PaginationToken']
                response_deployment = restag.get_resources(TagFilters=[{'Key':'tag2'}], ResourcesPerPage=100, PaginationToken=token)
                self.writeToCsv(writer, response_deployment['ResourceTagMappingList'])
        
        s3.upload_file('/tmp/tags.csv', Bucket = 'my-bucket', Key = 'tags.csv')
4

1 回答 1

0

那是因为您在第一次通话中使用了 TagFilters,而在随后的分页通话中错过了它们

分页调用必须包含相同的参数集,包括后续调用的分页标记

包含TagFilters=[{'Key':'tag1'}]在带有分页标记的循环内的 get_resources 调用中

遍历每个区域并为所有区域调用 get_resources

# List all regions
ec2_client = boto3.client('ec2')
regions = [region['RegionName'] for region in ec2_client.describe_regions()['Regions']]
于 2021-11-23T16:41:26.537 回答