【问题标题】:How to describe snapshots by OwnerIds and Filters using boto3如何使用 boto3 按 OwnerIds 和 Filters 描述快照
【发布时间】:2019-09-04 09:23:45
【问题描述】:

如何同时描述我拥有的快照和标签过滤?

它用下面的代码描述了我拥有的快照:

import boto3
region_src = 'us-east-1'
client_src = boto3.client('ec2', region_name=region_src)

def get_snapshots_src():
    response = client_src.describe_snapshots(OwnerIds=['012345678900'])
    return response["Snapshots"]

snap = get_snapshots_src()
print(*snap, sep="\n")

但是当我添加“过滤器”时,它开始忽略“OwnerIds”并仅按标签过滤。

client_src.describe_snapshots(OwnerIds=['012345678900'],Filters=[{'Name': 'tag:Disaster_Recovery', 'Values': ['Full']}])

我正在关注官方 boto3 文档: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_snapshots

【问题讨论】:

    标签: python amazon-web-services aws-lambda boto3 snapshot


    【解决方案1】:

    我认为 Filters 和 OwnerIds 选项是分开工作的。我希望 OwnerIds 选项是 Owner-id 的 Filters 选项的缩写,因为我也得到了 OwnerIds 选项被忽略的结果。

    因此,您可以使用过滤器选项,例如

        Filters=[
            {
                'Name': 'tag:key',
                'Values': [
                    'value'
                ]
            },
            {
                'Name': 'owner-id',
                'Values': [
                    'value'
                ]
            }
        ]
    

    它会和我一样好用。


    Filters (list) --
    The filters.
    
    description - A description of the snapshot.
    encrypted - Indicates whether the snapshot is encrypted (true | false )
    owner-alias - Value from an Amazon-maintained list (amazon | self | all | aws-marketplace | microsoft ) of snapshot owners. Not to be confused with the user-configured AWS account alias, which is set from the IAM console.
    owner-id - The ID of the AWS account that owns the snapshot.
    progress - The progress of the snapshot, as a percentage (for example, 80%).
    snapshot-id - The snapshot ID.
    start-time - The time stamp when the snapshot was initiated.
    status - The status of the snapshot (pending | completed | error ).
    tag :<key> - The key/value combination of a tag assigned to the resource. Use the tag key in the filter name and the tag value as the filter value. For example, to find all resources that have a tag with the key Owner and the value TeamA , specify tag:Owner for the filter name and TeamA for the filter value.
    tag-key - The key of a tag assigned to the resource. Use this filter to find all resources assigned a tag with a specific key, regardless of the tag value.
    volume-id - The ID of the volume the snapshot is for.
    volume-size - The size of the volume, in GiB.
    (dict) --
    A filter name and value pair that is used to return a more specific list of results from a describe operation. Filters can be used to match a set of resources by specific criteria, such as tags, attributes, or IDs. The filters supported by a describe operation are documented with the describe operation.
    

    【讨论】:

    • 您的解决方案有效,但我不明白它如何使用破折号 (-) 找到“所有者 ID”,在描述中,它是 OwnerId。
    • @OrestGulman 如果你仔细看过滤器部分的描述,你会注意到红色字符和owner-id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2020-02-02
    相关资源
    最近更新 更多