Upload file to Azure Blob Storage directly from browser?

后端 未结 5 835
北海茫月
北海茫月 2020-12-08 10:43

Is it possible to create an html form to allow web users to upload files directly to azure blob store without using another server as a intermediary? S3 and GAW blobstore b

相关标签:
5条回答
  • 2020-12-08 10:58

    EDIT November 2019

    You can now refer to the official documentation:

    • Azure Storage JavaScript Client Library Sample for Blob Operations
    • Azure Storage client library for JavaScript

    Initial answer

    There is a New Azure Storage JavaScript client library for browsers (Preview).

    (Everything from this post comes from the original article above)

    • The JavaScript Client Library for Azure Storage enables many web development scenarios using storage services like Blob, Table, Queue, and File, and is compatible with modern browsers
    • The new JavaScript Client Library for Browsers supports all the storage features available in the latest REST API version 2016-05-31 since it is built with Browserify using the Azure Storage Client Library for Node.js

    We highly recommend use of SAS tokens to authenticate with Azure Storage since the JavaScript Client Library will expose the authentication token to the user in the browser. A SAS token with limited scope and time is highly recommended. In an ideal web application it is expected that the backend application will authenticate users when they log on, and will then provide a SAS token to the client for authorizing access to the Storage account. This removes the need to authenticate using an account key. Check out the Azure Function sample in our Github repository that generates a SAS token upon an HTTP POST request.

    Code sample:

    1. Insert the following script tags in your HTML code. Make sure the JavaScript files located in the same folder.

      <script src="azure-storage.common.js"></script/>
      <script src="azure-storage.blob.js"></script/>
      
    2. Let’s now add a few items to the page to initiate the transfer. Add the following tags inside the BODY tag. Notice that the button calls uploadBlobFromText method when clicked. We will define this method in the next step.

      <input type="text" id="text" name="text" value="Hello World!" />
      <button id="upload-button" onclick="uploadBlobFromText()">Upload</button>
      
    3. So far, we have included the client library and added the HTML code to show the user a text input and a button to initiate the transfer. When the user clicks on the upload button, uploadBlobFromText will be called. Let’s define that now:

      <script>
      function uploadBlobFromText() {
          // your account and SAS information
          var sasKey ="....";
          var blobUri = "http://<accountname>.blob.core.windows.net";
          var blobService = AzureStorage.createBlobServiceWithSas(blobUri, sasKey).withFilter(new AzureStorage.ExponentialRetryPolicyFilter());
          var text = document.getElementById('text');
          var btn = document.getElementById("upload-button");
          blobService.createBlockBlobFromText('mycontainer', 'myblob', text.value,  function(error, result, response){
              if (error) {
                  alert('Upload filed, open browser console for more detailed info.');
                  console.log(error);
              } else {
                  alert('Upload successfully!');
              }
          });
      }
      </script>
      
    0 讨论(0)
  • 2020-12-08 11:12

    Do take a look at these blog posts for uploading files directly from browser to blob storage:

    http://coderead.wordpress.com/2012/11/21/uploading-files-directly-to-blob-storage-from-the-browser/

    http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript

    The 2nd post (written by me) makes use of HTML 5 File API and thus would not work in all browsers.

    The basic idea is to create a Shared Access Signature (SAS) for a blob container. The SAS should have Write permission. Since Windows Azure Blob Storage does not support CORS yet (which is supported by both Amazon S3 and Google), you would need to host the HTML page in the blob storage where you want your users to upload the file. Then you can use jQuery's Ajax functionality.

    0 讨论(0)
  • 2020-12-08 11:16

    I have written a blog post with an example on how to do this, the code is at GitHub

    It is based on Gaurav Mantris post and works by hosting the JavaScript on the Blob Storage itself.

    0 讨论(0)
  • 2020-12-08 11:21

    Now that Windows Azure storage services support CORS, you can do this. You can see the announcement here: Windows Azure Storage Release - Introducing CORS, JSON, Minute Metrics, and More.

    I have a simple example that illustrates this scenario here: http://www.contentmaster.com/azure/windows-azure-storage-cors/

    The example shows how to upload and download directly from a private blob using jQuery.ajax. This example still requires a server component to generate the shared access signature: this avoids the need to expose the storage account key in the client code.

    0 讨论(0)
  • 2020-12-08 11:24

    You can use HTML5 File API, AJAX and MVC 3 to build a robust file upload control to upload huge files securely and reliably to Windows Azure blob storage with a provision of monitoring operation progress and operation cancellation. The solution works as below:

    1. Client-side JavaScript that accepts and processes a file uploaded by user.
    2. Server-side code that processes file chunks sent by JavaScript.
    3. Client-side UI that invokes JavaScript.

    Get the sample code here: Reliable Uploads to Windows Azure Blob Storage via an HTML5 Control

    0 讨论(0)
提交回复
热议问题