AliYunOSSHelper类:
namespace VOL.WebApi
{
public class AliYunOSSHelper: IScopedDependency
{
private ILogger _logger;
/// <summary>
/// 阿里云帮助类
/// </summary>
/// <param name="logger"></param>
/// <param name="configHelper"></param>
public AliYunOSSHelper(ILogger<AliYunOSSHelper> logger)
{
_logger = logger;
}
public AliYunOSSHelper()
{
}
/// <summary>
/// 阿里云上传底层方法,文件为IFormFile
/// </summary>
/// <param name="shop_code"></param>
/// <param name="files"></param>
/// <returns></returns>
public async Task<List<string>> AliYunUpLoad(List<IFormFile> files/*, string name*/)
{
List<string> ossfilesNameList = new List<string>();
try
{
//阿里云oss相关参数,请自行补齐 oss-cn-shenzhen-internal.aliyuncs.com aapi.oss-cn-shenzhen-internal.aliyuncs.com
//账户1
//var endpoint = "https://oss-cn-beijing.aliyuncs.com";
//var accessKeyId = "*";
//var accessKeySecret = "*";
//var bucket = "aapi";
//var imageUrl = "https://*.oss-cn-beijing.aliyuncs.com";
//账户2
var endpoint = "http://oss-cn-beijing.aliyuncs.com";
var accessKeyId = "*";
var accessKeySecret = "*";
var bucket = "cdn*";
var imageUrl = "http://cdn*.oss-cn-beijing.aliyuncs.com";
var write_client = new OssClient(endpoint, accessKeyId, accessKeySecret);
var read_client = new OssClient(endpoint, accessKeyId, accessKeySecret);
for (int i = 0; i < files.Count; i++)
{
var file = files[i];
var fname = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + i + Path.GetExtension(file.FileName);
using (var stream = file.OpenReadStream())
{
write_client.PutObject(bucket, fname, stream);
}
DateTime expiration = DateTime.Now.AddYears(20);
var url = read_client.GeneratePresignedUri(bucket, fname, expiration);
string urlstring = imageUrl + url.AbsolutePath;
ossfilesNameList.Add(urlstring);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "阿里云图片上传失败,ex=" + ex.Message);
throw;
}
return ossfilesNameList;
}
}
}
启动项目注入:
services.AddScoped<AliYunOSSHelper>();
接口:
AliyunController
sing Aliyun.OSS;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using VOL.Core.Controllers.Basic;
using VOL.Service.IServices;
using VOL.Service.Services;
namespace VOL.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class AliyunController : ControllerBase
{
public AliyunController()
{ }
private readonly AliYunOSSHelper _oSSHelper=new AliYunOSSHelper();
/ <summary>
/ 构造函数
/ </summary>
/ <param name="projectAppService"></param>
//public AliyunStsController(AliYunOSSHelper oSSHelper)
//{
// _oSSHelper = oSSHelper;
//}
/// <summary>
/// 通用的图片上传,支持多图片上传
/// </summary>
/// <param name="files"></param>
/// <returns></returns>
[HttpPost, Route("UploadPic"), AllowAnonymous]
[DisableRequestSizeLimit]
public async Task<ActionResult> UploadPic(List<IFormFile> files)
{
//15M
var maxRequestLength = 1024 * 1024 * 15;
List<string> filesName = new List<string>();
if (files.Count == 0)
return Content("NoFile"+files.Count+ filesName, "text/html");
else
{
for (var i = 0; i < files.Count; i++)
{
var file = files[i];
if (null == file || file.Length <= 0)
return Content("格式不正确!", "text/html");
if (file.Length > maxRequestLength)
{
return Content("文件大小超出限制!", "text/html");
}
var fname = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + i + Path.GetExtension(file.FileName);
// if (!FileExtensionFun.CheckImageFileType(fname))
//{
// return Content("上传的图片格式不正确", "text/html");
//}
}
filesName = await _oSSHelper.AliYunUpLoad(files);
}
return Content(string.Join(",", filesName), "text/html");
}
}
}
上传报错:
解决方案: