Skip to content

Instantly share code, notes, and snippets.

@unter
Created March 8, 2019 22:23
Show Gist options
  • Save unter/95f5ec9d22b5f1df9a15ff61f623adab to your computer and use it in GitHub Desktop.
Save unter/95f5ec9d22b5f1df9a15ff61f623adab to your computer and use it in GitHub Desktop.
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
namespace AzureBlobBlogStream
{
class Program
{
static string localFilePath = @"c:\tmp";
static void Main(string[] args)
{
AzureBlobStreamTest();
}
private static void AzureBlobStreamTest()
{
string storageConnectionString = "<insert blob storage connection string here>";
CloudStorageAccount account;
CloudStorageAccount.TryParse(storageConnectionString, out account);
CloudBlobClient cloudBlobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.GetContainerReference("zipfiles");
// Create a new blob object to reference for the stream
// Using a Guid for the name for demo purposes
var blob = container.GetBlockBlobReference(Guid.NewGuid().ToString());
try
{
using (Stream blobStream = blob.OpenWrite())
{
for (int i = 0; i < 256; i++)
{
string tempFilePath = Path.Combine(localFilePath, $"file{i}");
CreateRandomTempFile(tempFilePath, 8);
using (FileStream fs = new FileStream(tempFilePath, FileMode.Open))
{
fs.CopyTo(blobStream);
}
File.Delete(tempFilePath);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception in BlobStreamTest: {ex.Message}");
Console.ReadLine();
}
}
private static void CreateRandomTempFile(string fileName, long lengthInMb)
{
// https://stackoverflow.com/questions/4432178/creating-a-random-file-in-c-sharp
byte[] data = new byte[lengthInMb * 1024 * 1024];
Random rng = new Random();
rng.NextBytes(data);
File.WriteAllBytes(fileName, data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment