Created
March 31, 2016 02:53
-
-
Save davidfowl/8cecb0a679576917cc169ddea65a3c29 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 System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var cs = @"Put your connection string here"; | |
var storageAccount = CloudStorageAccount.Parse(cs); | |
var client = storageAccount.CreateCloudBlobClient(); | |
var container = client.GetContainerReference("test"); | |
container.CreateIfNotExists(); | |
container.SetPermissions(new BlobContainerPermissions | |
{ | |
PublicAccess = BlobContainerPublicAccessType.Blob | |
}); | |
// Get reference to a blob | |
var blockBlob = container.GetBlockBlobReference("someblob"); | |
try | |
{ | |
using (var stream = new MyStream()) | |
{ | |
blockBlob.UploadFromStreamAsync(stream).GetAwaiter().GetResult(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Could not upload image" + e.Message); | |
throw; | |
} | |
} | |
} | |
public class MyStream : Stream | |
{ | |
public override bool CanRead => true; | |
public override bool CanSeek => false; | |
public override bool CanWrite => false; | |
public override long Length | |
{ | |
get | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
public override long Position | |
{ | |
get | |
{ | |
throw new NotImplementedException(); | |
} | |
set | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
public override void Flush() | |
{ | |
throw new NotImplementedException(); | |
} | |
public override int Read(byte[] buffer, int offset, int count) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override long Seek(long offset, SeekOrigin origin) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override void SetLength(long value) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override void Write(byte[] buffer, int offset, int count) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) | |
{ | |
var tcs = new TaskCompletionSource<int>(state); | |
tcs.SetResult(0); | |
// Synchronous completion | |
callback(tcs.Task); | |
return tcs.Task; | |
} | |
public override int EndRead(IAsyncResult asyncResult) | |
{ | |
var tcs = (TaskCompletionSource<int>)asyncResult; | |
return tcs.Task.GetAwaiter().GetResult(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment