Skip to content

Commit a043515

Browse files
author
Pedro Wood
committed
added CORS and Page Blob Ranges samples
1 parent b188505 commit a043515

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

BlobStorage/Advanced.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ public static async Task CallBlobAdvancedSamples()
7575

7676
// Call shared access signature samples (both container and blob).
7777
await CallSasSamples(container);
78+
79+
// CORS Rules
80+
await CorsSample(blobClient);
81+
82+
// Page Blob Ranges
83+
await PageRangesSample(blobClient, container);
7884
}
7985
catch (StorageException e)
8086
{
@@ -1827,5 +1833,78 @@ private static async Task TestBlobSAS(string sasUri, string blobContent)
18271833
}
18281834

18291835
#endregion
1836+
1837+
/// <summary>
1838+
/// Query the Cross-Origin Resource Sharing (CORS) rules for the Queue service
1839+
/// </summary>
1840+
/// <param name="blobClient"></param>
1841+
private static async Task CorsSample(CloudBlobClient blobClient)
1842+
{
1843+
// Set CORS rules
1844+
Console.WriteLine("Set CORS rules");
1845+
1846+
CorsProperties cors = new CorsProperties();
1847+
CorsRule corsRule = new CorsRule
1848+
{
1849+
AllowedHeaders = new List<string> { "*" },
1850+
AllowedMethods = CorsHttpMethods.Get,
1851+
AllowedOrigins = new List<string> { "*" },
1852+
ExposedHeaders = new List<string> { "*" },
1853+
MaxAgeInSeconds = 3600
1854+
};
1855+
1856+
cors.CorsRules.Add(corsRule);
1857+
await blobClient.SetServicePropertiesAsync(new ServiceProperties(null, null, null, cors));
1858+
Console.WriteLine();
1859+
}
1860+
1861+
/// <summary>
1862+
/// Get a list of valid page ranges for a page blob
1863+
/// </summary>
1864+
/// <param name="blobClient"></param>
1865+
/// <param name="container"></param>
1866+
/// <returns>A Task object.</returns>
1867+
private static async Task PageRangesSample(CloudBlobClient blobClient, CloudBlobContainer container)
1868+
{
1869+
BlobRequestOptions requestOptions = new BlobRequestOptions() { RetryPolicy = new NoRetry() };
1870+
await container.CreateIfNotExistsAsync(requestOptions, null);
1871+
1872+
Console.WriteLine("Create Page Blob");
1873+
CloudPageBlob pageBlob = container.GetPageBlobReference("blob1");
1874+
pageBlob.Create(4 * 1024);
1875+
1876+
Console.WriteLine("Write Pages to Blob");
1877+
byte[] buffer = GetRandomBuffer(1024);
1878+
using (MemoryStream memoryStream = new MemoryStream(buffer))
1879+
{
1880+
pageBlob.WritePages(memoryStream, 512);
1881+
}
1882+
1883+
using (MemoryStream memoryStream = new MemoryStream(buffer))
1884+
{
1885+
pageBlob.WritePages(memoryStream, 3 * 1024);
1886+
}
1887+
1888+
Console.WriteLine("Get Page Ranges");
1889+
IEnumerable<PageRange> pageRanges = pageBlob.GetPageRanges();
1890+
foreach (PageRange pageRange in pageRanges)
1891+
{
1892+
Console.WriteLine(pageRange.ToString());
1893+
}
1894+
1895+
// Clean up after the demo. This line is not strictly necessary as the container is deleted in the next call.
1896+
// It is included for the purposes of the example.
1897+
Console.WriteLine("Delete page blob");
1898+
await pageBlob.DeleteIfExistsAsync();
1899+
Console.WriteLine();
1900+
}
1901+
1902+
private static byte[] GetRandomBuffer(int size)
1903+
{
1904+
byte[] buffer = new byte[size];
1905+
Random random = new Random();
1906+
random.NextBytes(buffer);
1907+
return buffer;
1908+
}
18301909
}
18311910
}

0 commit comments

Comments
 (0)