繁体   English   中英

如何在浏览器上打开文件,而不是在本地下载存储在 Azure Data Lake Gen 2 中的文件

[英]How to open the file on browser instead of downloading in local that is stored in Azure Data lake Gen 2

目前,我正在从 Azure 文件共享生成 SAS 令牌并尝试访问该文件。 但是当我在浏览器上点击该网址时,文件会自动下载而不是直接在浏览器上打开。

我的目标是从 Azure 文件共享生成 SAS 令牌,并直接在浏览器上打开该文件(.pdf、.txt、.mkv、.docx),而无需使用该 SAS 令牌进行下载。

• 当您尝试使用 SAS 令牌 URI 访问文件共享上的图像时,它们必然会被下载,因为它们应该由 SAS 令牌访问,而不是在浏览器本身中显示数据。 就好像存储在文件共享中的图像或任何数据要在浏览器本身上打开和显示一样,这将违反隐私和保护它的数据加密 因此,考虑到存储在 Azure 存储中的文件的安全性和可访问性,使用 SAS 令牌访问文件时会下载这些文件。

但是,您肯定可以在打印之前以预览状态在浏览器中打开文件/图像,方法是creating a web app service that uploads images/files in a blob storage account and displays it upon request by using the SAS URI token 为此,您必须创建一个 ASP .Net MVC 应用程序并将其配置为使用 SAS URI 而不是帐户密钥来从 blob 存储帐户检索图像。 请在下面找到网络应用程序的源代码:-

     //ViewModel
   public class ViewModel
  {
   public string FileUrl { get; set; }
    }


    public ActionResult Index()

     {
         var readPolicy = new SharedAccessBlobPolicy()
         {
             Permissions = SharedAccessBlobPermissions.Read,
             SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(5)
         };
            
         // Retrieve storage account from connection string.
         string conn = "DefaultEndpointsProtocol=https;AccountName=straccountname;AccountKey=key==;EndpointSuffix=core.windows.net";
         Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = CloudStorageAccount.Parse(conn);
         // Create the blob client.
         CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
         // Retrieve reference to a previously created container.
         CloudBlobContainer container = blobClient.GetContainerReference("test");
         // Retrieve reference to a blob ie "20200809_125724.jpg".
         CloudBlockBlob blockBlob = container.GetBlockBlobReference("20200809_125724.jpg");
         //------
         var newUri = new Uri(blockBlob.Uri.AbsoluteUri + blockBlob.GetSharedAccessSignature(readPolicy));
         var viewModel = new ViewModel()
         {
             FileUrl = newUri.ToString()
         };
         return View("Index", viewModel);
         // return View();
     } 

此外,有关更多信息和详细信息,我建议您参考以下链接:-

https://docs.microsoft.com/en-us/answers/questions/252303/sas-url-to-display-in-browser-rather-than-download.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM