|
@@ -0,0 +1,110 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Net.Http.Headers;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using HaBookCms.AzureBlobStorageTest.Models;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.WindowsAzure.Storage;
|
|
|
+using Microsoft.WindowsAzure.Storage.Blob;
|
|
|
+
|
|
|
+namespace HaBookCms.AzureBlobStorageTest.Controllers
|
|
|
+{
|
|
|
+ [Route("api/[controller]")]
|
|
|
+ [ApiController]
|
|
|
+ public class ValuesController : ControllerBase
|
|
|
+ {
|
|
|
+ // GET api/values
|
|
|
+ [HttpGet]
|
|
|
+ public ActionResult<IEnumerable<string>> Get()
|
|
|
+ {
|
|
|
+ return new string[] { "value1", "value2" };
|
|
|
+ }
|
|
|
+
|
|
|
+ // GET api/values/5
|
|
|
+ [HttpGet("{id}")]
|
|
|
+ public ActionResult<string> Get(int id)
|
|
|
+ {
|
|
|
+ return "value";
|
|
|
+ }
|
|
|
+
|
|
|
+ // POST api/values
|
|
|
+ [HttpPost]
|
|
|
+ public void Post([FromBody] string value)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ // PUT api/values/5
|
|
|
+ [HttpPut("{id}")]
|
|
|
+ public void Put(int id, [FromBody] string value)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ // DELETE api/values/5
|
|
|
+ [HttpDelete("{id}")]
|
|
|
+ public void Delete(int id)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ [HttpPost("urls")]
|
|
|
+ public async Task<IActionResult> Index()
|
|
|
+ {
|
|
|
+ var urls = new List<string>();
|
|
|
+ var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn");
|
|
|
+ var blobClient = storageAccount.CreateCloudBlobClient();
|
|
|
+ var container = blobClient.GetContainerReference("staticcontent");
|
|
|
+ var data = await container.ListBlobsSegmentedAsync
|
|
|
+ ("", true, BlobListingDetails.All, 200, null, null, null);
|
|
|
+ foreach (IListBlobItem item in data.Results)
|
|
|
+ {
|
|
|
+ urls.Add(item.StorageUri.PrimaryUri.ToString());
|
|
|
+ }
|
|
|
+ return Ok(urls);
|
|
|
+ }
|
|
|
+ [HttpPost("Upload")]
|
|
|
+ //[Produces("application/json")]
|
|
|
+ //[Consumes("application/json", "multipart/form-data")]//此处为新增
|
|
|
+ public async Task<IActionResult> Upload(IFormFile[] file )
|
|
|
+ {
|
|
|
+ var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn");
|
|
|
+ var blobClient = storageAccount.CreateCloudBlobClient();
|
|
|
+ var container = blobClient.GetContainerReference("staticcontent");
|
|
|
+ //var file = picture.File;
|
|
|
+ foreach (IFormFile f in file) {
|
|
|
+ var parsedContentDisposition =
|
|
|
+ ContentDispositionHeaderValue.Parse(f.ContentDisposition);
|
|
|
+ var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
|
|
|
+ var blockBlob = container.GetBlockBlobReference(filename+Guid.NewGuid());
|
|
|
+ await blockBlob.UploadFromStreamAsync(f.OpenReadStream());
|
|
|
+ }
|
|
|
+ return Ok();
|
|
|
+ }
|
|
|
+ [HttpGet("Download")]
|
|
|
+ //[Produces("application/json")]
|
|
|
+ //[Consumes("application/json", "multipart/form-data")]//此处为新增
|
|
|
+ public async Task<IActionResult> Download()
|
|
|
+ {
|
|
|
+ var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn");
|
|
|
+ var blobClient = storageAccount.CreateCloudBlobClient();
|
|
|
+ var container = blobClient.GetContainerReference("staticcontent");
|
|
|
+ string url = "https://teammodelstorage.blob.core.chinacloudapi.cn/staticcontent/234fa2a1-7309-4241-886f-c58ae195f9da.pdf";
|
|
|
+ CloudBlockBlob blockBlob = container.GetBlockBlobReference("234fa2a1-7309-4241-886f-c58ae195f9da.pdf");
|
|
|
+ using (var fileStream = System.IO.File.OpenWrite(@"F:\234fa2a1-7309-4241-886f-c58ae195f9da.pdf"))
|
|
|
+ {
|
|
|
+ await blockBlob.DownloadToStreamAsync(fileStream);
|
|
|
+ }
|
|
|
+ //var file = picture.File;
|
|
|
+ //foreach (IFormFile f in file)
|
|
|
+ //{
|
|
|
+ // var parsedContentDisposition =
|
|
|
+ // ContentDispositionHeaderValue.Parse(f.ContentDisposition);
|
|
|
+ // var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
|
|
|
+ // var blockBlob = container.GetBlockBlobReference(filename + Guid.NewGuid());
|
|
|
+ // await blockBlob.UploadFromStreamAsync(f.OpenReadStream());
|
|
|
+ //}
|
|
|
+ return Ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|