Created
March 11, 2019 17:53
-
-
Save unter/2e024a5e305d7eeb7d1803f9e0fe53d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ICSharpCode.SharpZipLib.Zip; | |
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()) | |
{ | |
using (ZipOutputStream zipStream = new ZipOutputStream(blobStream)) | |
{ | |
for (int i = 0; i < 256; i++) | |
{ | |
string tempFilePath = Path.Combine(localFilePath, $"file{i}"); | |
CreateRandomTempFile(tempFilePath, 8); | |
zipStream.PutNextEntry(new ZipEntry($"file{i}")); | |
using (FileStream fs = new FileStream(tempFilePath, FileMode.Open)) | |
{ | |
fs.CopyTo(zipStream); | |
} | |
zipStream.CloseEntry(); | |
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