【问题标题】:Fetching Windows Azure Storage tables names through python API通过 python API 获取 Windows Azure 存储表名称
【发布时间】:2015-05-01 10:38:46
【问题描述】:

我正在尝试获取我的 Windows Azure 存储帐户的所有表的名称,以便使用 table_service.delete_table('tasktable') 删除它们。

很遗憾,我没有在 Windows Azure HOWTO 中找到任何内容。我只找到了official REST API documentation 和这个blog,它们解释了我想要做什么,但它不在 python 中。

有没有什么方法可以使用 python 获取所有的名称表?

【问题讨论】:

    标签: python azure azure-storage azure-table-storage


    【解决方案1】:

    Azure 表存储在预览版中有一个新的 Python 库,可通过 pip 安装。要安装使用以下 pip 命令

    pip install azure-data-tables
    

    此 SDK 能够以 Tables 或 Cosmos 端点为目标(尽管有 known issues with Cosmos)。

    要查询所有表并随后删除,您可以使用list_tables 方法,如下所示:

    from azure.data.tables import TableServiceClient
    
    table_service_client = TableServiceClient.from_connection_string(my_conn_str)
    
    for table in table_service_client.list_tables():
        table_service_client.delete_table(table.table_name)
    

    您还可以使用query_tables 方法和OData 过滤器仅删除表的子集:

    ...
    table_filter = "TableName ne 'tableToKeep'"
    
    for table in table_service_client.query_tables(filter=table_filter):
        table_service_client.delete_table(table.table_name)
    

    (仅供参考,我是 Azure SDK for Python 团队的 Microsoft 员工)

    【讨论】:

      【解决方案2】:

      该服务已更新到 list_tables 但它可以工作。 David Makogon 的上述回答是有效的,但 query_table() 端点现在是 table_list

      from azure.cosmosdb.table.tableservice import TableService
      
      table_service = TableService(account_name='name', account_key='key')
      alltables = table_service.table_list()
      for table in alltables:
          print table.name
      

      【讨论】:

        【解决方案3】:

        您可以调用query_tables() 来枚举存储帐户中的各种表:

        from azure.storage import TableService
        
        table_service = TableService(account_name='name', account_key='key')
        alltables = table_service.query_tables()
        for table in alltables:
            print table.name
        

        你可以在tableservice.py中看到query_tables()的定义

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-12
          • 1970-01-01
          • 2018-09-15
          • 1970-01-01
          • 2023-04-02
          • 2019-10-20
          • 1970-01-01
          • 2015-06-07
          相关资源
          最近更新 更多