首页 .NET/Web OSS上传文件帮助类文档

OSS上传文件帮助类文档

作者:胡同里的砖头 围观群众:328 更新于:2021-11-19

public class AliyunHelper{
/// <summary>
/// 上传文件
/// </summary>
/// <param name="accessKeyId">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
/// <param name="accessKeySecret">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
/// <param name="endpoint">Endpoint,创建Bucket时对应的Endpoint</param>
/// <param name="bucketName">Bucket名称,创建Bucket时对应的Bucket名称</param>
/// <param name="key">文件标识</param>
/// <param name="file">需要上传文件的文件路径</param>
public static void PutObject(string accessKeyId, string accessKeySecret, string endpoint, string bucketName, string key, string file)
{
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
client.PutObject(bucketName, key, file);
}
catch (OssException ex)
{

}
}

/// <summary>
/// 下载文件
/// </summary>
/// <param name="accessKeyId">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
/// <param name="accessKeySecret">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
/// <param name="endpoint">Endpoint,创建Bucket时对应的Endpoint</param>
/// <param name="bucketName">Bucket名称,创建Bucket时对应的Bucket名称</param>
/// <param name="key">文件标识</param>
/// <param name="file">下载存放的文件路径</param>
public static void GetObject(string accessKeyId, string accessKeySecret, string endpoint, string bucketName, string key, string file)
{
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
var result = client.GetObject(bucketName, key);
using (var requestStream = result.Content)
{
using (var fs = File.Open(file, FileMode.OpenOrCreate))
{
int length = 4 * 1024;
var buf = new byte[length];
do
{
length = requestStream.Read(buf, 0, length);
fs.Write(buf, 0, length);
} while (length != 0);
}
}
}
catch (OssException ex)
{

}
}

/// <summary>
/// 获取图片地址
/// </summary>
/// <param name="accessKeyId">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
/// <param name="accessKeySecret">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
/// <param name="endpoint">Endpoint,创建Bucket时对应的Endpoint</param>
/// <param name="bucketName">Bucket名称,创建Bucket时对应的Bucket名称</param>
/// <param name="key">文件标识</param>
/// <param name="width">设置图片的宽度</param>
/// <param name="height">设置图片的高度</param>
/// <returns></returns>
public static string GetIamgeUri(string accessKeyId, string accessKeySecret, string endpoint, string bucketName, string key, float width = 100, float height = 100)
{
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
var process = $"image/resize,m_fixed,w_{width},h_{height}";
var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get)
{
Expiration = DateTime.Now.AddHours(1),
Process = process
};
var uri = client.GeneratePresignedUri(req);
return uri.ToString();
}
catch (OssException ex)
{
return "";
}
}
}
调用:
[HttpPost]
public void upload(string 本地文件路径)
{
AliyunHelper.PutObject("accessKeyId", "accessKeySecret", "endpoint", "bucketName", 预存OSS路径, 本地文件路径);
}

  • 本文标题: OSS上传文件帮助类文档
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8