Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:09:07+00:00 2026-06-09T22:09:07+00:00

We currently use the REST API (based on microsoft samples) to upload blobs blockwise

  • 0

We currently use the REST API (based on microsoft samples) to upload blobs blockwise from a .NET client profile machine. The REST API examples use the Azure storage account name and access key directly to construct the SharedKey entry in the request header. For production code, we’ll need to calculate the SharedKey on our server, and deliver it for the client to use during the session.

Examples of SharedKey creation for blobs provide me with a Url plus query string that contains the access parameters.

My question: How do I get this Url/query string key format work in conjunction with the SharedKey header entry required by the Azure REST API?

Any pointers or tips greatly appreciated!
R

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:09:08+00:00Added an answer on June 9, 2026 at 10:09 pm

    Here you go. Obviously a lot of improvement can be made to this code 🙂 Give it a try. Do let me know if it works for you. I was able to upload a blob in development storage using the code below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Web;
    using System.Net;
    using System.Collections.Specialized;
    using System.Globalization;

    namespace UploadBlobUsingSASUrl
    {
    class Program
    {
    //This is part of SAS signature (query string). We will construct the URI later using this.
    private static string sasSignature = "sr=c&st=2012-08-16T14%3A38%3A48Z&se=2012-08-16T15%3A38%3A48Z&sp=w&sig=aNTLYQtwA1UmjG7j8Lg44t8YThL16FkNYBi54kl4ZKo%3D";
    //Blob storage endpoint
    private static string blobStorageEndpoint = "http://127.0.0.1:10000/devstoreaccount1";
    //Blob container name
    private static string blobContainerName = "[blob container name. SAS URI with Write permission must be created on this blob container]";
    //File to upload
    private static string fileToUpload = @"[Full path of the file you wish to upload]";
    //This is the default block size (This application always assumes that a file will be split in blocks and then uploaded).
    private static int blockSize = 256 * 1024;//256 KB
    //Storage service version (Unless you're using latest SAS related changes in cloud storage, use this version). For development storage always use this version.
    private static string x_ms_version = "2011-08-18";
    //Template for put block list
    private static string blockListTemplate = @"{0}";
    // Template for block id (to be included in put block list template)
    private static string blockIdTemplate = "{0}";
    //We'll keep a list of block ids.
    private static List blockIds = new List();
    static void Main(string[] args)
    {

    FileInfo file = new FileInfo(fileToUpload);
    long totalFileSize = file.Length;//Get the file size
    long bytesFrom = 0;
    long bytesRemaining = totalFileSize;
    string blobName = file.Name;
    //This is the base URI which will be used for put blocks and put block list operations.
    //It is essentially would be something like "http://127.0.0.1:10000/devstoreaccount1/myblobcontainer/myblobname?sassignature"
    string baseUri = string.Format("{0}/{1}/{2}?{3}", blobStorageEndpoint, blobContainerName, blobName, sasSignature);
    int counter = 0;
    //In this loop, we'll read file in chunks and try and upload one chunk at a time.
    while (true)
    {
    int bytesToRead = blockSize;
    if (bytesRemaining < blockSize)
    {
    bytesToRead = (int)bytesRemaining;
    }
    //Read the file in chunks
    byte[] fileContents = ReadFile(fileToUpload, bytesFrom, bytesToRead);
    bytesRemaining -= fileContents.Length;
    bytesFrom += fileContents.Length;
    //Create block id
    string blockId = string.Format("Block-{0:D5}", counter);
    //Append that to the block id list.
    blockIds.Add(blockId);
    //Now let's upload the block.
    var isBlockUploaded = UploadBlock(baseUri, fileContents, blockId);
    Console.WriteLine("Block Id: " + blockId + " Block Size: " + fileContents.Length + " Uploaded: " + isBlockUploaded);
    counter++;
    if (bytesRemaining <= 0)
    {
    break;
    }
    }
    //All blocks uploaded, now let's commit the block list
    var isBlockListCommitted = CommitBlockList(baseUri, blockIds);
    Console.WriteLine("Is Block List Committed: " + isBlockListCommitted);
    Console.WriteLine("Press any key to terminate the program ....");
    Console.ReadLine();
    }

    /// <summary>
    /// This function reads a chunk of the file and returns that as byte array.
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="bytesFrom"></param>
    /// <param name="bytesToRead"></param>
    /// <returns></returns>
    private static byte[] ReadFile(string fileName, long bytesFrom, int bytesToRead)
    {
    using (FileStream fs = new FileStream(fileName, FileMode.Open))
    {
    byte[] byteArray = new byte[bytesToRead];
    fs.Seek(bytesFrom, SeekOrigin.Begin);
    fs.Read(byteArray, 0, bytesToRead);
    return byteArray;
    }
    }

    /// <summary>
    /// This function uploads a block.
    /// </summary>
    /// <param name="baseUri"></param>
    /// <param name="blockContents"></param>
    /// <param name="blockId"></param>
    /// <returns></returns>
    private static bool UploadBlock(string baseUri, byte[] blockContents, string blockId)
    {
    bool isBlockUploaded = false;
    //Create request URI -
    string uploadBlockUri = string.Format("{0}&comp=block&blockId={1}", baseUri, Convert.ToBase64String(Encoding.UTF8.GetBytes(blockId)));
    // Create request object
    var request = (HttpWebRequest) HttpWebRequest.Create(uploadBlockUri);
    NameValueCollection requestHeaders = new NameValueCollection();
    var requestDate = DateTime.UtcNow;
    //Add request headers. Please note that since we're using SAS URI, we don't really need "Authorization" header.
    requestHeaders.Add("x-ms-date", string.Format(CultureInfo.InvariantCulture, "{0:R}", requestDate));
    requestHeaders.Add("x-ms-version", x_ms_version);
    request.Headers.Add(requestHeaders);
    //Set content length header.
    request.ContentLength = blockContents.Length;
    //Set request HTTP method.
    request.Method = "PUT";

    // Send the request
    using (Stream requestStream = request.GetRequestStream())
    {
    requestStream.Write(blockContents, 0, blockContents.Length);
    }
    // Get the response
    using (var response = (HttpWebResponse)request.GetResponse())
    {
    isBlockUploaded = response.StatusCode.Equals(HttpStatusCode.Created);
    }
    return isBlockUploaded;
    }

    /// <summary>
    /// This function commits the block list.
    /// </summary>
    /// <param name="baseUri"></param>
    /// <param name="blockIds"></param>
    /// <returns></returns>
    private static bool CommitBlockList(string baseUri, List<string> blockIds)
    {
    bool isBlockListCommitted = false;
    //Create the request payload
    StringBuilder blockIdsPayload = new StringBuilder();
    foreach (var blockId in blockIds)
    {
    blockIdsPayload.AppendFormat(blockIdTemplate, Convert.ToBase64String(Encoding.UTF8.GetBytes(blockId)));
    }
    string putBlockListPayload = string.Format(blockListTemplate, blockIdsPayload.ToString());
    // Create request URI
    string putBlockListUrl = string.Format("{0}&comp=blocklist", baseUri);
    // Create request object.
    var request = (HttpWebRequest)HttpWebRequest.Create(putBlockListUrl);
    NameValueCollection requestHeaders = new NameValueCollection();
    //Add request headers. Please note that since we're using SAS URI, we don't really need "Authorization" header.
    var requestDate = DateTime.UtcNow;
    requestHeaders.Add("x-ms-date", string.Format(CultureInfo.InvariantCulture, "{0:R}", requestDate));
    requestHeaders.Add("x-ms-version", x_ms_version);
    byte[] requestPayload = Encoding.UTF8.GetBytes(putBlockListPayload);
    //Set content length header.
    request.ContentLength = requestPayload.Length;
    request.Headers.Add(requestHeaders);
    //Set request HTTP method.
    request.Method = "PUT";

    // Send the request
    using (Stream requestStream = request.GetRequestStream())
    {
    requestStream.Write(requestPayload, 0, requestPayload.Length);
    }

    // Get the response
    using (var response = (HttpWebResponse)request.GetResponse())
    {
    isBlockListCommitted = response.StatusCode.Equals(HttpStatusCode.Created);
    }
    return isBlockListCommitted;

    }
    }

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently building a REST API. All GET methods currently use JSON as response
I would like to use the Pingdom REST API from Classic ASP, but the
In my REST API I would like to use some token based / cookie
We are looking at converting our currently in development WCF REST API to use
I'm building Pylons-based web application with RESTful API, which currently lacks any authentication. So
Does anyone know how to use the TeamCity REST API to find out which
I need to test web API functions in REST format. Currently using Selenium RC
We have a Rest API that requires client certificate authentication. The API is used
I'm opening up a few REST API calls to others to use. Some of
I'm trying to use Bing's REST api to geocode. But my 'y' value is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.