title: Upload files to Azure Blob storage through WebApi without accessing local filesystem
body: <p>I'm trying to upload files from local command line client to Azure storage through web-api.
  I'm using Azure Web-Site for that. Working with the client is not a problem. And I've got everything working locally fine. Here is the web-api code:</p>
  
  <pre><code>    public async Task&lt;HttpResponseMessage&gt; PostUpload()
      {
          // need a local resource to store uploaded files temporarily
          LocalResource localResource = null;
          try
          {
              // Azure web-site fails here
              localResource = RoleEnvironment.GetLocalResource("TempStorage");
          }
          catch (Exception e)
          {
              return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Unable to get access to local resources");
          }
  
          var provider = new MultipartFormDataStreamProvider(localResource.RootPath);
  
          // Read the form data.
          await Request.Content.ReadAsMultipartAsync(provider);
  
          // snipped validation code
          var container = // code to get container
  
          foreach (var fileData in provider.FileData)
          {
              var filename = GetBlobName(fileData);
              var blob = container.GetBlockBlobReference(filename);
              using (var filestream = File.OpenRead(fileData.LocalFileName))
              {
                  blob.UploadFromStream(filestream);
              }
              File.Delete(fileData.LocalFileName);
          }
  
          return Request.CreateResponse(HttpStatusCode.OK);
      }
  </code></pre>
  
  <p>Everything works fine when I run locally, but as soon as I deploy web-site in Azure, I can't upload, because Azure Web-Sites don't have access to LocalResource. And I'll need to switch to  Azure Web-Role. I can switch, but accessing local file system is bothering me all together. </p>
  
  <p>And LocalResource is required for instance of MultipartFormDataStreamProvider(). And I have not found alternative ways to upload files to WebApi. My plan was to <a href="http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/" rel="nofollow">channel through upload directly to Azure</a>, without storing anything on a local HDD.</p>
  
  <p>Is there any other way to upload files?</p>
  
  <p>p.s. I have seen <a href="http://blog.smarx.com/posts/shared-access-signatures-are-easy-these-days" rel="nofollow">usages of Shared Access Signatures</a> where I can give client application a url with signature and let the client upload directly to Azure Blog. But I'm not sure about how secure that is going to be and not really comfortable (yet) with passing the signatures down to the client. At the moment I presume the client is going to be run in very hostile environment and nothing can be trusted coming back from the client.</p>
  
  <p><strong>UPD</strong> My final solution involved using write only Shared Access Signature issued on the server and passed down to the client. And client then uploads files directly to Azure. This way I save a lot of hassle with managing uploaded files. And here is <a href="http://tech.trailmax.info/2013/07/upload-files-to-azure-blob-storage-with-using-shared-access-keys/" rel="nofollow">more detailed description</a> of my solution.</p>