@@ -1,5 +1,6 @@
|
||||
using AlibabaCloud.SDK.Sts20150401;
|
||||
using Aliyun.OSS;
|
||||
using Aliyun.OSS.Common;
|
||||
using Amazon;
|
||||
using Amazon.Runtime;
|
||||
using Amazon.S3;
|
||||
@@ -153,6 +154,8 @@ public interface IOSSService
|
||||
List<string> GetRootFolderNames();
|
||||
|
||||
public ObjectStoreDTO GetObjectStoreTempToken();
|
||||
|
||||
public void MoveObject(string sourcePath, string destPath, bool overwrite = true);
|
||||
}
|
||||
|
||||
|
||||
@@ -533,6 +536,60 @@ public class OSSService : IOSSService
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 移动OSS文件到新路径
|
||||
/// </summary>
|
||||
/// <param name="sourcePath">原文件路径(格式:bucket/key)</param>
|
||||
/// <param name="destPath">新文件路径(格式:bucket/key)</param>
|
||||
/// <param name="overwrite">是否覆盖已存在的目标文件(默认true)</param>
|
||||
public void MoveObject(string sourcePath, string destPath, bool overwrite = true)
|
||||
{
|
||||
|
||||
var aliConfig = ObjectStoreServiceOptions.AliyunOSS;
|
||||
|
||||
var client = new OssClient(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? aliConfig.EndPoint : aliConfig.InternalEndpoint, AliyunOSSTempToken.AccessKeyId, AliyunOSSTempToken.AccessKeySecret, AliyunOSSTempToken.SecurityToken);
|
||||
|
||||
|
||||
|
||||
var sourceBucket = aliConfig.BucketName;
|
||||
var sourceKey = sourcePath;
|
||||
|
||||
var destBucket = aliConfig.BucketName;
|
||||
|
||||
var destKey = destPath;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// 检查目标是否存在(当不允许覆盖时)
|
||||
if (!overwrite && client.DoesObjectExist(destBucket, destKey))
|
||||
{
|
||||
throw new InvalidOperationException("File exist");
|
||||
}
|
||||
|
||||
// 执行复制
|
||||
var copyRequest = new Aliyun.OSS.CopyObjectRequest(
|
||||
sourceBucket, sourceKey,
|
||||
destBucket, destKey);
|
||||
|
||||
// 保持原文件元数据
|
||||
copyRequest.NewObjectMetadata = new ObjectMetadata
|
||||
{
|
||||
ContentType = client.GetObjectMetadata(sourceBucket, sourceKey).ContentType
|
||||
};
|
||||
|
||||
var result = client.CopyObject(copyRequest);
|
||||
|
||||
// 删除原文件(仅在复制成功后)
|
||||
client.DeleteObject(sourceBucket, sourceKey);
|
||||
}
|
||||
catch (OssException ex)
|
||||
{
|
||||
throw new Exception($"[{ex.ErrorCode}] {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有根目录名称
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user