Skip to content

Instantly share code, notes, and snippets.

@buchizo

buchizo/Program.cs

Created Dec 14, 2020
Embed
What would you like to do?
Azure Storage SDK for .NET v12 stream / sas / zip sample
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using Azure.Storage.Sas;
using Cocona;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp17
{
class Program
{
static async Task Main(string[] args)
{
await CoconaApp.RunAsync<Program>(args);
}
public async Task Upload(string connectionString, string containerName, string blobName)
{
var storageAccount = new BlobServiceClient(connectionString);
var container = storageAccount.GetBlobContainerClient(containerName);
// GetBlockBlobClient and SetHttpHeadersAsync methods is defined in Azure.Storage.Blobs.Specialized
var blockBlob = container.GetBlockBlobClient(blobName);
using var stream = await blockBlob.OpenWriteAsync(overwrite: true);
using var tw = new StreamWriter(stream);
do
{
Console.Write("input data: ");
var input = Console.ReadLine();
if (string.IsNullOrEmpty(input)) break;
// write something to stream...
await tw.WriteLineAsync(input);
} while (true);
tw.Flush();
tw.Close();
await blockBlob.SetHttpHeadersAsync(new BlobHttpHeaders { ContentType = "text/plain" });
}
public async Task ZipUpload(string connectionString, string containerName, string blobName)
{
var storageAccount = new BlobServiceClient(connectionString);
var container = storageAccount.GetBlobContainerClient(containerName);
// GetBlockBlobClient and SetHttpHeadersAsync methods is defined in Azure.Storage.Blobs.Specialized
var blockBlob = container.GetBlockBlobClient(blobName);
using var stream = await blockBlob.OpenWriteAsync(overwrite: true);
using var zipstream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(stream);
zipstream.SetLevel(0); // 0 - store only to 9 - means best compression
do
{
Console.Write("input filename: ");
var filename = Console.ReadLine();
if (string.IsNullOrEmpty(filename)) break;
using var memory = new MemoryStream();
Console.Write("\t input data: ");
await memory.WriteAsync(Encoding.UTF8.GetBytes(Console.ReadLine()));
// archive file
zipstream.PutNextEntry(new ICSharpCode.SharpZipLib.Zip.ZipEntry(filename)
{
Size = memory.Length
});
memory.Position = 0;
memory.CopyTo(zipstream);
zipstream.Flush();
} while (true);
await zipstream.FlushAsync();
zipstream.Close();
await blockBlob.SetHttpHeadersAsync(new BlobHttpHeaders { ContentType = "application/zip" });
}
public void GenerateSas(string connectionString, string account, string accountKey, string containerName, string blobName)
{
var storageAccount = new BlobServiceClient(connectionString);
var container = storageAccount.GetBlobContainerClient(containerName);
var blockBlob = container.GetBlobClient(blobName);
// SAS for blob
var sasBuilder = new BlobSasBuilder(BlobContainerSasPermissions.Read,
DateTimeOffset.UtcNow.AddDays(1)
)
{
BlobContainerName = containerName,
BlobName = blobName
};
var sharedKeyCred = new StorageSharedKeyCredential(account, accountKey);
var sasToken = sasBuilder.ToSasQueryParameters(sharedKeyCred).ToString();
Console.WriteLine($"url: {blockBlob.Uri}");
Console.WriteLine($"sasToken: {sasToken}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment