/// <summary> /// 封装的上传图片到OSS的方法 /// 固定上传到 /upload/{year}{month}/{md5}.{type} /// </summary> /// <param name="setting"></param> /// <param name="file"></param> /// <returns></returns> public static string UploadImage(this OSSSetting setting, IFormFile file) { if (file == null) { throw new NullReferenceException(); } string fix = file.FileName[file.FileName.LastIndexOf('.')..][1..];
/// <summary> /// 分片断点续传 /// </summary> /// <param name="setting"></param> /// <param name="objectName"></param> /// <param name="binaryData"></param> /// <param name="index">第多少个分片(从1开始)</param> /// <param name="uploadId">如果是第二个分片则必须传入</param> /// <returns></returns> public static PartETag Upload(this OSSSetting setting, string objectName, byte[] binaryData, int index, ref string uploadId) { OssClient client = new OssClient(setting.endpoint, setting.accessKeyId, setting.accessKeySecret); PartETag partETagResult; if (index == 1) { var request = new InitiateMultipartUploadRequest(setting.bucketName, objectName, new ObjectMetadata { ExpirationTime = DateTime.Now.AddDays(1) }); var result = client.InitiateMultipartUpload(request); uploadId = result.UploadId; } using (MemoryStream requestContent = new(binaryData)) { var result = client.UploadPart(new UploadPartRequest(setting.bucketName, objectName, uploadId) { InputStream = requestContent, PartSize = binaryData.Length, PartNumber = index }); partETagResult = result.PartETag; } return(partETagResult); }
/// <summary> /// 删除文件 /// </summary> public static bool Delete(this OSSSetting setting, string objectName) { OssClient client = new OssClient(setting.endpoint, setting.accessKeyId, setting.accessKeySecret); DeleteObjectResult result = client.DeleteObject(setting.bucketName, objectName); return(true); }
/// <summary> /// 设定文件的过期时间 /// </summary> /// <param name="setting"></param> /// <param name="objectName"></param> /// <param name="expireTime">为null是永不过期</param> /// <returns></returns> public static bool SetExpirationTime(this OSSSetting setting, string objectName, DateTime?expireTime = null) { expireTime ??= DateTime.MaxValue; OssClient client = new OssClient(setting.endpoint, setting.accessKeyId, setting.accessKeySecret); client.ModifyObjectMeta(setting.bucketName, objectName, new ObjectMetadata { ExpirationTime = expireTime.Value }); return(true); }
/// <summary> /// 分片断点续传完成后传入 /// </summary> /// <param name="setting"></param> /// <param name="objectName"></param> /// <param name="uploadId">如果是第二个分片则必须传入</param> /// <param name="partETags"> </param> /// <returns></returns> public static void UploadFinish(this OSSSetting setting, string objectName, string uploadId, List <PartETag> partETags) { OssClient client = new(setting.endpoint, setting.accessKeyId, setting.accessKeySecret); var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(setting.bucketName, objectName, uploadId); foreach (var partETag in partETags.OrderBy(o => o.PartNumber)) { completeMultipartUploadRequest.PartETags.Add(partETag); } client.CompleteMultipartUpload(completeMultipartUploadRequest); }
/// <summary> /// 上传本地文件 /// </summary> /// <param name="setting"></param> /// <param name="objectName">远程文件名(包含路径),不能以/开头</param> /// <param name="localFilename">本地路径</param> /// <param name="message"></param> /// <returns></returns> public static bool Upload(this OSSSetting setting, string objectName, string localFilename, out string?message) { message = null; try { OssClient client = new OssClient(setting.endpoint, setting.accessKeyId, setting.accessKeySecret); PutObjectResult result = client.PutObject(setting.bucketName, objectName, localFilename); return(true); } catch (Exception ex) { message = "OSS错误:" + ex.Message; return(false); } }
/// <summary> /// 上传二进制内容 /// </summary> /// <param name="setting"></param> /// <param name="objectName">远程文件名(包含路径),不能以/开头</param> /// <param name="binaryData">二进制内容</param> /// <param name="message"></param> /// <returns></returns> public static bool Upload(this OSSSetting setting, string objectName, byte[] binaryData, ObjectMetadata metadata, out string?message) { message = null; try { using (MemoryStream requestContent = new MemoryStream(binaryData)) { OssClient client = new OssClient(setting.endpoint, setting.accessKeyId, setting.accessKeySecret); PutObjectResult result = client.PutObject(setting.bucketName, objectName, requestContent, metadata); return(true); } } catch (Exception ex) { message = "OSS错误:" + ex.Message; return(false); } }