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, *, credential: AzureSasCredential | AzureNamedKeyCredential | TokenCredential | None = None, api_version: str | None = None, **kwargs: Any)
Parameters
Name | Description |
---|---|
endpoint
Required
|
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. |
endpoint
Required
|
A URL to an Azure Tables account. |
Keyword-Only Parameters
Name | Description |
---|---|
credential
|
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 a TokenCredential implementation from azure-identity. |
api_version
|
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. |
credential
|
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 a TokenCredential implementation from azure-identity. |
api_version
|
Specifies the version of the operation to use for this request. Default value is "2019-02-02". |
Examples
Authenticating a TableServiceClient from a Shared Access Key
from datetime import datetime, timedelta
from azure.data.tables import TableServiceClient, generate_account_sas, ResourceTypes, AccountSasPermissions
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential
credential = AzureNamedKeyCredential(self.account_name, self.access_key)
# Create a SAS token to use for authentication of a client
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 table_service_client:
properties = table_service_client.get_service_properties()
print(f"{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_client:
properties = table_service_client.get_service_properties()
print(f"{properties}")
Variables
Name | Description |
---|---|
account_name
|
The name of the Tables account. |
url
|
The full URL to the Tables account. |
Methods
close |
This method is to close the sockets opened by the client. It need not be used when using with a context manager. |
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. |
close
This method is to close the sockets opened by the client. It need not be used when using with a context manager.
close() -> None
create_table
Creates a new table under the current account.
create_table(table_name: str, **kwargs) -> TableClient
Parameters
Name | Description |
---|---|
table_name
Required
|
The Table name. |
Returns
Type | Description |
---|---|
TableClient |
Exceptions
Type | Description |
---|---|
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=self.table_name)
print(f"Created table {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) -> TableClient
Parameters
Name | Description |
---|---|
table_name
Required
|
The Table name. |
Returns
Type | Description |
---|---|
TableClient |
Exceptions
Type | Description |
---|---|
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=self.table_name)
print(f"Table name: {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) -> None
Parameters
Name | Description |
---|---|
table_name
Required
|
The Table name. |
Returns
Type | Description |
---|---|
None |
Exceptions
Type | Description |
---|---|
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=self.table_name)
print(f"Deleted table {self.table_name}!")
from_connection_string
Create TableServiceClient from a connection string.
from_connection_string(conn_str: str, **kwargs: Any) -> TableServiceClient
Parameters
Name | Description |
---|---|
conn_str
Required
|
A connection string to an Azure Storage or Cosmos account. |
Returns
Type | Description |
---|---|
A Table service client. |
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_client:
properties = table_service_client.get_service_properties()
print(f"{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) -> Dict[str, object]
Returns
Type | Description |
---|---|
Dictionary of service properties |
Exceptions
Type | Description |
---|---|
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) -> Dict[str, object]
Returns
Type | Description |
---|---|
Dictionary of service stats |
Exceptions
Type | Description |
---|---|
azure.core.exceptions.HttpResponseError:
|
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) -> TableClient
Parameters
Name | Description |
---|---|
table_name
Required
|
The table name |
Returns
Type | Description |
---|---|
A TableClient object. |
list_tables
Queries tables under the given account.
list_tables(*, results_per_page: int | None = None, **kwargs) -> ItemPaged[TableItem]
Keyword-Only Parameters
Name | Description |
---|---|
results_per_page
|
Number of tables per page in returned ItemPaged |
Returns
Type | Description |
---|---|
An iterator of TableItem |
Exceptions
Type | Description |
---|---|
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(f"\t{table.name}")
query_tables
Queries tables under the given account.
query_tables(query_filter: str, *, results_per_page: int | None = None, parameters: Dict[str, Any] | None = None, **kwargs) -> ItemPaged[TableItem]
Parameters
Name | Description |
---|---|
query_filter
Required
|
Specify a filter to return certain tables. |
Keyword-Only Parameters
Name | Description |
---|---|
results_per_page
|
Number of tables per page in return ItemPaged |
parameters
|
Dictionary for formatting query with additional, user defined parameters |
Returns
Type | Description |
---|---|
An iterator of TableItem |
Exceptions
Type | Description |
---|---|
Examples
Querying tables in a storage account
table_name = "mytable1"
name_filter = f"TableName eq '{table_name}'"
queried_tables = table_service.query_tables(name_filter)
print("Queried_tables")
for table in queried_tables:
print(f"\t{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(*, analytics_logging: TableAnalyticsLogging | None = None, hour_metrics: TableMetrics | None = None, minute_metrics: TableMetrics | None = None, cors: List[TableCorsRule] | None = None, **kwargs) -> None
Keyword-Only Parameters
Name | Description |
---|---|
analytics_logging
|
Properties for analytics |
hour_metrics
|
Hour level metrics |
minute_metrics
|
Minute level metrics |
cors
|
Cross-origin resource sharing rules |
Returns
Type | Description |
---|---|
None |
Exceptions
Type | Description |
---|---|
Attributes
api_version
The version of the Storage API used for requests.
Returns
Type | Description |
---|---|
The Storage API version. |
url
The full endpoint URL to this entity, including SAS token if used.
This could be either the primary endpoint, or the secondary endpoint depending on the current <xref:azure.data.tables.location_mode>.
Returns
Type | Description |
---|---|
The full endpoint URL including SAS token if used. |
Azure SDK for Python