博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.NET 使用 Azure Blob 存储图片或文件

Posted on 2017-07-13 14:10  Hamilton Tan  阅读(1464)  评论(0编辑  收藏  举报

使用的是VS2017

一、先使用 NuGet 获取这两个包。 执行以下步骤:

在“解决方案资源管理器”中,右键单击你的项目并选择“管理 NuGet 包”。

1.在线搜索“WindowsAzure.Storage”,然后单击“安装” 以安装存储客户端库和依赖项。

2.在线搜索“WindowsAzure.ConfigurationManager”,然后单击“安装”以安装 Azure Configuration Manager。

会生成5个dll,如下图:

封装代码如下:

需要先引用

using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

1).AzureBlob.cs

public class AzureBlob
{
    #region 私有变量
    //类的成员,用于创建Blob服务客户端
    CloudBlobClient blobClient;

    //容器和Blob其实就相当于文件夹、文件名

    /// <summary>
    /// 连接字符串
    /// </summary>
    private string storageConnectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
    #endregion

    #region 构造函数创建Blob服务客户端
    public AzureBlob()
    {
        //解析配置中的连接字符串
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
        //创建Blob服务客户端
        blobClient = storageAccount.CreateCloudBlobClient();
    }
    #endregion

    #region 获取块Blob引用
    /// <summary>
    /// 获取块Blob引用
    /// </summary>
    /// <param name="mycontainer">容器名</param>
    /// <param name="fileName">文件名</param>
    /// <returns></returns>
    public CloudBlockBlob GetContainer(string mycontainer, string fileName)
    {
        //获取容器的引用
        CloudBlobContainer container = blobClient.GetContainerReference(mycontainer);
        //获取块 Blob 引用
        CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
        return blob;
    }
    #endregion

    #region 二进制形式上传文件
    /// <summary>
    /// 二进制形式上传文件
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="mycontainer">容器名</param>
    /// <param name="bytes">二进制形式的文件</param>
    /// <returns>异步信息</returns>
    public Task UploadToBlob(string fileName,string mycontainer,byte[] bytes)
    {
        //获取容器的引用
        CloudBlobContainer container = blobClient.GetContainerReference(mycontainer);
        //创建一个容器(如果该容器不存在)
        container.CreateIfNotExists();
        //设置该容器为公共容器,也就是说网络上能访问容器中的文件,但不能修改、删除
        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        //将Blob(文件)上载到容器中,如果已存在同名Blob,则覆盖它
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);//获取块 Blob 引用
        Task result = blockBlob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);//将二进制文件上传
        return result;
    }
    #endregion

    #region 文件路径上传
    /// <summary>
    /// 文件路径上传
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="mycontainer">容器名</param>
    /// <param name="filePath">文件路径</param>
    /// <returns></returns>
    public string UploadToBlob(string fileName, string mycontainer,string filePath)
    {
        ////获取容器的引用
        CloudBlobContainer container = blobClient.GetContainerReference(mycontainer);
        //创建一个容器(如果该容器不存在)
        container.CreateIfNotExists();
        //设置该容器为公共容器,也就是说网络上能访问容器中的文件,但不能修改、删除
        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        //将Blob(文件)上载到容器中,如果已存在同名Blob,则覆盖它
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);//获取块 Blob 引用
        //文件路径
        using (var fileStream = System.IO.File.OpenRead(filePath))
        {
            blockBlob.UploadFromStream(fileStream);
        }
        return blockBlob.Uri.ToString();
    }
    #endregion

    #region 根据文件名和容器名取到Blob地址
    /// <summary>
    /// 根据文件名和容器名取到Blob地址
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="mycontainer">容器名</param>
    /// <returns></returns>
    public string GetBlobURI(string fileName, string mycontainer)
    {
        CloudBlockBlob blob = GetContainer(mycontainer, fileName);
        return blob.Uri.ToString();
    }
    #endregion

    #region 下载Blob
    /// <summary>
    /// 下载Blob
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="mycontainer">容器名</param>
    /// <param name="fliePath">文件路径</param>
    public void DownloadToFile(string fileName, string mycontainer, string fliePath)
    {
        CloudBlockBlob blob = GetContainer(mycontainer, fileName);
        using (var fileStream = File.OpenWrite(fliePath))
        {
            blob.DownloadToStream(fileStream); //将blob保存在指定路径
        }
    }
    #endregion

    #region 下载Blob
    /// <summary>
    /// 下载Blob
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="mycontainer">容器名</param>
    /// <param name="fliePath">文件路径</param>
    public string DownloadToStream(string fileName, string mycontainer)
    {
        CloudBlockBlob blob = GetContainer(mycontainer, fileName);
        string text;
        using (var memoryStream = new MemoryStream())
        {
            blob.DownloadToStream(memoryStream);
            text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
        }
        return text;
    }
    #endregion

    #region 下载Blob
    /// <summary>
    /// 下载Blob
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="mycontainer">容器名</param>
    /// <param name="fliePath">文件路径</param>
    public void DownloadToFileStream(string fileName, string mycontainer, string fliePath)
    {
        CloudBlockBlob blob = GetContainer(mycontainer, fileName);
        using (var fileStream = new FileStream(fliePath, FileMode.OpenOrCreate))
        {
            blob.DownloadToStream(fileStream); //将blob保存在指定路径
        };
    }
    #endregion

    #region 删除Blob
    /// <summary>
    /// 删除Blob
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="mycontainer">容器名</param>
    public void DeleteBlob(string fileName, string mycontainer)
    {
        CloudBlockBlob blob = GetContainer(mycontainer, fileName);
        blob.Delete();
    }
    #endregion
}
View Code

