• Azure Blob上传和下载


    添加NuGet包:

    <package id="Azure.Core" version="1.19.0" targetFramework="net472" />
    <package id="Azure.Storage.Blobs" version="12.10.0" targetFramework="net472" />
    <package id="Azure.Storage.Common" version="12.9.0" targetFramework="net472" />

     1 public JsonResult PreApprovalDocFileUpLoadTest(FormCollection form)
     2 {
     3     try
     4     {
     5         if (Request.Files != null && Request.Files.Count > 0)
     6         {
     7             string asposePath = Server.MapPath(ConfigurationManager.AppSettings["AsposeLicensePath"]);
     8             Aspose.Email.License emailicense = new Aspose.Email.License();
     9             emailicense.SetLicense(asposePath);
    10             Aspose.Words.License wordLicense = new Aspose.Words.License();
    11             wordLicense.SetLicense(asposePath);
    12 
    13             var accountName = ConfigurationManager.AppSettings["Azure_AccountName"];
    14             var accountKey = ConfigurationManager.AppSettings["Azure_AccountKey"];
    15             var containerName = ConfigurationManager.AppSettings["Azure_ContainerName"];
    16 
    17             var connectionString = $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accountKey};EndpointSuffix=core.windows.net";
    18 
    19 
    20             for (int i = 0; i < Request.Files.Count; i++)
    21             {
    22                 var file = Request.Files[i];
    23                 var fileName = file.FileName;
    24                 var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
    25                 var extensionName = Path.GetExtension(fileName);
    26                 var mailMsg = Aspose.Email.MailMessage.Load(file.InputStream);
    27                 var ms = new MemoryStream();
    28 
    29                 mailMsg.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);
    30 
    31                 // create an instance of LoadOptions and set the LoadFormat to Mhtml
    32                 var loadOptions = new Aspose.Words.Loading.LoadOptions();
    33                 loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;
    34 
    35                 // create an instance of Document and load the MTHML from MemoryStream
    36                 var document = new Aspose.Words.Document(ms, loadOptions);
    37 
    38                 // create an instance of HtmlSaveOptions and set the SaveFormat to Html
    39                 var saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
    40 
    41                 var filePath = $"C:\GroupM\Temp\{fileNameWithoutExtension}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.pdf";
    42                 document.Save(filePath, saveOptions);
    43 
    44                 var blobName = $"{fileNameWithoutExtension.Replace(" ", "_")}_{ DateTime.Now.ToString("yyyyMMddHHmmss") }.pdf";
    45 
    46                 // intialize BobClient 
    47                 var blobClient = new BlobClient(
    48                     connectionString: connectionString,
    49                     blobContainerName: containerName,
    50                     blobName: blobName);
    51 
    52                 // upload the file
    53                 blobClient.Upload(filePath);
    54 
    55                 var storageSharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
    56                 var sasBuilder = new AccountSasBuilder()
    57                 {
    58                     Services = AccountSasServices.Blobs,
    59                     ResourceTypes = AccountSasResourceTypes.Object,
    60                     ExpiresOn = DateTimeOffset.UtcNow.AddHours(1),
    61                     Protocol = SasProtocol.Https
    62                 };
    63 
    64                 sasBuilder.SetPermissions(AccountSasPermissions.Read | AccountSasPermissions.List);
    65 
    66                 // Use the key to get the SAS token.
    67                 var sasToken = sasBuilder.ToSasQueryParameters(storageSharedKeyCredential).ToString();
    68                 var downloadUrl = $"https://{accountName}.blob.core.windows.net/{containerName}/{blobName}?{sasToken}";
    69                 return Json(new { status = 0, result = downloadUrl });
    70             }
    71         }
    72         else
    73         {
    74             return Json(new { message = "Choose to upload a file.", returnValue = 0 });
    75         }
    76 
    77     }
    78     catch (Exception ex)
    79     {
    80         LogManager.Logger.Error(ex.ToString());
    81         return Json(new { status = -1, result = ex.Message });
    82     }
    83     return Json(new { status = -1, result = "You don't have permission to upload this file!" });
    84 }
  • 相关阅读:
    小程序 订阅消息功能实现 wx.requestSubscribeMessage
    Azure API Management (2) API Managment 保护内网服务,且通过JWT验证
    Azure Firewall (1) Azure虚拟桌面结合Azure防火墙设置访问白名单
    一文详解脏读、不可重复读、幻读
    SpringBoot引入第三方jar的Bean的三种方式
    单例模式,真不简单
    一文详解MySQL的锁机制
    高并发下秒杀商品,必须知道的9个细节
    MySQL记录锁、间隙锁、临键锁小案例演示
    Git五个常见问题及解决方法
  • 原文地址:https://www.cnblogs.com/elliot-lei/p/15437604.html
Copyright © 2020-2023  润新知