0

过去,您过去可以在 Web 控制台上查看 AWS 存储网关中的磁带标签,但现在看来 AWS 已删除该选项。查看标签的唯一方法是单独选择每个磁带并单击标签选项卡。

我有超过 100 个磁带,手动执行此操作会相当耗时。有没有一个命令可以输出每个磁带条码及其相关标签的列表?

或者,我们在所有这些磁带上只使用大约 10 个标签(每个磁带一个标签),我尝试使用 Python/boto3 脚本(虽然我不是 python 大师)输出特定标签的所有磁带,可悲的是,下面的脚本不能完全工作。它会输出一些磁带,但不是全部。例如,如果我搜索标签“仓库”,它将返回大约 8 个磁带 - 这听起来不正确。因此,在 AWS 控制台中手动查看我已经手动找到了 3-4 个带有此标签的磁带,并且没有使用此代码在列表中返回:

#Import the boto3 library
import boto3
 
#Create the tape gateway client
tgw_client = boto3.client('storagegateway')
 
#Create a list of the tapes
tapes = tgw_client.list_tapes()['TapeInfos']
 
#Create a for loop to process each tape in the tapes list
for tape in tapes:
    #Set the TapeARN variable
    tape_arn = tape['TapeARN']
    #Describe the tags for the tape using the tape ARN
    tape_tag = tgw_client.list_tags_for_resource(ResourceARN = tape_arn)
    #Exception handing
    try:
        #Check to see if the first tag value matches job001
        if tape_tag['Tags'][0]['Value'] == 'Warehouse':
            #If the tag value matches job001, then print the list_tags_for_resource response for that tape
            print(tape_tag)
        #Ignore tapes which do not have the job001 tag
        else:
            pass
    except:
        pass

非常感谢您在获取磁带和标签的输出或如何修复此 python 脚本方面的任何帮助!

4

1 回答 1

1

该脚本没有返回所有结果,因为该帐户有 206 个磁带实例,并且 list_tapes 方法默认返回最大值为 100。为了处理这个问题并提取完整列表,我们需要评估 list_tapes 响应并使用 Marker 元素发出后续请求以检索下一组磁带。

下面是列出所有 Lambda 函数的参考脚本,这些函数可以轻松更新以从 Storage Gateway 中提取磁带实例。只需更新客户端和响应映射,然后添加您自己的评估逻辑。

import boto3
    
def list_functions(marker):
    try:
        client = boto3.client('lambda')
        if marker is not None:
            print("Retrieving next page...")
            response = client.list_functions(
            Marker=marker,
            )
        else:
            print("Retrieving first page..")
            response = client.list_functions(
            )
        return response
    except Exception as e: print(e)

def runtime():
    try:
        flist = []
        marker = None
        data = list_functions(marker)
        if 'NextMarker' in data:
            marker = data['NextMarker']
        else:
            marker = None
        for i in data['Functions']:
            flist.append(i['FunctionName'])
        
        while marker is not None:
            if '==' in marker:
                data = list_functions(marker)
                if 'NextMarker' in data:
                    marker = data['NextMarker']
                else:
                    marker = None

                for i in data['Functions']:
                    flist.append(i['FunctionName'])
            else:
                marker = None

        print(flist)
    except Exception as e: print(e)

runtime()

Boto3 文档供参考:https ://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/storagegateway.html#StorageGateway.Client.list_tapes

于 2022-01-20T23:58:47.633 回答