2).单元测试类:class AzureBlob_Test.cs

[TestClass]
public class AzureBlob_Test
{
    AzureBlob azureBlob = new AzureBlob();
    private string containername = ConfigurationManager.AppSettings["userimg"].ToString();

    [TestMethod]
    public void TestUploadToBlob()
    {
        //string imgCode = ""

        //Byte[] buffer = Convert.FromBase64String(imgCode.Replace(" ", "+"));
        //var result = azureBlob.UploadToBlob("test", containername, buffer);

        string str = "userImg/430223198701159158_601421613.png";
        var str1 = str.Contains("/");

        Assert.IsTrue(str1);
    }

    [TestMethod] //OK
    public void TestGetBlobURI() 
    {
        var result = azureBlob.GetBlobURI("test", containername);
        Assert.IsNotNull(result);//https://08afc0c4store.blob.core.chinacloudapi.cn/images/test
    }

    [TestMethod] //OK
    public void TestUploadToBlob2()
    {
        var fileName = "1_2.jpg";
        var filePath = @"F:\img\"+ fileName;
        azureBlob.UploadToBlob(fileName, containername, filePath);
    }

    [TestMethod]
    public void TestDownloadToFile()
    {
        var fileName = "2017.jpg";
        var filePath = @"images\2017.jpg";
        //filePath = azureBlob.GetBlobURI(fileName, containername);
        azureBlob.DownloadToFile(fileName, containername, filePath);
    }

    [TestMethod]
    public void TestDownloadToStream()
    {
        var fileName = "2017.jpg";;
        //filePath = azureBlob.GetBlobURI(fileName, containername);
        azureBlob.DownloadToStream(fileName, containername);
    }

    [TestMethod]
    public void TestDownloadToFileStream()
    {
        var fileName = "2017.jpg"; ;
        var filePath = @"test\2017.jpg";
        //filePath = azureBlob.GetBlobURI(fileName, containername);
        azureBlob.DownloadToFileStream(fileName, containername, filePath);
    }

    [TestMethod] //OK
    public void TestDeleteBlob()
    {
        var fileName = "2017.jpg";
        azureBlob.DeleteBlob(fileName, containername);
    }
}
View Code

3).在web.config 下的<configuration>-><appSettings>下添加:

<!--存储图片连接字符串 -->
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=test;AccountKey=1234;EndpointSuffix=core.chinacloudapi.cn" />
<!--容器名,必须是小写-->
<add key="test" value="test" />

4).通过Azure Storage Explorer 6查看上传的图片,下载地址为:http://azurestorageexplorer.codeplex.com/releases/view/125870 下的 AzureStorageExplorer6Preview3.zip

5).图片访问路径:https://(AccountName).blob.(EndpointSuffix)/ecgimg1/test.png

