我正在尝试使用 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')