.NET使用阿里云云存储上传文件

.NET使用阿里云云存储上传文件 .Net 第1张


1、web.config中配置阿里云云存储配置信息

<add key="Ali_OSS_AccessKeyID" value="" />
<add key="Ali_OSS_AccessKeySecret" value="" />
<add key="Ali_OSS_BucketName" value="" />
<add key="Ali_OSS_BucketName" value="" />


2、上传文件(图片)

//获取上传图片
HttpPostedFileBase file = Request.Files["file0"];
if (file != null && file.ContentLength > 0)
{
    OSS.Storage sotre = new OSS.Storage();
    ImageUrl= sotre.Upload(file.FileName, file.InputStream,"Product/");  
}


3、阿里云云存储操作类库

using Aliyun.OSS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Als.Utils.String;


namespace Als.Utils.External.Ali
{
    public static class OssStorage
    {

        public static string AccessKeyId { get; set; } = "Ali_OSS_AccessKeyID".ValueOfAppSetting();
        public static string AccessKeySecret { get; set; } = "Ali_OSS_AccessKeySecret".ValueOfAppSetting();
        public static string BucketName { get; set; } = "Ali_OSS_BucketName".ValueOfAppSetting();
        public static string EndPoint { get; set; } = "Ali_OSS_BucketName".ValueOfAppSetting();

        #region 默认配置信息上传文件

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="fileStream">文件流</param>
        public static void Upload(string fileName, Stream fileStream)
        {

            var ossClient = new OssClient(EndPoint, AccessKeyId, AccessKeySecret);
            var metadata = new ObjectMetadata { ContentType = GetContentType(fileName) };
            //根据文件名设置ContentType
            var key = "MerLogo/" + fileName;
            fileStream.Seek(0, SeekOrigin.Begin);
            ossClient.PutObject(BucketName, key, fileStream, metadata);
        }

        /// <summary>
        /// 上传文件并返回结果
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="fileStream">文件流</param>
        public static PutObjectResult UploadGetResult(string fileName, Stream fileStream)
        {
            var ossClient = new OssClient(EndPoint, AccessKeyId, AccessKeySecret);
            var metadata = new ObjectMetadata();
            //根据文件名设置ContentType
            metadata.ContentType = GetContentType(fileName);
            var key = "MerLogo/" + fileName;
            fileStream.Seek(0, SeekOrigin.Begin);
            return ossClient.PutObject(BucketName, key, fileStream, metadata);
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="fileStream"></param>
        /// <param name="dir"></param>
        /// <returns>返回存储后文件路径</returns>
        public static string Upload(string fileName, Stream fileStream, string dir)
        {
            var ossClient = new OssClient(EndPoint, AccessKeyId, AccessKeySecret);
            var metadata = new ObjectMetadata
            {
                //根据文件名设置ContentType
                ContentType = GetContentType(fileName)
            };
            var fileext = fileName.Split('.')[1];
            fileName = Guid.NewGuid() + "." + fileext;
            var key = dir + fileName;
            fileStream.Seek(0, SeekOrigin.Begin);
            ossClient.PutObject(BucketName, key, fileStream, metadata);
            return dir + fileName;
        }

        #endregion

        #region 设置配置信息上传文件

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="accessKeyId"></param>
        /// <param name="accessKeySecret"></param>
        /// <param name="bucketName"></param>
        /// <param name="endPoint"></param>
        /// <param name="fileName">文件名</param>
        /// <param name="fileStream">文件流</param>
        public static void Upload(string accessKeyId, string accessKeySecret, string bucketName, string endPoint,string fileName, Stream fileStream)
        {

            AccessKeyId = accessKeyId;
            AccessKeySecret = accessKeySecret;
            BucketName = bucketName;
            EndPoint = endPoint;

            var ossClient = new OssClient(EndPoint, AccessKeyId, AccessKeySecret);
            var metadata = new ObjectMetadata { ContentType = GetContentType(fileName) };
            //根据文件名设置ContentType
            var key = "MerLogo/" + fileName;
            fileStream.Seek(0, SeekOrigin.Begin);
            ossClient.PutObject(BucketName, key, fileStream, metadata);
        }

        /// <summary>
        /// 上传文件并返回结果
        /// </summary>
        /// <param name="accessKeyId"></param>
        /// <param name="accessKeySecret"></param>
        /// <param name="bucketName"></param>
        /// <param name="endPoint"></param>
        /// <param name="fileName">文件名</param>
        /// <param name="fileStream">文件流</param>
        public static PutObjectResult UploadGetResult(string accessKeyId, string accessKeySecret, string bucketName, string endPoint,string fileName, Stream fileStream)
        {
            AccessKeyId = accessKeyId;
            AccessKeySecret = accessKeySecret;
            BucketName = bucketName;
            EndPoint = endPoint;

            var ossClient = new OssClient(EndPoint, AccessKeyId, AccessKeySecret);
            var metadata = new ObjectMetadata();
            //根据文件名设置ContentType
            metadata.ContentType = GetContentType(fileName);
            var key = "MerLogo/" + fileName;
            fileStream.Seek(0, SeekOrigin.Begin);
            return ossClient.PutObject(BucketName, key, fileStream, metadata);
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="accessKeyId"></param>
        /// <param name="accessKeySecret"></param>
        /// <param name="bucketName"></param>
        /// <param name="endPoint"></param>
        /// <param name="fileName"></param>
        /// <param name="fileStream"></param>
        /// <param name="dir"></param>
        /// <returns>返回存储后文件路径</returns>
        public static string Upload(string accessKeyId, string accessKeySecret, string bucketName, string endPoint,string fileName, Stream fileStream, string dir)
        {
            AccessKeyId = accessKeyId;
            AccessKeySecret = accessKeySecret;
            BucketName = bucketName;
            EndPoint = endPoint;

            var ossClient = new OssClient(EndPoint, AccessKeyId, AccessKeySecret);
            var metadata = new ObjectMetadata
            {
                //根据文件名设置ContentType
                ContentType = GetContentType(fileName)
            };
            var fileext = fileName.Split('.')[1];
            fileName = Guid.NewGuid() + "." + fileext;
            var key = dir + fileName;
            fileStream.Seek(0, SeekOrigin.Begin);
            ossClient.PutObject(BucketName, key, fileStream, metadata);
            return dir + fileName;
        }

        #endregion

        #region 内部方法

        /// <summary>
        /// 获取contentType类型
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static string GetContentType(string fileName)
        {
            var fileNameArray = fileName.Split('.');
            var contentType = string.Empty;
            if (fileNameArray.Length < 2) return contentType;
            switch (fileNameArray[1])
            {
                case "gif":
                    contentType = "image/gif";
                    break;
                case "png":
                    contentType = "image/png";
                    break;
                case "jpg":
                    contentType = "image/jpg";
                    break;
                case "rar":
                    contentType = "application/octet-stream";
                    break;
                case "zip":
                    contentType = "application/zip";
                    break;
                default:
                    break;
            }
            return contentType;
        }

        #endregion

    }
}



本文标题:.NET使用阿里云云存储上传文件
本文链接:https://masoft.cn/post/73.html
作者授权:除特别说明外,本文由 智汇软件 原创编译并授权 北京,深圳,重庆,大连,青岛,济南软件设计与定制 刊载发布。
版权声明:本文使用「署名-禁止演绎 4.0 国际」创作共享协议,转载或使用请遵守署名协议。

本文 暂无 评论

Top

分享:

支付宝

微信