如何使用boto3

时间:2017-07-12 11:20:35

标签: python-3.x amazon-web-services boto3 amazon-efs

我正在使用the boto3 SDK创建AWS弹性文件系统资源。

在EFS的boto3文档中(上面已链接),没有服务员(与启动EC2实例等其他操作不同)。因此,我无法调用服务员来执行,直到创建资源,并且必须自己编写。还有一堆边缘案例浮现在脑海中,我无法找到处理它们的例子。

client = # Attach credentials and create an efs boto3 client

def find_or_create_file_system(self, a_token):

    fs = self.client.create_file_system(CreationToken=a_token, PerformanceMode='generalPurpose')
    # Returns either:
    # {
    #     'OwnerId': 'string',
    #     'CreationToken': 'string',
    #     'FileSystemId': 'string',
    #     'CreationTime': datetime(2015, 1, 1),
    #     'LifeCycleState': 'creating'|'available'|'deleting'|'deleted',
    #     'Name': 'string',
    #     'NumberOfMountTargets': 123,
    #     'SizeInBytes': {
    #         'Value': 123,
    #         'Timestamp': datetime(2015, 1, 1)
    #     },
    #     'PerformanceMode': 'generalPurpose'|'maxIO'
    # }

    # Or, if an FS is available with that creation token already, the above returns 
    # an error. According to boto3 docs, the error will contain the existing fs id. 
    # Is this an error I need to manage with try/catch? What is the syntax to get 
    # the id out of the error?
    if there_is_an_error
        # EFS already exists
        if fs['LifeCycleState'] == 'creating'
            # Need to wait until it's created then return its id
        elif fs['LifeCycleState'] != 'available'
            # It is being / has been deleted.
            # What now? Is that token never usable again? Does it eventually disappear so I can reuse it? How long do I have to wait before recreating it?

    #  Wait until available
    fs_desc = self.client.describe_file_systems(FileSystemId=fs.id)

    # TODO figure out whether there's a waiter for this
    while fs_desc['FileSystems'][0]['LifeCycleState'] == 'creating':
        time.sleep(5)
        fs_desc.update() # Updates metadata
        print("EFS state: {0}".format(fs_desc['FileSystems'][0]['LifeCycleState']))

    return fs.id

问题1 我是否认为我必须自己写服务员?我可以从API的其他地方劫持/重新使用服务员,还是没有无证件的服务员?

问题2 如何捕获具有该令牌的实例已存在时发生的错误?如何从错误消息中获取id以处理该情况?

问题3 删除文件系统后是否可以重复使用令牌(即AWS最终是否已清除,或者该令牌是否仍然存在)?

我问Q3的原因是client.describe_file_systems()中没有Filter = {}选项。因此,目前,我使用包含简单唯一文本句柄的令牌来创建并稍后检索客户唯一的EFS。我可以使用随机UUID令牌,然后使用组织名称标记...但无法根据标记进行检索!!!

问题4 那循环是否健壮?即是否存在AWS将永久返回&创造'状态(会让我陷入无限循环)?

感谢您的帮助!

0 个答案:

没有答案