6).通过html转pdf 微软云图片的路径(5))导出显示不了,只能先下载到本地,导出pdf成功之后,删除本地的图片。

参考:http://www.jianshu.com/p/bf265c6ceedd

          https://docs.azure.cn/zh-cn/storage/storage-dotnet-how-to-use-blobs 

相关开发资源 https://docs.azure.cn/zh-cn/storage/         

对比C#上传 

1. 图片上传类:

FileExtensions.cs

public class FileExtensions
{
    //    <!--图片上传路径-->
    public static readonly string filePath = AppDomain.CurrentDomain.BaseDirectory + @"img\";

    /// <summary>
    /// 将数据写入文件
    /// </summary>
    /// <param name="data"></param>
    /// <param name="url"></param>
    /// <returns></returns>
    public static void WriterBase64File(string data, string url)
    {
        var path = Path.Combine(filePath, url.Replace("/", "\\"));
        var dir = path.Substring(0, path.LastIndexOf("\\"));
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }

        if (File.Exists(path))
        {
            File.Delete(path);
        }

        Byte[] buffer = Convert.FromBase64String(data.Replace(" ", "+"));
        File.WriteAllBytes(path, buffer);
    }

    /// <summary>
    /// 将数据写入文件
    /// </summary>
    /// <param name="data"></param>
    /// <param name="url"></param>
    /// <returns></returns>
    public static void WriterImgFile(Bitmap data, string url)
    {
        var path = Path.Combine(filePath, url.Replace("~/", "").Replace("/", "\\"));
        var dir = path.Substring(0, path.LastIndexOf("\\"));
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }

        if (File.Exists(path))
        {
            File.Delete(path);
        }

        data.Save(path);
    }

    /// <summary>
    /// 将数据写入文件
    /// </summary>
    /// <param name="data"></param>
    /// <param name="url"></param>
    /// <returns></returns>
    public static void WriterBinaryFile(byte[] data, string url)
    {
        var path = Path.Combine(filePath, url.Replace("/", "\\"));
        var dir = path.Substring(0, path.LastIndexOf("\\"));
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }

        if (File.Exists(path))
        {
            File.Delete(path);
        }

        File.WriteAllBytes(path, data);
    }

    /// <summary>
    /// 图片旋转
    /// </summary>
    /// <param name="sm"></param>
    /// <param name="max"></param>
    /// <returns></returns>
    public static byte[] ResizeImageFile(Stream sm, int max = 1024)
    {
        Image original = Image.FromStream(sm);
        decimal ratio = (decimal)1.0;
        int targetW = original.Width;
        int targetH = original.Height;
        if (targetW <= max && targetH <= max)
        {
            return StreamToBytes(sm);
        }
        if (original.Height > original.Width)
        {
            ratio = (decimal)max / original.Height;
            targetH = 1024;
            targetW = (int)((decimal)targetW * ratio);
        }
        else
        {
            ratio = (decimal)max / original.Width;
            targetW = 1024;
            targetH = (int)((decimal)targetH * ratio);
        }

        Image imgPhoto = Image.FromStream(sm);
        // Create a new blank canvas.  The resized image will be drawn on this canvas.
        Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(72, 72);
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
        grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width,
            original.Height, GraphicsUnit.Pixel);
        // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
        MemoryStream mm = new MemoryStream();
        bmPhoto.Save(mm, ImageFormat.Jpeg);
        original.Dispose();
        imgPhoto.Dispose();
        bmPhoto.Dispose();
        grPhoto.Dispose();
        return mm.GetBuffer();
    }

    /// <summary>
    /// 根据图片exif调整方向  
    /// </summary>
    /// <param name="byteArray"></param>
    /// <param name="orien">angle 1:逆时针90,2:逆时针180,3:逆时针270</param>
    /// <returns></returns>
    public static Bitmap RotateImage(byte[] byteArray, int orien)
    {
        MemoryStream sm = new MemoryStream(byteArray);
        Image img = Image.FromStream(sm);
        switch (orien)
        {
            case 1:
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;
            case 2:
                img.RotateFlip(RotateFlipType.Rotate180FlipNone); //vertical flip  
                break;
            case 3:
                img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;
            default:
                break;
        }
        return (Bitmap)img;
    }


    public static byte[] BitmapToBytes(Bitmap bitmap)
    {
        using (var ms = new MemoryStream())
        {
            bitmap.Save(ms, ImageFormat.Jpeg);
            var byteImage = ms.ToArray();
            return byteImage;
        }
    }


    public static byte[] StreamToBytes(Stream sm)
    {
        byte[] bytes = new byte[sm.Length];
        sm.Read(bytes,0, bytes.Length);
        //  设置当前流的位置为流的开始
        sm.Seek(0,SeekOrigin.Begin);
        return bytes;
    }
   }
}
FileExtensions

