Upload to Shared Access Signature blob using WebClient (REST API)

14 ביולי 2012

tags: ,
2 comments

I want asked by a client how to upload a blob (Put blob) to a SAS (Shared Access Signature) blob using the REST Api.

Here is a simple code snippet demonstrating that using WebClient.

class Program
{
   private static CloudBlobContainer m_container;

   static void Main(string[] args)
   {
      try
      {
         var m_StorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
         var m_BlobClient = m_StorageAccount.CreateCloudBlobClient();
         m_container = m_BlobClient.GetContainerReference("myContainer");
         m_container.CreateIfNotExist();

         BlobContainerPermissions permissions = new BlobContainerPermissions();
         // The container itself doesn't allow public access.
         permissions.PublicAccess = BlobContainerPublicAccessType.Off;
                
         // The container itself doesn't allow SAS access.
         var containerPolicy = new SharedAccessPolicy() { Permissions = SharedAccessPermissions.None };
         permissions.SharedAccessPolicies.Clear();
         permissions.SharedAccessPolicies.Add("TestPolicy", containerPolicy);
         m_container.SetPermissions(permissions);
             
         var uri = GetSharedAccessSignature("b1", DateTime.Now.AddDays(1));
         var client = new WebClient();
         client.UploadFile(uri,"PUT", "b1.txt");
         Console.WriteLine("Done");
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex.ToString());
      }
      Console.ReadLine();
    }


   public static string GetSharedAccessSignature(string objId, DateTime expiryTime)
   {
     CloudBlob blob = m_container.GetBlobReference(objId);

     var blobAccess = new SharedAccessPolicy
     {
         Permissions = SharedAccessPermissions.Write,
         SharedAccessExpiryTime = expiryTime
     };

     return blob.Uri + blob.GetSharedAccessSignature(blobAccess, "TestPolicy");
   }

}
Enjoy
Manu
Add comment
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

תגי HTML מותרים: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2 comments

  1. Pearl8 באוגוסט 2012 ב 20:44

    You've made some decent points there. I looked on the net for more information about the issue and found most people will go along with your views on this website.

    להגיב
  2. SoinoVarasess23 באפריל 2013 ב 5:23

    [URL=][/URL]

    להגיב