首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得AWS存储网关中所有磁带的标记?

如何获得AWS存储网关中所有磁带的标记?
EN

Stack Overflow用户
提问于 2022-01-20 20:51:51
回答 1查看 77关注 0票数 0

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

我有100多个磁带,手动这样做会很费时。是否有命令可以输出每个磁带条形码及其相关标签的列表?

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

代码语言:javascript
复制
#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脚本将是非常感谢的!

EN

回答 1

Stack Overflow用户

发布于 2022-01-20 23:58:47

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

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

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70793239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档