TableServiceClient Class
A client to interact with the Table Service at the account level.
This client provides operations to retrieve and configure the account properties as well as list, create and delete tables within the account. For operations relating to a specific table, a client for this entity can be retrieved using the get_table_client function.
- Inheritance
-
azure.data.tables._base_client.TablesBaseClientTableServiceClient
Constructor
TableServiceClient(endpoint: str, **kwargs: Any)
Parameters
- endpoint
- str
The URL to the table service endpoint. Any other entities included in the URL path (e.g. table) will be discarded. This URL can be optionally authenticated with a SAS token.
- credential
- AzureNamedKeyCredential or AzureSasCredential or TokenCredential
The credentials with which to authenticate. This is optional if the account URL already has a SAS token. The value can be one of AzureNamedKeyCredential (azure-core), AzureSasCredential (azure-core), or TokenCredentials from azure-identity.
- api_version
- str
The Storage API version to use for requests. Default value is '2019-02-02'. Setting to an older version may result in reduced feature compatibility.
Example:Authenticating a TableServiceClient from a Shared Access Key
from azure.data.tables import TableServiceClient
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential
# Create a SAS token to use for authentication of a client
from azure.data.tables import generate_account_sas, ResourceTypes, AccountSasPermissions
print("Account name: {}".format(self.account_name))
credential = AzureNamedKeyCredential(self.account_name, self.access_key)
sas_token = generate_account_sas(
credential,
resource_types=ResourceTypes(service=True),
permission=AccountSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
with TableServiceClient(endpoint=self.endpoint, credential=AzureSasCredential(sas_token)) as token_auth_table_service:
properties = token_auth_table_service.get_service_properties()
print("Shared Access Signature: {}".format(properties))
Authenticating a TableServiceClient from a Shared Account Key
from azure.data.tables import TableServiceClient
from azure.core.credentials import AzureNamedKeyCredential
credential = AzureNamedKeyCredential(self.account_name, self.access_key)
with TableServiceClient(endpoint=self.endpoint, credential=credential) as table_service:
properties = table_service.get_service_properties()
print("Shared Key: {}".format(properties))
Variables
- account_name
- str
The name of the Tables account.
- url
- str
The full URL to the Tables account.
Methods
| create_table |
Creates a new table under the current account. |
| create_table_if_not_exists |
Creates a new table if it does not currently exist. If the table currently exists, the current table is returned. |
| delete_table |
Deletes the table under the current account. No error will be raised if the given table is not found. |
| from_connection_string |
Create TableServiceClient from a connection string. |
| get_service_properties |
Gets the properties of an account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules. |
| get_service_stats |
Retrieves statistics related to replication for the Table service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the account. |
| get_table_client |
Get a client to interact with the specified table. The table need not already exist. |
| list_tables |
Queries tables under the given account. |
| query_tables |
Queries tables under the given account. |
| set_service_properties |
Sets properties for an account's Table service endpoint, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules. |
create_table
Creates a new table under the current account.
create_table(table_name: str, **kwargs: Any) -> azure.data.tables._table_client.TableClient
Parameters
Returns
TableClient
Return type
Exceptions
Examples
Creating a table from the TableServiceClient object
with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
try:
table_client = table_service_client.create_table(table_name="myTable")
print("Created table {}!".format(table_client.table_name))
except ResourceExistsError:
print("Table already exists")
create_table_if_not_exists
Creates a new table if it does not currently exist. If the table currently exists, the current table is returned.
create_table_if_not_exists(table_name: str, **kwargs: Any) -> azure.data.tables._table_client.TableClient
Parameters
Returns
TableClient
Return type
Exceptions
Examples
Creating a table if it doesn't exist, from the TableServiceClient object
with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
table_client = table_service_client.create_table_if_not_exists(table_name="myTable")
print("Table name: {}".format(table_client.table_name))
delete_table
Deletes the table under the current account. No error will be raised if the given table is not found.
delete_table(table_name: str, **kwargs: Any) -> None
Parameters
Returns
None
Return type
Exceptions
Examples
Deleting a table from the TableServiceClient object
with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
table_service_client.delete_table(table_name="myTable")
print("Deleted table {}!".format("myTable"))
from_connection_string
Create TableServiceClient from a connection string.
from_connection_string(conn_str: str, **kwargs: Any) -> azure.data.tables._table_service_client.TableServiceClient
Parameters
- conn_str
Returns
A Table service client.
Return type
Exceptions
Examples
Authenticating a TableServiceClient from a connection_string
from azure.data.tables import TableServiceClient
with TableServiceClient.from_connection_string(conn_str=self.connection_string) as table_service:
properties = table_service.get_service_properties()
print("Connection String: {}".format(properties))
get_service_properties
Gets the properties of an account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.
get_service_properties(**kwargs: Any) -> Dict[str, object]
Returns
Dictionary of service properties
Return type
Exceptions
get_service_stats
Retrieves statistics related to replication for the Table service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the account.
get_service_stats(**kwargs: Any) -> Dict[str, object]
Returns
Dictionary of service stats
Return type
Exceptions
get_table_client
Get a client to interact with the specified table.
The table need not already exist.
get_table_client(table_name: str, **kwargs: Any) -> azure.data.tables._table_client.TableClient
Parameters
Returns
A TableClient object.
Return type
Exceptions
list_tables
Queries tables under the given account.
list_tables(**kwargs: Any) -> azure.core.paging.ItemPaged[azure.data.tables._models.TableItem]
Parameters
- results_per_page
- int
Number of tables per page in returned ItemPaged
Returns
ItemPaged[TableItem]
Return type
Exceptions
Examples
Listing all tables in a storage account
# List all the tables in the service
list_tables = table_service.list_tables()
print("Listing tables:")
for table in list_tables:
print("\t{}".format(table.name))
query_tables
Queries tables under the given account.
query_tables(query_filter: str, **kwargs: Any) -> azure.core.paging.ItemPaged[azure.data.tables._models.TableItem]
Parameters
- results_per_page
- int
Number of tables per page in return ItemPaged
- parameters
- <xref:Dict>[str, <xref:Any>]
Dictionary for formatting query with additional, user defined parameters
Returns
ItemPaged[TableItem]
Return type
Exceptions
Examples
Querying tables in a storage account
table_name = "mytable1"
name_filter = "TableName eq '{}'".format(table_name)
queried_tables = table_service.query_tables(name_filter)
print("Queried_tables")
for table in queried_tables:
print("\t{}".format(table.name))
set_service_properties
Sets properties for an account's Table service endpoint, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.
set_service_properties(**kwargs: Any) -> None
Parameters
- analytics_logging
- TableAnalyticsLogging
Properties for analytics
- hour_metrics
- TableMetrics
Hour level metrics
- minute_metrics
- TableMetrics
Minute level metrics
- cors
- <xref:List>[TableCorsRule]
Cross-origin resource sharing rules
Returns
None
Return type
Exceptions
Feedback
Submit and view feedback for