Skip to content

Commit 39f4819

Browse files
authored
Merge branch 'master' into master
2 parents 57dd269 + 5949ddb commit 39f4819

File tree

6 files changed

+103
-66
lines changed

6 files changed

+103
-66
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ BlobStorage/StyleCop.cache
99
*.nupkg
1010
packages/*
1111
*.ps1
12-
.vs/*
12+
.vs/*

BlobStorage/Advanced.cs

Lines changed: 88 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
namespace BlobStorage
1818
{
19+
using Microsoft.Azure;
20+
using Microsoft.Azure.Storage;
21+
using Microsoft.Azure.Storage.Blob;
22+
using Microsoft.Azure.Storage.RetryPolicies;
23+
using Microsoft.Azure.Storage.Shared.Protocol;
1924
using System;
2025
using System.Collections.Generic;
2126
using System.IO;
@@ -88,6 +93,12 @@ public static async Task CallBlobAdvancedSamples()
8893

8994
// Call shared access signature samples (both container and blob).
9095
await CallSasSamples(container);
96+
97+
// CORS Rules
98+
await CorsSample(blobClient);
99+
100+
// Page Blob Ranges
101+
await PageRangesSample(container);
91102
}
92103
catch (StorageException e)
93104
{
@@ -106,7 +117,6 @@ public static async Task CallBlobAdvancedSamples()
106117
// The sample code deletes any containers it created if it runs completely. However, if you need to delete containers
107118
// created by previous sessions (for example, if you interrupted the running code before it completed),
108119
// you can uncomment and run the following line to delete them.
109-
110120
//await DeleteContainersWithPrefix(blobClient, ContainerPrefix);
111121

112122
// Return the service properties/storage analytics settings to their original values.
@@ -132,9 +142,6 @@ private static async Task CallBlobClientSamples(CloudBlobClient blobClient)
132142
// Configure storage analytics (metrics and logging) on Blob storage.
133143
await ConfigureBlobAnalyticsAsync(blobClient);
134144

135-
// Get geo-replication stats for Blob storage.
136-
await GetServiceStatsForSecondaryAsync(blobClient);
137-
138145
// List all containers in the storage account.
139146
ListAllContainers(blobClient, "sample-");
140147

@@ -323,7 +330,7 @@ private static async Task ConfigureBlobAnalyticsAsync(CloudBlobClient blobClient
323330
serviceProperties.MinuteMetrics.Version = "1.0";
324331

325332
// Set the default service version to be used for anonymous requests.
326-
serviceProperties.DefaultServiceVersion = "2015-04-05";
333+
serviceProperties.DefaultServiceVersion = "2018-11-09";
327334

328335
// Set the service properties.
329336
await blobClient.SetServicePropertiesAsync(serviceProperties);
@@ -336,50 +343,6 @@ private static async Task ConfigureBlobAnalyticsAsync(CloudBlobClient blobClient
336343
}
337344
}
338345

339-
/// <summary>
340-
/// Gets the Blob service stats for the secondary endpoint for an RA-GRS (read-access geo-redundant) storage account.
341-
/// </summary>
342-
/// <param name="blobClient">The Blob service client.</param>
343-
/// <returns>A Task object.</returns>
344-
private static async Task GetServiceStatsForSecondaryAsync(CloudBlobClient blobClient)
345-
{
346-
try
347-
{
348-
// Get the URI for the secondary endpoint for Blob storage.
349-
Uri secondaryUri = blobClient.StorageUri.SecondaryUri;
350-
351-
// Create a new service client based on the secondary endpoint.
352-
CloudBlobClient blobClientSecondary = new CloudBlobClient(secondaryUri, blobClient.Credentials);
353-
354-
// Get the current stats for the secondary.
355-
// The call will fail if your storage account does not have RA-GRS enabled.
356-
// Change the retry policy for this call so that if it fails, it fails quickly.
357-
BlobRequestOptions requestOptions = new BlobRequestOptions() { RetryPolicy = new NoRetry() };
358-
ServiceStats blobStats = await blobClientSecondary.GetServiceStatsAsync(requestOptions, null);
359-
360-
Console.WriteLine("Geo-replication status: {0}", blobStats.GeoReplication.Status);
361-
Console.WriteLine("Last geo-replication sync time: {0}", blobStats.GeoReplication.LastSyncTime);
362-
Console.WriteLine();
363-
}
364-
catch (StorageException e)
365-
{
366-
// In this case, we do not re-throw the exception, so that the sample will continue to run even if RA-GRS is not enabled
367-
// for this storage account.
368-
if (e.RequestInformation.HttpStatusCode == 403)
369-
{
370-
Console.WriteLine("This storage account does not appear to support RA-GRS.");
371-
Console.WriteLine("More information: {0}", e.Message);
372-
Console.WriteLine();
373-
}
374-
else
375-
{
376-
Console.WriteLine(e.Message);
377-
Console.ReadLine();
378-
throw;
379-
}
380-
}
381-
}
382-
383346
/// <summary>
384347
/// Lists all containers in the storage account.
385348
/// Note that the ListContainers method is called synchronously, for the purposes of the sample. However, in a real-world
@@ -2058,5 +2021,81 @@ private static async Task UploadByteArrayAsync(CloudBlobContainer container, lon
20582021
}
20592022

20602023
#endregion
2024+
2025+
/// <summary>
2026+
/// Query the Cross-Origin Resource Sharing (CORS) rules for the Queue service
2027+
/// </summary>
2028+
/// <param name="blobClient"></param>
2029+
private static async Task CorsSample(CloudBlobClient blobClient)
2030+
{
2031+
// Get CORS rules
2032+
Console.WriteLine("Get CORS rules");
2033+
2034+
ServiceProperties serviceProperties = await blobClient.GetServicePropertiesAsync();
2035+
2036+
// Add CORS rule
2037+
Console.WriteLine("Add CORS rule");
2038+
2039+
CorsRule corsRule = new CorsRule
2040+
{
2041+
AllowedHeaders = new List<string> { "*" },
2042+
AllowedMethods = CorsHttpMethods.Get,
2043+
AllowedOrigins = new List<string> { "*" },
2044+
ExposedHeaders = new List<string> { "*" },
2045+
MaxAgeInSeconds = 3600
2046+
};
2047+
2048+
serviceProperties.Cors.CorsRules.Add(corsRule);
2049+
await blobClient.SetServicePropertiesAsync(serviceProperties);
2050+
Console.WriteLine();
2051+
}
2052+
2053+
/// <summary>
2054+
/// Get a list of valid page ranges for a page blob
2055+
/// </summary>
2056+
/// <param name="container"></param>
2057+
/// <returns>A Task object.</returns>
2058+
private static async Task PageRangesSample(CloudBlobContainer container)
2059+
{
2060+
BlobRequestOptions requestOptions = new BlobRequestOptions { RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 3) };
2061+
await container.CreateIfNotExistsAsync(requestOptions, null);
2062+
2063+
Console.WriteLine("Create Page Blob");
2064+
CloudPageBlob pageBlob = container.GetPageBlobReference("blob1");
2065+
pageBlob.Create(4 * 1024);
2066+
2067+
Console.WriteLine("Write Pages to Blob");
2068+
byte[] buffer = GetRandomBuffer(1024);
2069+
using (MemoryStream memoryStream = new MemoryStream(buffer))
2070+
{
2071+
pageBlob.WritePages(memoryStream, 512);
2072+
}
2073+
2074+
using (MemoryStream memoryStream = new MemoryStream(buffer))
2075+
{
2076+
pageBlob.WritePages(memoryStream, 3 * 1024);
2077+
}
2078+
2079+
Console.WriteLine("Get Page Ranges");
2080+
IEnumerable<PageRange> pageRanges = pageBlob.GetPageRanges();
2081+
foreach (PageRange pageRange in pageRanges)
2082+
{
2083+
Console.WriteLine(pageRange.ToString());
2084+
}
2085+
2086+
// Clean up after the demo. This line is not strictly necessary as the container is deleted in the next call.
2087+
// It is included for the purposes of the example.
2088+
Console.WriteLine("Delete page blob");
2089+
await pageBlob.DeleteIfExistsAsync();
2090+
Console.WriteLine();
2091+
}
2092+
2093+
private static byte[] GetRandomBuffer(int size)
2094+
{
2095+
byte[] buffer = new byte[size];
2096+
Random random = new Random();
2097+
random.NextBytes(buffer);
2098+
return buffer;
2099+
}
20612100
}
20622101
}

BlobStorage/App.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
</dependentAssembly>
3434
</assemblyBinding>
3535
</runtime>
36-
</configuration>
36+
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /></startup>
37+
</configuration>

BlobStorage/BlobStorage.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>BlobStorage</RootNamespace>
1111
<AssemblyName>BlobStorage</AssemblyName>
12-
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
<TargetFrameworkProfile />
1516
</PropertyGroup>
1617
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1718
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -54,9 +55,6 @@
5455
<Reference Include="Microsoft.Data.Services.Client, Version=5.8.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5556
<HintPath>..\packages\Microsoft.Data.Services.Client.5.8.4\lib\net40\Microsoft.Data.Services.Client.dll</HintPath>
5657
</Reference>
57-
<Reference Include="Microsoft.WindowsAzure.Storage, Version=9.3.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
58-
<HintPath>..\packages\WindowsAzure.Storage.9.3.3\lib\net45\Microsoft.WindowsAzure.Storage.dll</HintPath>
59-
</Reference>
6058
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
6159
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
6260
</Reference>

BlobStorage/packages.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
<package id="Microsoft.Data.Services.Client" version="5.8.4" targetFramework="net452" />
1010
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net452" />
1111
<package id="System.Spatial" version="5.8.4" targetFramework="net452" />
12-
<package id="WindowsAzure.Storage" version="9.3.3" targetFramework="net452" />
1312
</packages>

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
---
22
services: storage
33
platforms: dotnet
4-
author: perrysk-msft
4+
author: tamram
55
---
66

7-
# Getting Started with Azure Blob Service in .NET
7+
# Azure Blob Storage Samples for .NET
88

99
Demonstrates how to use the Blob Storage service.
1010
Blob storage stores unstructured data such as text, binary data, documents or media files.
1111
Blobs can be accessed from anywhere in the world via HTTP or HTTPS.
1212

13-
Note: This sample uses the .NET 4.5 asynchronous programming model to demonstrate how to call the Storage Service using the
14-
storage client libraries asynchronous API's. When used in real applications this approach enables you to improve the
15-
responsiveness of your application. Calls to the storage service are prefixed by the await keyword.
16-
If you don't have a Microsoft Azure subscription you can
17-
get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
13+
Note: This sample uses the .NET 4.5 asynchronous programming model to demonstrate how to call Azure Storage using asynchronous API calls. When used in real applications, this approach enables you to improve the
14+
responsiveness of your application. Calls to Azure Storage are prefixed by the `await` keyword. For more information about asynchronous programming using the Async/Await pattern, see [Asynchronous Programming with Async and Await (C# and Visual Basic)](https://msdn.microsoft.com/library/hh191443.aspx).
15+
16+
If you don't already have a Microsoft Azure subscription, you can
17+
get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212).
1818

1919
## Running this sample
2020

2121
This sample can be run using either the Azure Storage Emulator that installs as part of this SDK - or by
2222
updating the App.Config file with your AccountName and Key.
2323
To run the sample using the Storage Emulator (default option):
2424

25-
1. Download and Install the Azure Storage Emulator [here](http://azure.microsoft.com/en-us/downloads/).
25+
1. Download and Install the Azure Storage Emulator [here](http://azure.microsoft.com/downloads/).
2626
2. Start the Azure Storage Emulator (once only) by pressing the Start button or the Windows key and searching for it by typing "Azure Storage Emulator". Select it from the list of applications to start it.
2727
3. Set breakpoints and run the project using F10.
2828

@@ -33,8 +33,8 @@ To run the sample using the Storage Service
3333
3. Set breakpoints and run the project using F10.
3434

3535
## More information
36-
- [What is a Storage Account](http://azure.microsoft.com/en-us/documentation/articles/storage-whatis-account/)
37-
- [Getting Started with Blobs](http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/)
36+
- [How to create, manage, or delete a storage account in the Azure Portal](https://azure.microsoft.com/en-us/documentation/articles/storage-create-storage-account/)
37+
- [Get started with Azure Blob storage (object storage) using .NET](https://azure.microsoft.com/documentation/articles/storage-dotnet-how-to-use-blobs/)
3838
- [Blob Service Concepts](http://msdn.microsoft.com/en-us/library/dd179376.aspx)
3939
- [Blob Service REST API](http://msdn.microsoft.com/en-us/library/dd135733.aspx)
4040
- [Blob Service C# API](http://go.microsoft.com/fwlink/?LinkID=398944)

0 commit comments

Comments
 (0)