For GetBlobReferenceFromServer to work, the blob must be present in the blob storage. This is useful in the scenario when you know that the blob exist in storage and would want to find out the type of blob - Block Blob or Page Blob.
If you want to create a block blob by uploading a file from the local computer you could do something like:
var blob = new CloudBlockBlob(new Uri(blobWithSas.AbsoluteUri), new StorageCredentials(blobWithSas.Sas));
using (var stream = new FileStream(fullFilePath, FileMode.Open))
{
blob.UploadFromStream(stream);
}
Coming to shared access signature functionality, I wrote a blog post not too long ago about this: http://gauravmantri.com/2013/02/13/revisiting-windows-azure-shared-access-signature/. You can call it version 2 of Steve's blog post:). I've shown examples of uploading blobs with shared access signature using both REST API and Storage Client Library 2.0.
Some code samples from the blog post:
Using Storage Client Library:
/// <summary>
/// Uploads a blob in a blob container where SAS permission is defined on a blob container using storage client library.
/// </summary>
/// <param name="blobContainerSasUri"></param>
static void UploadBlobWithStorageClientLibrarySasPermissionOnBlobContainer(string blobContainerSasUri)
{
CloudBlobContainer blobContainer = new CloudBlobContainer(new Uri(blobContainerSasUri));
CloudBlockBlob blob = blobContainer.GetBlockBlobReference("sample.txt");
string sampleContent = "This is sample text.";
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sampleContent)))
{
blob.UploadFromStream(ms);
}
}
Using REST API:
/// <summary>
/// Uploads a blob in a blob container where SAS permission is defined on a blob container using REST API.
/// </summary>
/// <param name="blobContainerSasUri"></param>
static void UploadBlobWithRestAPISasPermissionOnBlobContainer(string blobContainerSasUri)
{
string blobName = "sample.txt";
string sampleContent = "This is sample text.";
int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
string queryString = (new Uri(blobContainerSasUri)).Query;
string blobContainerUri = blobContainerSasUri.Split('?')[0];
string requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/{1}{2}", blobContainerUri, blobName, queryString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "PUT";
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.ContentLength = contentLength;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
}
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
}
}
Hope it helps!
If you want to become Azure solution architect, then join Azure online course today.
Thank you!!