/// <summary> /// Demonstrate basic Table CRUD operations. /// </summary> /// <param name="table">The sample table</param> /// <returns>A Task object</returns> private static async Task BasicDataOperationsAsync(CloudTable table) { // Create an instance of a customer entity. See the Model\CustomerEntity.cs for a description of the entity. CustomerEntity customer = new CustomerEntity("Harp", "Walter") { Email = "*****@*****.**", PhoneNumber = "425-555-0101" }; // Demonstrate how to insert the entity Console.WriteLine("Insert an Entity."); customer = await SamplesUtils.InsertOrMergeEntityAsync(table, customer); // Demonstrate how to Update the entity by changing the phone number Console.WriteLine("Update an existing Entity using the InsertOrMerge Upsert Operation."); customer.PhoneNumber = "425-555-0105"; await SamplesUtils.InsertOrMergeEntityAsync(table, customer); Console.WriteLine(); // Demonstrate how to Read the updated entity using a point query Console.WriteLine("Reading the updated Entity."); customer = await SamplesUtils.RetrieveEntityUsingPointQueryAsync(table, "Harp", "Walter"); Console.WriteLine(); // Demonstrate how to Delete an entity Console.WriteLine("Delete the entity. "); await SamplesUtils.DeleteEntityAsync(table, customer); Console.WriteLine(); }
public async Task RunSamples() { Console.WriteLine("Azure Table Storage - Advanced Samples\n"); Console.WriteLine(); string tableName = "demo" + Guid.NewGuid().ToString().Substring(0, 5); // Create or reference an existing table CloudTable table = await Common.CreateTableAsync(tableName); CloudTableClient tableClient = table.ServiceClient; try { // Demonstrate advanced functionality such as batch operations and segmented multi-entity queries await AdvancedDataOperationsAsync(table); // List tables in the storage account await TableListingOperations(tableClient); if (!SamplesUtils.IsAzureCosmosdbTable()) { // Create a SAS and try CRUD operations with the SAS. await AdvancedDataOperationsWithSasAsync(table); // Service Properties await ServicePropertiesSample(tableClient); // CORS await CorsSample(tableClient); // Service Stats await ServiceStatsSample(tableClient); // Table Acl await TableAclSample(table); // Create a SAS and try CRUD operations with the SAS and shared access policy on the table. await AdvancedDataOperationsWithSasAndSharedAccessPolicyOnTableAsync(table); } } finally { Console.WriteLine("Delete the Table? [Y]es or [N]o "); var delete = Console.ReadKey(); if (delete.ToString().ToUpper() == "Y") { // Delete the table await table.DeleteIfExistsAsync(); } } }
/// <summary> /// Demonstrate basic Table CRUD operations. /// </summary> /// <param name="table">The sample table</param> /// <returns>A Task object</returns> private static async Task BasicDataOperationsAsync(CloudTable table) { // Create an instance of a customer entity. See the Model\CustomerEntity.cs for a description of the entity. CustomerEntity customer = new CustomerEntity("Harp", "Walter") { Email = "*****@*****.**", PhoneNumber = "425-555-0101" }; Author author = new Author("fiction", "NobleHouse") { Price = 34.4, PublishedYear = 2019, Title = "Avengers : Game started" }; // Demonstrate how to insert the entity Console.WriteLine("Insert an Entity."); customer = await SamplesUtils.InsertOrMergeEntityAsync(table, customer); //// Demonstrate how to insert the entity - Authors Console.WriteLine("Insert Author Entity."); author = await SamplesUtils.InsertOrMergeEntityAsync(table, author); //Read a customer entity var storedCustomer = await SamplesUtils.RetrieveEntityUsingPointQueryAsync(table, "Harp", "Walter"); // Demonstrate how to Update the entity by changing the phone number Console.WriteLine("Update an existing Entity using the InsertOrMerge Upsert Operation."); customer.PhoneNumber = "425-555-0105"; await SamplesUtils.InsertOrMergeEntityAsync(table, customer); Console.WriteLine(); // Demonstrate how to Read the updated entity using a point query Console.WriteLine("Reading the updated Entity."); customer = await SamplesUtils.RetrieveEntityUsingPointQueryAsync(table, "Harp", "Walter"); Console.WriteLine(); //// Demonstrate how to Delete an entity //Console.WriteLine("Delete the entity. "); //await SamplesUtils.DeleteEntityAsync(table, customer); //Console.WriteLine(); }
/// <summary> /// Tests a table SAS to determine which operations it allows. /// </summary> /// <param name="sasUri">A string containing a URI with a SAS appended.</param> /// <param name="customer">The customer entity.</param> /// <returns>A Task object</returns> private static async Task TestTableSAS(string sasUri, CustomerEntity customer) { // Try performing table operations with the SAS provided. // Note that the storage account credentials are not required here; the SAS provides the necessary // authentication information on the URI. // Return a reference to the table using the SAS URI. CloudTable table = new CloudTable(new Uri(sasUri)); // Upsert (add/update) operations: insert an entity. // This operation requires both add and update permissions on the SAS. try { // Insert the new entity. customer = await SamplesUtils.InsertOrMergeEntityAsync(table, customer); Console.WriteLine("Add operation succeeded for SAS {0}", sasUri); Console.WriteLine(); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode == 403) { Console.WriteLine("Add operation failed for SAS {0}", sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } else { Console.WriteLine(e.Message); Console.ReadLine(); throw; } } // Read operation: query an entity. // This operation requires read permissions on the SAS. CustomerEntity customerRead = null; try { TableOperation retrieveOperation = TableOperation.Retrieve <CustomerEntity>(customer.PartitionKey, customer.RowKey); TableResult result = await table.ExecuteAsync(retrieveOperation); customerRead = result.Result as CustomerEntity; if (customerRead != null) { Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", customerRead.PartitionKey, customerRead.RowKey, customerRead.Email, customerRead.PhoneNumber); } Console.WriteLine("Read operation succeeded for SAS {0}", sasUri); Console.WriteLine(); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode == 403) { Console.WriteLine("Read operation failed for SAS {0}", sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } else { Console.WriteLine(e.Message); Console.ReadLine(); throw; } } // Delete operation: delete an entity. try { Console.WriteLine("Delete an entity? [Y]es or [N]o "); var delete = Console.ReadKey(); if (delete.ToString().ToUpper() == "Y") { if (customerRead != null) { await SamplesUtils.DeleteEntityAsync(table, customerRead); } Console.WriteLine("Delete operation succeeded for SAS {0}", sasUri); } else { Console.WriteLine("The operation was not done!"); } Console.WriteLine(); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode == 403) { Console.WriteLine("Delete operation failed for SAS {0}", sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } else { Console.WriteLine(e.Message); Console.ReadLine(); throw; } } Console.WriteLine(); }