chenpeng

C#对象存储上传阿里云的帮助类
using System; using System.Drawing; using System.Drawing....
扫描右侧二维码阅读全文
31
2019/08

C#对象存储上传阿里云的帮助类

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Aliyun.OSS;

namespace Auxiliary
{
public class OssALiHelper
{
    private string _accessKeyId = string.Empty;
    private string _accessKeySecret = string.Empty;
    private string _endpoint = string.Empty;
    private string _bucketName = string.Empty;

    /// <summary>
    /// 实例化构造函数
    /// </summary>
    /// <param name="accessKeyId">accessKeyId,例如:</param>
    /// <param name="accessKeySecret">Secret,例如:</param>
    /// <param name="endpoint">地域节点,例如:</param>
    /// <param name="bucketName">bucket名称,该名称在阿里云是唯一的,申请时请注意。例如:drumbeat</param>
    public OssALiHelper(string accessKeyId= "", string accessKeySecret= "", string endpoint= "", string bucketName= "otcstore")
    {
        _accessKeyId = accessKeyId ?? throw new ArgumentNullException(nameof(accessKeyId));
        _accessKeySecret = accessKeySecret ?? throw new ArgumentNullException(nameof(accessKeySecret));
        _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
        _bucketName = bucketName ?? throw new ArgumentNullException(nameof(bucketName));
    }
    public OssClient GetClient()
    {
        return new OssClient(_endpoint, _accessKeyId, _accessKeySecret);
    }
    /// <summary>
    /// 上传一个图片
    /// </summary>
    /// <param name="base64Code">图片经过base64加密后的结果</param>
    /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param>
    public bool PushImg(string base64Code, string fileName)
    {
        try
        {
            var client = GetClient();
            MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Code));
            return client.PutObject(_bucketName, fileName, stream).HttpStatusCode == System.Net.HttpStatusCode.OK;
        }
        catch (Exception)
        { }
        return false;
    }

    /// <summary>
    /// 上传一个文件
    /// </summary>
    /// <param name="filebyte">图片字节 </param>
    /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param>
    public bool PushFile(byte[] filebyte, string fileName)
    {
        try
        {
            var client = GetClient();
            MemoryStream stream = new MemoryStream(filebyte, 0, filebyte.Length);
            return client.PutObject(_bucketName, fileName, stream).HttpStatusCode == System.Net.HttpStatusCode.OK;
        }
        catch (Exception)
        { }
        return false;
    }
    /// <summary>
    /// 获取鉴权后的URL,文件过期时间默认设置为100年
    /// </summary>
    /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param>
    /// <returns></returns>
    public string GetFileUrl(string fileName)
    {
        var client = GetClient();
        var key = fileName;
        var req = new GeneratePresignedUriRequest(_bucketName, key, SignHttpMethod.Get)
        {
            Expiration = DateTime.Now.AddYears(100)
        };
        return client.GeneratePresignedUri(req).ToString();
    }
    /// <summary>
    /// 获取鉴权后的URL
    /// </summary>
    /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param>
    /// <param name="expiration">URL有效日期,例如:DateTime.Now.AddHours(1) </param>
    /// <returns></returns>
    public string GetFileUrl(string fileName, DateTime expiration)
    {
        var client = GetClient();
        var key = fileName;
        var req = new GeneratePresignedUriRequest(_bucketName, key, SignHttpMethod.Get)
        {
            Expiration = expiration
        };
        return client.GeneratePresignedUri(req).ToString();
    }

    /// <summary>
    /// 图片转换二进制
    /// </summary>
    /// <param name="SourFilePath">图片路径</param>
    /// <returns></returns>
    public  byte[] BytImg(string SourFilePath)
    {
        Image img = new Bitmap(SourFilePath);
        MemoryStream stream = new MemoryStream();
        img.Save(stream, ImageFormat.Bmp);
        BinaryReader br = new BinaryReader(stream);
        byte[] photo = stream.ToArray();
        stream.Close();
        return photo;
    }
}
}
Last modification:August 31st, 2019 at 05:34 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment