/// <inheritdoc />
        public async Task InitializeDataStoreAsync(TableServiceClient tableServiceClient)
        {
            EnsureArg.IsNotNull(tableServiceClient, nameof(tableServiceClient));

            try
            {
                _logger.LogInformation("Initializing Table Storage and tables");
                foreach (string tableName in Constants.AllTables)
                {
                    if (await tableServiceClient.CreateTableIfNotExistsAsync(tableName) != null)
                    {
                        _logger.LogInformation("Created Table named '{TableName}'", tableName);
                    }
                    else
                    {
                        _logger.LogInformation("Table '{TableName}' already exists", tableName);
                    }
                }

                _logger.LogInformation("Table Storage and tables successfully initialized");
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, "Table Storage and table initialization failed");
                throw;
            }
        }
예제 #2
0
        public async Task SasAuth()
        {
            string storageUri  = StorageUri;
            string accountName = StorageAccountName;
            string accountKey  = PrimaryStorageAccountKey;
            string tableName   = "OfficeSupplies";

            #region Snippet:TablesAuthSas
            // Construct a new <see cref="TableServiceClient" /> using a <see cref="TableSharedKeyCredential" />.

            var credential = new TableSharedKeyCredential(accountName, accountKey);

            var serviceClient = new TableServiceClient(
                new Uri(storageUri),
                credential);

            // Build a shared access signature with the Write and Delete permissions and access to all service resource types.

            TableAccountSasBuilder sasWriteDelete = serviceClient.GetSasBuilder(TableAccountSasPermissions.Write | TableAccountSasPermissions.Delete, TableAccountSasResourceTypes.All, new DateTime(2040, 1, 1, 1, 1, 0, DateTimeKind.Utc));
            string tokenWriteDelete = sasWriteDelete.Sign(credential);

            // Create the TableServiceClients using the SAS URIs.

            var serviceClientWithSas = new TableServiceClient(new Uri(storageUri), new AzureSasCredential(tokenWriteDelete));

            // Validate that we are able to create a table using the SAS URI with Write and Delete permissions.

            await serviceClientWithSas.CreateTableIfNotExistsAsync(tableName);

            // Validate that we are able to delete a table using the SAS URI with Write and Delete permissions.

            await serviceClientWithSas.DeleteTableAsync(tableName);

            #endregion
        }
            async Task <TableClient> InitializeAndCacheTableAsync(string tableName, CancellationToken cancellationToken)
            {
                try
                {
                    if (_client == null)
                    {
                        throw new InvalidOperationException("CloudTableClient has not been initialized");
                    }

                    InternalLogger.Debug("AzureDataTablesTarget: Initializing table: {0}", tableName);

                    var tableExists = await _client.CreateTableIfNotExistsAsync(tableName, cancellationToken).ConfigureAwait(false);

                    var table = _client.GetTableClient(tableName);

                    if (tableExists != null)
                    {
                        InternalLogger.Debug("AzureDataTablesTarget: Created new table: {0}", tableName);
                    }
                    else
                    {
                        InternalLogger.Debug("AzureDataTablesTarget: Opened existing table: {0}", tableName);
                    }

                    _table = table;
                    return(table);
                }
                catch (Exception exception)
                {
                    InternalLogger.Error(exception, "AzureDataTablesTarget: Failed to initialize table={0}", tableName);
                    throw;
                }
            }
예제 #4
0
        private async Task <TableClient> RetrieveTable()
        {
            var serviceClient = new TableServiceClient(connectionString);

            await serviceClient.CreateTableIfNotExistsAsync(TableName);

            var tableClient = serviceClient.GetTableClient(TableName);

            return(tableClient);
        }
        /// <inheritdoc/>
        public async Task PerformTestAsync(CancellationToken cancellationToken = default)
        {
            await _testServiceClient.CreateTableIfNotExistsAsync(TestTable, cancellationToken : cancellationToken);

            var tableClient = _testServiceClient.GetTableClient(TestTable);
            var entity      = new HealthEntity(TestPartitionKey, TestRowKey)
            {
                Data = TestData
            };

            await tableClient.UpsertEntityAsync(entity, cancellationToken : cancellationToken);

            await tableClient.GetEntityAsync <HealthEntity>(TestPartitionKey, TestRowKey, cancellationToken : cancellationToken);

            await tableClient.DeleteEntityAsync(TestPartitionKey, TestRowKey, cancellationToken : cancellationToken);

            await _testServiceClient.DeleteTableAsync(TestTable, cancellationToken);
        }
        public async Task <TableClient> GetTableAsync(string tablename)
        {
            Console.Write("Checking storage...");

            var serviceClient = new TableServiceClient(_storageConnectionString);

            var result = await serviceClient.CreateTableIfNotExistsAsync(tablename);

            if (result != null)
            {
                Console.WriteLine("table '{0}' created", tablename);
            }
            else
            {
                Console.WriteLine("table '{0}' exists", tablename);
            }

            var table = serviceClient.GetTableClient(tablename);



            return(table);
        }
 public async Task Create <T>()
 => await serviceClient.CreateTableIfNotExistsAsync(typeof(T).Name);