paparazzo
paparazzo

Reputation: 45096

Upload file to Wep App from browser with out a storage account key

Cleary I don't want to give out the Azure Storage account key

How can I load a local file to a BLOB storage without the Azure Storage account key from a browser?
Or can it even be done?

I get from a thick .NET client can do it with SAS.

Azure File storage

We currently do not support AD-based authentication or ACLs, but do have it in our list of feature requests. For now, the Azure Storage account keys are used to provide authentication to the file share.

This wants a path FileUpload Class

protected void UploadButton_Click(object sender, EventArgs 
{
    // Specify the path on the server to
    // save the uploaded file to.
    String savePath = @"c:\temp\uploads\";

// Before attempting to perform operations
// on the file, verify that the FileUpload 
// control contains a file.
if (FileUpload1.HasFile)
{
  // Get the name of the file to upload.
  String fileName = FileUpload1.FileName;

  // Append the name of the file to upload to the path.
  savePath += fileName;


  // Call the SaveAs method to save the 
  // uploaded file to the specified path.
  // This example does not perform all
  // the necessary error checking.               
  // If a file with the same name
  // already exists in the specified path,  
  // the uploaded file overwrites it.
  FileUpload1.SaveAs(savePath);

I don't see how you could use a SAS as an upload end point.
I don't see anyway in ASP.NET way to upload a file to a blob.

Upvotes: 0

Views: 96

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

Some differences between Azure Blob Storage and Azure File Storage are as follows:

  • Blob Storage could store unstructured object data (e.g. text or binary data, such as a document, media file).
  • File Storage offers shared storage for legacy applications using the standard SMB protocol. Azure File Service is targeted to internal file handling which means that you could mount a file share to a VM in the cloud or on-premises. Azure VM could share files across applications via mounted shares, and on-premises applications could access files via the File service REST API.

For more details, I recommend you reading the following tutorials:

https://blogs.msdn.microsoft.com/windowsazurestorage/2014/05/12/introducing-microsoft-azure-file-service/

SAS: It's a ideal approach to grant the limited access to resources in your storage account to other clients, without having to expose your account key. For more details, you could refer to Using Shared Access Signatures (SAS).

According to your requirement, I assumed that you could use Blob Storage to store your files. Please follow the steps below to achieve your purpose:

Generate a SAS URL on a Container

For a simple way, you could use Microsoft Azure Storage Explorer to create a SAS. For more details, you could follow this turtorial.

Note: You need to create a SAS with Write permission on the blob container which is used to store the uploaded files.

UploadFile.aspx.cs

protected void UploadButton_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        string containerSasUrl = "https://brucechen.blob.core.windows.net/blob-container-01?st=2016-09-21T01%3A59%3A00Z&se=2016-10-01T01%3A59%3A00Z&sp=rw&sv=2015-04-05&sr=c&sig=xGbgBMypxKP%2FBXAHFuzv%2FabqZqjM3W89JYjgf5uvdHo%3D";
        try
        {
            string uploadedFileUrl = string.Empty;
            var file = FileUpload1.PostedFile;
            string blobName = FileUpload1.FileName;
            //Retrieve a reference to a container.
            CloudBlobContainer container = new CloudBlobContainer(new Uri(containerSasUrl));
            //Retrieve reference to a blob named "blobName".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
            blockBlob.UploadFromStreamAsync(file.InputStream);
            //Retrieve the uploaded file(blob) url
            uploadedFileUrl = blockBlob.Uri.ToString();
        }
        catch (Exception ex)
        {
            //TODO:log
        }
    }
}

Additionally, you could manage your blob(file) resources and provide public or the specify desired access level to others. For a better understanding of it, you could refer to this tutorial.

Upvotes: 1

Related Questions