public async Task DownloadBlobText() { string data = "hello world"; //setup blob string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); var containerClient = new BlobContainerClient(ConnectionString, containerName); try { containerClient.Create(); containerClient.GetBlobClient(blobName).Upload(BinaryData.FromString(data)); #region Snippet:SampleSnippetsBlobMigration_DownloadBlobText BlobClient blobClient = containerClient.GetBlobClient(blobName); BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync(); string downloadedData = downloadResult.Content.ToString(); #endregion Assert.AreEqual(data, downloadedData); } finally { await containerClient.DeleteIfExistsAsync(); } }
public async Task EditBlobWithMetadata() { string data = "hello world"; var initialMetadata = new Dictionary <string, string> { { "fizz", "buzz" } }; string containerName = Randomize("sample-container"); var containerClient = new BlobContainerClient(ConnectionString, containerName); try { containerClient.Create(); BlobClient blobClient = containerClient.GetBlobClient(Randomize("sample-blob")); await blobClient.UploadAsync(BinaryData.FromString(data), new BlobUploadOptions { Metadata = initialMetadata }); #region Snippet:SampleSnippetsBlobMigration_EditBlobWithMetadata // download blob content and metadata BlobDownloadResult blobData = blobClient.DownloadContent(); // modify blob content string modifiedBlobContent = blobData.Content + "FizzBuzz"; // reupload modified blob content while preserving metadata // not adding metadata is a metadata clear blobClient.Upload( BinaryData.FromString(modifiedBlobContent), new BlobUploadOptions() { Metadata = blobData.Details.Metadata }); #endregion var actualMetadata = (await blobClient.GetPropertiesAsync()).Value.Metadata; Assert.AreEqual(initialMetadata.Count, actualMetadata.Count); foreach (var expectedKvp in initialMetadata) { Assert.IsTrue(actualMetadata.TryGetValue(expectedKvp.Key, out var actualValue)); Assert.AreEqual(expectedKvp.Value, actualValue); } } finally { await containerClient.DeleteIfExistsAsync(); } }
public async Task ClaimCheck() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false)) { #region Snippet:CreateBlobContainer #if SNIPPET var containerClient = new BlobContainerClient("<storage connection string>", "claim-checks"); #else var containerClient = new BlobContainerClient(TestEnvironment.StorageClaimCheckConnectionString, "claim-checks"); #endif await containerClient.CreateIfNotExistsAsync(); #endregion try { #region Snippet:UploadMessage byte[] body = ServiceBusTestUtilities.GetRandomBuffer(1000000); string blobName = Guid.NewGuid().ToString(); await containerClient.UploadBlobAsync(blobName, new BinaryData(body)); var message = new ServiceBusMessage { ApplicationProperties = { ["blob-name"] = blobName } }; #endregion #region Snippet:ClaimCheckSendMessage #if SNIPPET var client = new ServiceBusClient("<service bus connection string>"); #else var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString); #endif ServiceBusSender sender = client.CreateSender(scope.QueueName); await sender.SendMessageAsync(message); #endregion #region Snippet:ReceiveClaimCheck ServiceBusReceiver receiver = client.CreateReceiver(scope.QueueName); ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync(); if (receivedMessage.ApplicationProperties.TryGetValue("blob-name", out object blobNameReceived)) { #if SNIPPET var blobClient = new BlobClient("<storage connection string>", "claim-checks", (string)blobNameReceived); #else var blobClient = new BlobClient( TestEnvironment.StorageClaimCheckConnectionString, "claim-checks", (string)blobNameReceived); #endif BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync(); BinaryData messageBody = downloadResult.Content; // Once we determine that we are done with the message, we complete it and delete the corresponding blob. await receiver.CompleteMessageAsync(receivedMessage); await blobClient.DeleteAsync(); #if !SNIPPET Assert.AreEqual(body, messageBody.ToArray()); #endif } #endregion } finally { await containerClient.DeleteAsync(); } } }
/// <summary> /// Tests a blob SAS to determine which operations it allows. /// </summary> /// <param name="sasUri">A string containing a URI with a SAS appended.</param> /// <param name="blobContent">A string content content to write to the blob.</param> static void TestBlobSAS(Uri sasUri, string blobContent) { //Try performing blob operations using the SAS provided. //Return a reference to the blob using the SAS URI. BlobClient blob = new BlobClient(sasUri); //Create operation: Upload a blob with the specified name to the container. //If the blob does not exist, it will be created. If it does exist, it will be overwritten. try { //string blobContent = "This blob was created with a shared access signature granting write permissions to the blob. "; blob.Upload(BinaryData.FromString(blobContent)); Console.WriteLine("Create operation succeeded for SAS " + sasUri); Console.WriteLine(); } catch (RequestFailedException e) { Console.WriteLine("Create operation failed for SAS " + sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } // Write operation: Add metadata to the blob try { IDictionary <string, string> metadata = new Dictionary <string, string>(); metadata.Add("name", "value"); blob.SetMetadata(metadata); Console.WriteLine("Write operation succeeded for SAS " + sasUri); Console.WriteLine(); } catch (RequestFailedException e) { Console.WriteLine("Write operation failed for SAS " + sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } //Read operation: Read the contents of the blob. try { BlobDownloadResult download = blob.DownloadContent(); string content = download.Content.ToString(); Console.WriteLine(content); Console.WriteLine(); Console.WriteLine("Read operation succeeded for SAS " + sasUri); Console.WriteLine(); } catch (RequestFailedException e) { Console.WriteLine("Read operation failed for SAS " + sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } //Delete operation: Delete the blob. try { blob.Delete(); Console.WriteLine("Delete operation succeeded for SAS " + sasUri); Console.WriteLine(); } catch (RequestFailedException e) { Console.WriteLine("Delete operation failed for SAS " + sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } }