3ac41541918739.jpg

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

    }
}
最后修改:2022 年 06 月 17 日 10 : 29 AM
如果觉得我的文章对你有用,请随意赞赏