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 }

 

posted @ 2021-10-22 10:31  ㄌㄟ  阅读(190)  评论(0编辑  收藏  举报