调用:

//限定上传图片的格式类型
string[] LimitPictureType = { ".jpg", ".jpeg", ".gif", ".png", ".bmp" };
//当图片上被选中时,拿到文件的扩展名
string currentPictureExtension = Path.GetExtension(file.FileName).ToLower();

//文件名称
var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + currentPictureExtension;

//图片字节流
Byte[] fileData = new Byte[file.ContentLength]; //file 是 System.Web.HttpPostedFile;
using (var binaryReader = new BinaryReader(file.InputStream))
{
    fileData = binaryReader.ReadBytes(file.ContentLength);
}

//图片上传
FileExtensions.WriterBinaryFile(fileData, fileName);


 //图片字节流
Byte[] fileData = FileExtensions.ResizeImageFile(file.InputStream);


// angle 1:逆时针90,2:逆时针180,3:逆时针270
using (var img = FileExtensions.RotateImage(fileData, angle))
{
    fileData = FileExtensions.BitmapToBytes(img);
    AzureBlob.UploadToBlob(fileName, containname, fileData);
}

 2.C#获取某站点下的图片文件二进制字节流

#region 获取图片的二进制流
/// <summary>
/// 获取图片的二进制流
/// </summary>
/// <param name="path">h5上保存路边的具体地址(比如:http://10.2.29.176:8082/img/20180320.jpg )</param>
/// <returns></returns>
private static byte[] GetfileData(string path)
{
    WebRequest myrequest = WebRequest.Create(path);
    WebResponse myresponse = myrequest.GetResponse();
    Stream imgstream = myresponse.GetResponseStream();
    Image img = Image.FromStream(imgstream);
    MemoryStream ms = new MemoryStream();
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    var fileData = ms.ToArray();
    return fileData;
}
#endregion

 3. Http上传文件 

#region Http上传文件
/// <summary>
/// Http上传文件
/// </summary> 
/// <param name="url">url</param>
/// <param name="contentType">类型:multipart/form-data</param>
/// <param name="fileName">文件名称</param>
/// <param name="bArr">文件二进制字节流</param>
/// <param name="uploadfilename">input type='file' 对应的name</param>
/// <returns></returns>
public static string HttpUploadFile(string url,string contentType,string fileName,byte[] bArr,string uploadfilename)
{
    // 设置参数
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    CookieContainer cookieContainer = new CookieContainer();
    request.CookieContainer = cookieContainer;
    request.AllowAutoRedirect = true;
    request.Method = "POST";
    string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
    request.ContentType = string.Format("{0};charset=utf-8;boundary={1}", contentType, boundary);
    byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
    byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");

    //请求头部信息 
    StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n", uploadfilename, fileName));
    byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());

    Stream postStream = request.GetRequestStream();
    postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
    postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
    postStream.Write(bArr, 0, bArr.Length);
    postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
    postStream.Close();

    //发送请求并获取相应回应数据
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    //直到request.GetResponse()程序才开始向目标网页发送Post请求
    Stream instream = response.GetResponseStream();
    StreamReader sr = new StreamReader(instream, Encoding.UTF8);
    //返回结果网页(html)代码
    string content = sr.ReadToEnd();
    return content;
}
#endregion

调用:HttpUploadFile(‘具体的url’, "multipart/form-data", fileName, fileData, "uploadfile1");

注意:uploadfile 为<input type="file" name="uploadfile1" style="font-size:14px"> 中 name的值

对应的form表单为:

<form enctype="multipart/form-data" action="/upload" method="post">
    <input type="file" name="uploadfile1" style="font-size:14px">
    <input type="submit" value="upload" style="font-size:14px">
</form>