public void Errors() { // Get a connection string to our Azure Storage account. string connectionString = ConnectionString; // Get a reference to a container named "sample-container" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, Randomize("sample-container")); container.Create(); try { // Try to create the container again container.Create(); } catch (RequestFailedException ex) when(ex.ErrorCode == BlobErrorCode.ContainerAlreadyExists) { // Ignore any errors if the container already exists } catch (RequestFailedException ex) { Assert.Fail($"Unexpected error: {ex}"); } // Clean up after the test when we're finished container.Delete(); }
public void BatchErrors() { string connectionString = ConnectionString; string containerName = Randomize("sample-container"); #region Snippet:SampleSnippetsBatch_Troubleshooting // Get a connection string to our Azure Storage account. //@@ string connectionString = "<connection_string>"; //@@ string containerName = "sample-container"; // Get a reference to a container named "sample-container" and then create it BlobServiceClient service = new BlobServiceClient(connectionString); BlobContainerClient container = service.GetBlobContainerClient(containerName); container.Create(); // Create a blob named "valid" BlobClient valid = container.GetBlobClient("valid"); valid.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Valid!"))); // Get a reference to a blob named "invalid", but never create it BlobClient invalid = container.GetBlobClient("invalid"); // Delete both blobs at the same time BlobBatchClient batch = service.GetBlobBatchClient(); try { batch.DeleteBlobs(new Uri[] { valid.Uri, invalid.Uri }); } catch (AggregateException) { // An aggregate exception is thrown for all the individual failures // Check ex.InnerExceptions for RequestFailedException instances } #endregion }
public void BatchDelete() { string connectionString = ConnectionString; string containerName = Randomize("sample-container"); #region Snippet:SampleSnippetsBatch_DeleteBatch // Get a connection string to our Azure Storage account. //@@ string connectionString = "<connection_string>"; //@@ string containerName = "sample-container"; // Get a reference to a container named "sample-container" and then create it BlobServiceClient service = new BlobServiceClient(connectionString); BlobContainerClient container = service.GetBlobContainerClient(containerName); container.Create(); // Create three blobs named "foo", "bar", and "baz" BlobClient foo = container.GetBlobClient("foo"); BlobClient bar = container.GetBlobClient("bar"); BlobClient baz = container.GetBlobClient("baz"); foo.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Foo!"))); bar.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Bar!"))); baz.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Baz!"))); // Delete all three blobs at once BlobBatchClient batch = service.GetBlobBatchClient(); batch.DeleteBlobs(new Uri[] { foo.Uri, bar.Uri, baz.Uri }); #endregion Assert.AreEqual(0, container.GetBlobs().ToList().Count); // Clean up after we're finished container.Delete(); }
public void BatchSetAccessTier() { // Get a connection string to our Azure Storage account. string connectionString = ConnectionString; // Get a reference to a container named "sample-container" and then create it BlobServiceClient service = new BlobServiceClient(connectionString); BlobContainerClient container = service.GetBlobContainerClient(Randomize("sample-container")); container.Create(); try { // Create three blobs named "foo", "bar", and "baz" BlobClient foo = container.GetBlobClient("foo"); BlobClient bar = container.GetBlobClient("bar"); BlobClient baz = container.GetBlobClient("baz"); foo.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Foo!"))); bar.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Bar!"))); baz.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Baz!"))); // Set the access tier for all three blobs at once BlobBatchClient batch = service.GetBlobBatchClient(); batch.SetBlobsAccessTier(new Uri[] { foo.Uri, bar.Uri, baz.Uri }, AccessTier.Cool); } finally { // Clean up after the test when we're finished container.Delete(); } }
public void BatchSetAccessTierCool() { string connectionString = ConnectionString; string containerName = Randomize("sample-container"); #region Snippet:SampleSnippetsBatch_AccessTier // Get a connection string to our Azure Storage account. //@@ string connectionString = "<connection_string>"; //@@ string containerName = "sample-container"; // Get a reference to a container named "sample-container" and then create it BlobServiceClient service = new BlobServiceClient(connectionString); BlobContainerClient container = service.GetBlobContainerClient(containerName); container.Create(); // Create three blobs named "foo", "bar", and "baz" BlobClient foo = container.GetBlobClient("foo"); BlobClient bar = container.GetBlobClient("bar"); BlobClient baz = container.GetBlobClient("baz"); foo.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Foo!"))); bar.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Bar!"))); baz.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Baz!"))); // Set the access tier for all three blobs at once BlobBatchClient batch = service.GetBlobBatchClient(); batch.SetBlobsAccessTier(new Uri[] { foo.Uri, bar.Uri, baz.Uri }, AccessTier.Cool); #endregion foreach (BlobItem blob in container.GetBlobs()) { Assert.AreEqual(AccessTier.Cool, blob.Properties.AccessTier); } // Clean up after the test when we're finished }
public void List() { // Get a connection string to our Azure Storage account. string connectionString = ConnectionString; // Get a reference to a container named "sample-container" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, Randomize("sample-container")); container.Create(); try { // Upload a couple of blobs so we have something to list container.UploadBlob("first", File.OpenRead(CreateTempFile())); container.UploadBlob("second", File.OpenRead(CreateTempFile())); container.UploadBlob("third", File.OpenRead(CreateTempFile())); // List all the blobs List <string> names = new List <string>(); foreach (BlobItem blob in container.GetBlobs()) { names.Add(blob.Name); } Assert.AreEqual(3, names.Count); Assert.Contains("first", names); Assert.Contains("second", names); Assert.Contains("third", names); } finally { // Clean up after the test when we're finished container.Delete(); } }
public async Task BlobContentHash() { string data = "hello world"; using Stream contentStream = new MemoryStream(Encoding.UTF8.GetBytes(data)); // precalculate hash for sample byte[] precalculatedContentHash; using (var md5 = MD5.Create()) { precalculatedContentHash = md5.ComputeHash(contentStream); } contentStream.Position = 0; // setup blob string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); var containerClient = new BlobContainerClient(ConnectionString, containerName); try { containerClient.Create(); var blobClient = containerClient.GetBlobClient(blobName); #region Snippet:SampleSnippetsBlobMigration_BlobContentMD5 // upload with blob content hash await blobClient.UploadAsync( contentStream, new BlobUploadOptions() { HttpHeaders = new BlobHttpHeaders() { ContentHash = precalculatedContentHash } }); // download whole blob and validate against stored blob content hash Response <BlobDownloadInfo> response = await blobClient.DownloadAsync(); Stream downloadStream = response.Value.Content; byte[] blobContentMD5 = response.Value.Details.BlobContentHash ?? response.Value.ContentHash; // validate stream against hash in your workflow #endregion byte[] downloadedBytes; using (var memStream = new MemoryStream()) { await downloadStream.CopyToAsync(memStream); downloadedBytes = memStream.ToArray(); } Assert.AreEqual(data, Encoding.UTF8.GetString(downloadedBytes)); Assert.IsTrue(Enumerable.SequenceEqual(precalculatedContentHash, blobContentMD5)); } finally { await containerClient.DeleteIfExistsAsync(); } }
public void SetUp() { const int ContainerNameMaxLength = 63; var name = "test-" + TestContext.CurrentContext.Test.MethodName .ToLowerInvariant() .Replace('_', '-'); if (name.Length > ContainerNameMaxLength) { name = name.Substring(0, ContainerNameMaxLength); } Configuration = new AzureBlobStorageConfiguration { ConnectionString = "UseDevelopmentStorage=true", ContainerName = name, }; Container = new BlobContainerClient( Configuration.ConnectionString, Configuration.ContainerName ); Container.DeleteIfExists(); Container.Create(); }
public async Task DownloadBlob() { string data = "hello world"; //setup blob string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); var containerClient = new BlobContainerClient(ConnectionString, containerName); string downloadFilePath = this.CreateTempPath(); try { containerClient.Create(); containerClient.GetBlobClient(blobName).Upload(new MemoryStream(Encoding.UTF8.GetBytes(data))); #region Snippet:SampleSnippetsBlobMigration_DownloadBlob BlobClient blobClient = containerClient.GetBlobClient(blobName); await blobClient.DownloadToAsync(downloadFilePath); #endregion FileStream fs = File.OpenRead(downloadFilePath); string downloadedData = await new StreamReader(fs).ReadToEndAsync(); fs.Close(); Assert.AreEqual(data, downloadedData); } finally { await containerClient.DeleteIfExistsAsync(); } }
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 void AuthWithSasCredential() { //setup blob string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); var container = new BlobContainerClient(ConnectionString, containerName); try { container.Create(); BlobClient blobClient = container.GetBlobClient(blobName); blobClient.Upload(BinaryData.FromString("hello world")); // build SAS URI for sample Uri sasUri = blobClient.GenerateSasUri(BlobSasPermissions.All, DateTimeOffset.UtcNow.AddHours(1)); #region Snippet:SampleSnippetsBlobMigration_SasUri BlobClient blob = new BlobClient(sasUri); #endregion var stream = new MemoryStream(); blob.DownloadTo(stream); Assert.Greater(stream.Length, 0); } finally { container.Delete(); } }
public void Download() { // Create a temporary Lorem Ipsum file on disk that we can upload string originalPath = CreateTempFile(SampleFileContent); // Get a temporary path on disk where we can download the file string downloadPath = CreateTempPath(); // Get a connection string to our Azure Storage account. string connectionString = ConnectionString; // Get a reference to a container named "sample-container" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, Randomize("sample-container")); container.Create(); try { // Get a reference to a blob named "sample-file" BlobClient blob = container.GetBlobClient(Randomize("sample-file")); // First upload something the blob so we have something to download blob.Upload(originalPath); // Download the blob's contents and save it to a file blob.DownloadTo(downloadPath); // Verify the contents Assert.AreEqual(SampleFileContent, File.ReadAllText(downloadPath)); } finally { // Clean up after the test when we're finished container.Delete(); } }
public async Task ListBlobsHierarchy() { string data = "hello world"; string virtualDirName = Randomize("sample-virtual-dir"); string containerName = Randomize("sample-container"); var containerClient = new BlobContainerClient(ConnectionString, containerName); try { containerClient.Create(); foreach (var blobName in new List <string> { "foo.txt", "bar.txt", virtualDirName + "/fizz.txt", virtualDirName + "/buzz.txt" }) { containerClient.GetBlobClient(blobName).Upload(BinaryData.FromString(data)); } var expectedBlobNamesResult = new HashSet <string> { "foo.txt", "bar.txt" }; // tools to consume blob listing while looking good in the sample snippet HashSet <string> downloadedBlobNames = new HashSet <string>(); HashSet <string> downloadedPrefixNames = new HashSet <string>(); void MyConsumeBlobItemFunc(BlobHierarchyItem item) { if (item.IsPrefix) { downloadedPrefixNames.Add(item.Prefix); } else { downloadedBlobNames.Add(item.Blob.Name); } } // show in snippet where the prefix goes, but our test doesn't want a prefix for its data set string blobPrefix = null; string delimiter = "/"; #region Snippet:SampleSnippetsBlobMigration_ListHierarchy IAsyncEnumerable <BlobHierarchyItem> results = containerClient.GetBlobsByHierarchyAsync(prefix: blobPrefix, delimiter: delimiter); await foreach (BlobHierarchyItem item in results) { MyConsumeBlobItemFunc(item); } #endregion Assert.IsTrue(expectedBlobNamesResult.SetEquals(downloadedBlobNames)); Assert.IsTrue(new HashSet <string> { virtualDirName + '/' }.SetEquals(downloadedPrefixNames)); } finally { await containerClient.DeleteIfExistsAsync(); } }
public async Task ListBlobsManual() { string data = "hello world"; string containerName = Randomize("sample-container"); var containerClient = new BlobContainerClient(ConnectionString, containerName); try { containerClient.Create(); HashSet <string> blobNames = new HashSet <string>(); foreach (var _ in Enumerable.Range(0, 10)) { string blobName = Randomize("sample-blob"); containerClient.GetBlobClient(blobName).Upload(new MemoryStream(Encoding.UTF8.GetBytes(data))); blobNames.Add(blobName); } // tools to consume blob listing while looking good in the sample snippet HashSet <string> downloadedBlobNames = new HashSet <string>(); void MyConsumeBlobItemFunc(BlobItem item) { downloadedBlobNames.Add(item.Name); } #region Snippet:SampleSnippetsBlobMigration_ListBlobsManual // set this to already existing continuation token to pick up where you previously left off string initialContinuationToken = null; AsyncPageable <BlobItem> results = containerClient.GetBlobsAsync(); IAsyncEnumerable <Page <BlobItem> > pages = results.AsPages(initialContinuationToken); // the foreach loop requests the next page of results every loop // you do not need to explicitly access the continuation token just to get the next page // to stop requesting new pages, break from the loop // you also have access to the contination token returned with each page if needed await foreach (Page <BlobItem> page in pages) { // process page foreach (BlobItem item in page.Values) { MyConsumeBlobItemFunc(item); } // access continuation token if desired string continuationToken = page.ContinuationToken; } #endregion Assert.IsTrue(blobNames.SetEquals(downloadedBlobNames)); } finally { await containerClient.DeleteIfExistsAsync(); } }
public void CreateContainer(string containerName) { BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(containerName); if (containerClient.Exists()) { throw new ApplicationException($"Unable to create container '{containerName}' as it already exists"); } containerClient.Create(); }
public virtual Response <BlobContainerClient> CreateBlobContainer( string containerName, PublicAccessType publicAccessType = PublicAccessType.None, Metadata metadata = default, CancellationToken cancellationToken = default) { BlobContainerClient container = GetBlobContainerClient(containerName); Response <ContainerInfo> response = container.Create(publicAccessType, metadata, cancellationToken); return(new Response <BlobContainerClient>(response.GetRawResponse(), container)); }
public void UploadFile(string containerName, BlobFileData fileData, bool isPrivate) { var container = new BlobContainerClient(connectionString, containerName); if (!container.Exists()) { container.Create(); var accessType = isPrivate ? Azure.Storage.Blobs.Models.PublicAccessType.None : Azure.Storage.Blobs.Models.PublicAccessType.Blob; container.SetAccessPolicy(accessType); } using (MemoryStream blobStream = new MemoryStream(fileData.Contents)) { container.UploadBlob(fileData.Name, blobStream); } }
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 void Upload() { // Create a temporary Lorem Ipsum file on disk that we can upload string path = CreateTempFile(SampleFileContent); // Get a connection string to our Azure Storage account. You can // obtain your connection string from the Azure Portal (click // Access Keys under Settings in the Portal Storage account blade) // or using the Azure CLI with: // // az storage account show-connection-string --name <account_name> --resource-group <resource_group> // // And you can provide the connection string to your application // using an environment variable. string connectionString = ConnectionString; // Get a reference to a container named "sample-container" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, Randomize("sample-container")); container.Create(); try { // Get a reference to a blob named "sample-file" in a container named "sample-container" BlobClient blob = container.GetBlobClient(Randomize("sample-file")); // Open the file and upload its data using (FileStream file = File.OpenRead(path)) { blob.Upload(file); } // Verify we uploaded one blob with some content Assert.AreEqual(1, container.GetBlobs().Count()); BlobProperties properties = blob.GetProperties(); Assert.AreEqual(SampleFileContent.Length, properties.ContentLength); } finally { // Clean up after the test when we're finished container.Delete(); } }
public void AuthWithSasCredential() { //setup blob string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); var container = new BlobContainerClient(ConnectionString, containerName); try { container.Create(); container.GetBlobClient(blobName).Upload(new MemoryStream(Encoding.UTF8.GetBytes("hello world"))); // build SAS URI for sample BlobSasBuilder sas = new BlobSasBuilder { BlobContainerName = containerName, BlobName = blobName, ExpiresOn = DateTimeOffset.UtcNow.AddHours(1) }; sas.SetPermissions(BlobSasPermissions.All); StorageSharedKeyCredential credential = new StorageSharedKeyCredential(StorageAccountName, StorageAccountKey); UriBuilder sasUri = new UriBuilder(this.StorageAccountBlobUri); sasUri.Path = $"{containerName}/{blobName}"; sasUri.Query = sas.ToSasQueryParameters(credential).ToString(); string blobLocationWithSas = sasUri.Uri.ToString(); #region Snippet:SampleSnippetsBlobMigration_SasUri BlobClient blob = new BlobClient(new Uri(blobLocationWithSas)); #endregion var stream = new MemoryStream(); blob.DownloadTo(stream); Assert.Greater(stream.Length, 0); } finally { container.Delete(); } }
public void FineGrainedBatching() { string connectionString = ConnectionString; string containerName = Randomize("sample-container"); #region Snippet:SampleSnippetsBatch_FineGrainedBatching // Get a connection string to our Azure Storage account. //@@ string connectionString = "<connection_string>"; //@@ string containerName = "sample-container"; // Get a reference to a container named "sample-container" and then create it BlobServiceClient service = new BlobServiceClient(connectionString); BlobContainerClient container = service.GetBlobContainerClient(containerName); container.Create(); // Create three blobs named "foo", "bar", and "baz" BlobClient foo = container.GetBlobClient("foo"); BlobClient bar = container.GetBlobClient("bar"); BlobClient baz = container.GetBlobClient("baz"); foo.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Foo!"))); foo.CreateSnapshot(); bar.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Bar!"))); bar.CreateSnapshot(); baz.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Baz!"))); // Create a batch with three deletes BlobBatchClient batchClient = service.GetBlobBatchClient(); BlobBatch batch = batchClient.CreateBatch(); batch.DeleteBlob(foo.Uri, DeleteSnapshotsOption.IncludeSnapshots); batch.DeleteBlob(bar.Uri, DeleteSnapshotsOption.OnlySnapshots); batch.DeleteBlob(baz.Uri); // Submit the batch batchClient.SubmitBatch(batch); #endregion Pageable <BlobItem> blobs = container.GetBlobs(states: BlobStates.Snapshots); Assert.AreEqual(1, blobs.Count()); Assert.AreEqual("bar", blobs.FirstOrDefault().Name); // Clean up after the test when we're finished container.Delete(); }
public void FineGrainedBatching() { // Get a connection string to our Azure Storage account. string connectionString = ConnectionString; // Get a reference to a container named "sample-container" and then create it BlobServiceClient service = new BlobServiceClient(connectionString); BlobContainerClient container = service.GetBlobContainerClient(Randomize("sample-container")); container.Create(); try { // Create three blobs named "foo", "bar", and "baz" BlobClient foo = container.GetBlobClient("foo"); BlobClient bar = container.GetBlobClient("bar"); BlobClient baz = container.GetBlobClient("baz"); foo.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Foo!"))); bar.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Bar!"))); baz.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Baz!"))); // Create a batch with three deletes BlobBatchClient batchClient = service.GetBlobBatchClient(); BlobBatch batch = batchClient.CreateBatch(); Response fooResponse = batch.DeleteBlob(foo.Uri, DeleteSnapshotsOption.Include); Response barResponse = batch.DeleteBlob(bar.Uri); Response bazResponse = batch.DeleteBlob(baz.Uri); // Submit the batch batchClient.SubmitBatch(batch); // Verify the results Assert.AreEqual(202, fooResponse.Status); Assert.AreEqual(202, barResponse.Status); Assert.AreEqual(202, bazResponse.Status); } finally { // Clean up after the test when we're finished container.Delete(); } }
public async Task ListBlobs() { string data = "hello world"; string containerName = Randomize("sample-container"); var containerClient = new BlobContainerClient(ConnectionString, containerName); try { containerClient.Create(); HashSet <string> blobNames = new HashSet <string>(); foreach (var _ in Enumerable.Range(0, 10)) { string blobName = Randomize("sample-blob"); containerClient.GetBlobClient(blobName).Upload(new MemoryStream(Encoding.UTF8.GetBytes(data))); blobNames.Add(blobName); } // tools to consume blob listing while looking good in the sample snippet HashSet <string> downloadedBlobNames = new HashSet <string>(); void MyConsumeBlobItemFunc(BlobItem item) { downloadedBlobNames.Add(item.Name); } #region Snippet:SampleSnippetsBlobMigration_ListBlobs IAsyncEnumerable <BlobItem> results = containerClient.GetBlobsAsync(); await foreach (BlobItem item in results) { MyConsumeBlobItemFunc(item); } #endregion Assert.IsTrue(blobNames.SetEquals(downloadedBlobNames)); } finally { await containerClient.DeleteIfExistsAsync(); } }
public void List() { string connectionString = ConnectionString; string containerName = Randomize("sample-container"); string filePath = CreateTempFile(); #region Snippet:SampleSnippetsBlob_List // Get a connection string to our Azure Storage account. //@@ string connectionString = "<connection_string>"; //@@ string containerName = "sample-container"; //@@ string filePath = "hello.jpg"; // Get a reference to a container named "sample-container" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, containerName); container.Create(); // Upload a few blobs so we have something to list container.UploadBlob("first", File.OpenRead(filePath)); container.UploadBlob("second", File.OpenRead(filePath)); container.UploadBlob("third", File.OpenRead(filePath)); // Print out all the blob names foreach (BlobItem blob in container.GetBlobs()) { Console.WriteLine(blob.Name); } #endregion List <string> names = new List <string>(); foreach (BlobItem blob in container.GetBlobs()) { names.Add(blob.Name); } Assert.AreEqual(3, names.Count); Assert.Contains("first", names); Assert.Contains("second", names); Assert.Contains("third", names); container.Delete(); }
public async Task DownloadBlobDirectStream() { 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(new MemoryStream(Encoding.UTF8.GetBytes(data))); // tools to consume stream while looking good in the sample snippet string downloadedData = null; async Task MyConsumeStreamFunc(Stream stream) { downloadedData = await new StreamReader(stream).ReadToEndAsync(); } #region Snippet:SampleSnippetsBlobMigration_DownloadBlobDirectStream BlobClient blobClient = containerClient.GetBlobClient(blobName); BlobDownloadInfo downloadResponse = await blobClient.DownloadAsync(); using (Stream downloadStream = downloadResponse.Content) { await MyConsumeStreamFunc(downloadStream); } #endregion Assert.AreEqual(data, downloadedData); } finally { await containerClient.DeleteIfExistsAsync(); } }
public async Task EditMetadata() { 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_EditMetadata IDictionary <string, string> metadata = blobClient.GetProperties().Value.Metadata; metadata.Add("foo", "bar"); blobClient.SetMetadata(metadata); #endregion var expectedMetadata = new Dictionary <string, string> { { "foo", "bar" }, { "fizz", "buzz" } }; var actualMetadata = (await blobClient.GetPropertiesAsync()).Value.Metadata; Assert.AreEqual(expectedMetadata.Count, actualMetadata.Count); foreach (var expectedKvp in expectedMetadata) { Assert.IsTrue(actualMetadata.TryGetValue(expectedKvp.Key, out var actualValue)); Assert.AreEqual(expectedKvp.Value, actualValue); } } finally { await containerClient.DeleteIfExistsAsync(); } }
public void Upload() { string connectionString = ConnectionString; string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); string filePath = CreateTempFile(SampleFileContent); #region Snippet:SampleSnippetsBlob_Upload // Get a connection string to our Azure Storage account. You can // obtain your connection string from the Azure Portal (click // Access Keys under Settings in the Portal Storage account blade) // or using the Azure CLI with: // // az storage account show-connection-string --name <account_name> --resource-group <resource_group> // // And you can provide the connection string to your application // using an environment variable. //@@ string connectionString = "<connection_string>"; //@@ string containerName = "sample-container"; //@@ string blobName = "sample-blob"; //@@ string filePath = "sample-file"; // Get a reference to a container named "sample-container" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, containerName); container.Create(); // Get a reference to a blob named "sample-file" in a container named "sample-container" BlobClient blob = container.GetBlobClient(blobName); // Upload local file blob.Upload(filePath); #endregion Assert.AreEqual(1, container.GetBlobs().Count()); BlobProperties properties = blob.GetProperties(); Assert.AreEqual(SampleFileContent.Length, properties.ContentLength); }
public void BatchErrors() { // Get a connection string to our Azure Storage account. string connectionString = ConnectionString; // Get a reference to a container named "sample-container" and then create it BlobServiceClient service = new BlobServiceClient(connectionString); BlobContainerClient container = service.GetBlobContainerClient(Randomize("sample-container")); container.Create(); try { // Create a blob named "valid" BlobClient valid = container.GetBlobClient("valid"); valid.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Valid!"))); // Get a reference to a blob named "invalid", but never create it BlobClient invalid = container.GetBlobClient("invalid"); // Delete both blobs at the same time BlobBatchClient batch = service.GetBlobBatchClient(); batch.DeleteBlobs(new Uri[] { valid.Uri, invalid.Uri }); } catch (AggregateException ex) { // An aggregate exception is thrown for all the indivudal failures Assert.AreEqual(1, ex.InnerExceptions.Count); StorageRequestFailedException failure = ex.InnerException as StorageRequestFailedException; Assert.IsTrue(BlobErrorCode.BlobNotFound == failure.ErrorCode); } finally { // Clean up after the test when we're finished container.Delete(); } }
public async Task TransactionalMD5() { string data = "hello world"; string blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); List <string> blockList = new List <string> { blockId }; using Stream blockContentStream = new MemoryStream(Encoding.UTF8.GetBytes(data)); // precalculate hash for sample byte[] precalculatedBlockHash; using (var md5 = MD5.Create()) { precalculatedBlockHash = md5.ComputeHash(blockContentStream); } blockContentStream.Position = 0; // setup blob string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); var containerClient = new BlobContainerClient(ConnectionString, containerName); try { containerClient.Create(); var blockBlobClient = containerClient.GetBlockBlobClient(blobName); #region Snippet:SampleSnippetsBlobMigration_TransactionalMD5 // upload a block with transactional hash calculated by user await blockBlobClient.StageBlockAsync( blockId, blockContentStream, transactionalContentHash : precalculatedBlockHash); // upload more blocks as needed // commit block list await blockBlobClient.CommitBlockListAsync(blockList); // download any range of blob with transactional MD5 requested (maximum 4 MB for downloads) Response <BlobDownloadInfo> response = await blockBlobClient.DownloadAsync( range : new HttpRange(length: 4 * Constants.MB), // a range must be provided; here we use transactional download max size rangeGetContentHash : true); Stream downloadStream = response.Value.Content; byte[] transactionalMD5 = response.Value.ContentHash; // validate stream against hash in your workflow #endregion byte[] downloadedBytes; using (var memStream = new MemoryStream()) { await downloadStream.CopyToAsync(memStream); downloadedBytes = memStream.ToArray(); } Assert.AreEqual(data, Encoding.UTF8.GetString(downloadedBytes)); Assert.IsTrue(Enumerable.SequenceEqual(precalculatedBlockHash, transactionalMD5)); } finally { await containerClient.DeleteIfExistsAsync(); } }
static void Main(string[] args) { Console.WriteLine("*** Welcome to the Blob Container Copy Utility ***"); Console.Write("Enter the connection string of the source storage account: "); string sourceStorageConnectionString = Console.ReadLine(); Console.Write("Enter the name of the source container: "); string sourceContainer = Console.ReadLine(); Console.Write("Enter the connection string of the destination storage account: "); string destStorageConnectionString = Console.ReadLine(); Console.Write("Enter the name of the destination container: "); string destContainer = Console.ReadLine(); if (string.IsNullOrEmpty(sourceStorageConnectionString) || string.IsNullOrEmpty(sourceContainer) || string.IsNullOrEmpty(destStorageConnectionString) || string.IsNullOrEmpty(destContainer)) { Console.WriteLine("You must provide values for the source connection string, source container, destination connection string and destination container"); } else { BlobServiceClient sourceBlobServiceClient = new BlobServiceClient(sourceStorageConnectionString); BlobContainerClient sourceContainerClient = sourceBlobServiceClient.GetBlobContainerClient(sourceContainer); BlobServiceClient destBlobServiceClient = new BlobServiceClient(destStorageConnectionString); BlobContainerClient destContainerClient = destBlobServiceClient.GetBlobContainerClient(destContainer); if (sourceContainerClient.Exists()) { if (!destContainerClient.Exists()) { destContainerClient.Create(PublicAccessType.None); } List <BlobItem> blobs = sourceContainerClient.GetBlobs().ToList(); Console.WriteLine($"Copying {blobs.Count} files from source to destination."); foreach (BlobItem blob in blobs) { BlobClient sourceBlob = sourceContainerClient.GetBlobClient(blob.Name); if (sourceBlob.Exists()) { Console.WriteLine($"{blob.Name} copy started..."); BlobDownloadInfo download = sourceBlob.Download(); BlobClient destBlob = destContainerClient.GetBlobClient(blob.Name); destBlob.Upload(download.Content); Console.WriteLine($"{blob.Name} copied successfully."); } } } else { Console.WriteLine($"Source blob container {sourceContainer} does not exist."); } Console.WriteLine("*** Copy completed ***"); } Console.WriteLine("*** Press any key to exit ***"); Console.Read(); }