Python 3和Azure表存储表存储帐户不工作

2024-10-01 00:25:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Microsoft提供的示例,使用Python连接到Azure存储表。由于找不到TableStorage Account,下面的代码失败。我缺少的是我安装了azure软件包,但仍然抱怨找不到它

import azure.common
from azure.storage import CloudStorageAccount
from tablestorageaccount import TableStorageAccount

print('Azure Table Storage samples for Python')

# Create the storage account object and specify its credentials 
# to either point to the local Emulator or your Azure subscription
if IS_EMULATED:
    account = TableStorageAccount(is_emulated=True)
else:
    account_connection_string = STORAGE_CONNECTION_STRING
    # Split into key=value pairs removing empties, then split the pairs into a dict
    config = dict(s.split('=', 1) for s in account_connection_string.split(';') if s)

    # Authentication
    account_name = config.get('AccountName')
    account_key = config.get('AccountKey')
    # Basic URL Configuration
    endpoint_suffix = config.get('EndpointSuffix')
    if endpoint_suffix == None:
       table_endpoint  = config.get('TableEndpoint')
       table_prefix = '.table.'
       start_index = table_endpoint.find(table_prefix)
       end_index = table_endpoint.endswith(':') and len(table_endpoint) or table_endpoint.rfind(':')
       endpoint_suffix = table_endpoint[start_index+len(table_prefix):end_index]
    account = TableStorageAccount(account_name = account_name, connection_string = account_connection_string, endpoint_suffix=endpoint_suffix)

Tags: theimportconfiggetstringindexiftable
1条回答
网友
1楼 · 发布于 2024-10-01 00:25:08

我找到了源示例代码,在示例代码中仍然有一个自定义模块tablestorageaccount.py,它只是用来返回TableService。如果您已经有了存储连接字符串并希望进行测试,则可以直接连接到表

样本:

from azure.storage.table import TableService, Entity

account_connection_string = 'DefaultEndpointsProtocol=https;AccountName=account name;AccountKey=account key;EndpointSuffix=core.windows.net'

tableservice=TableService(connection_string=account_connection_string)

enter image description here

您还可以参考新的sdk连接表。这是关于Get started with Azure Table storage的官方教程

相关问题 更多 >