【问题标题】:Upload file to Wep App from browser with out a storage account key将文件从浏览器上传到 Web 应用,无需存储帐户密钥
【发布时间】:2017-01-29 01:00:16
【问题描述】:

很清楚,我不想泄露 Azure 存储帐户密钥

如何在没有 Azure 存储帐户密钥的情况下从浏览器将本地文件加载到 BLOB 存储?
或者甚至可以做到吗?

我从一个厚实的 .NET 客户端获得,可以使用 SAS 来完成。

Azure File storage

我们目前不支持基于 AD 的身份验证或 ACL,但支持 将其包含在我们的功能请求列表中。目前,Azure 存储 帐户密钥用于为文件共享提供身份验证。

这需要一条路径 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);

我不明白如何使用 SAS 作为上传端点。
无论如何,我看不到将文件上传到 blob 的 ASP.NET 方式。

【问题讨论】:

标签: asp.net azure file-upload azure-blob-storage


【解决方案1】:

Azure Blob 存储和 Azure 文件存储的一些区别如下:

  • Blob 存储可以存储非结构化对象数据(例如文本或二进制数据,例如文档、媒体文件)。
  • 文件存储使用标准 SMB 协议为遗留应用程序提供共享存储。 Azure 文件服务面向内部文件处理,这意味着您可以将文件共享装载到云中或本地的 VM。 Azure VM 可以通过挂载共享在应用程序之间共享文件,本地应用程序可以通过文件服务 REST API 访问文件。

有关详细信息,我建议您阅读以下教程:

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

SAS:这是一种理想的方法,可以将您存储帐户中的资源的有限访问权限授予其他客户端,而无需公开您的帐户密钥。更多详情可以参考Using Shared Access Signatures (SAS)

根据您的要求,我假设您可以使用 Blob Storage 来存储您的文件。请按照以下步骤实现您的目的:

在容器上生成 SAS URL

对于一个简单的方法,您可以使用Microsoft Azure Storage Explorer 创建一个 SAS。更多详情可以关注turtorial

注意:您需要在用于存储上传文件的 blob 容器上创建具有写入权限的 SAS。

上传文件.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
        }
    }
}

此外,您可以管理您的 blob(文件)资源并向其他人提供公共或指定所需的访问级别。为了更好地理解它,你可以参考这个tutorial

【讨论】:

  • 不错。还没有测试给它一个检查。这应该在 Azure 存储团队博客上
猜你喜欢
  • 2021-10-20
  • 2021-12-25
  • 2016-09-23
  • 1970-01-01
  • 2013-03-18
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
相关资源
最近更新 更多