获取文件大小 oss
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
c94d800646
commit
f99cab6983
|
@ -156,7 +156,9 @@ public interface IOSSService
|
|||
|
||||
public ObjectStoreDTO GetObjectStoreTempToken();
|
||||
|
||||
public Task MoveObject(string sourcePath, string destPath, bool overwrite = true);
|
||||
public Task MoveObject(string sourcePath, string destPath, bool overwrite = true);
|
||||
|
||||
public long GetObjectSizeAsync(string sourcePath);
|
||||
}
|
||||
|
||||
|
||||
|
@ -700,7 +702,7 @@ public class OSSService : IOSSService
|
|||
};
|
||||
await amazonS3Client.DeleteObjectAsync(deleteRequest);
|
||||
|
||||
|
||||
|
||||
}
|
||||
catch (AmazonS3Exception ex)
|
||||
{
|
||||
|
@ -711,9 +713,9 @@ public class OSSService : IOSSService
|
|||
break;
|
||||
default:
|
||||
throw new BusinessValidationFailedException("ERROR");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -998,6 +1000,78 @@ public class OSSService : IOSSService
|
|||
}
|
||||
}
|
||||
|
||||
public async Task<long> GetObjectSizeAsync(string sourcePath)
|
||||
{
|
||||
GetObjectStoreTempToken();
|
||||
|
||||
var objectkey = sourcePath.Trim('/');
|
||||
|
||||
if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS")
|
||||
{
|
||||
var aliConfig = ObjectStoreServiceOptions.AliyunOSS;
|
||||
|
||||
var _ossClient = new OssClient(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? aliConfig.EndPoint : aliConfig.InternalEndpoint, AliyunOSSTempToken.AccessKeyId, AliyunOSSTempToken.AccessKeySecret, AliyunOSSTempToken.SecurityToken);
|
||||
|
||||
var metadata = _ossClient.GetObjectMetadata(aliConfig.BucketName, objectkey);
|
||||
|
||||
long fileSize = metadata.ContentLength; // 文件大小(字节)
|
||||
|
||||
return fileSize;
|
||||
}
|
||||
else if (ObjectStoreServiceOptions.ObjectStoreUse == "MinIO")
|
||||
{
|
||||
var minIOConfig = ObjectStoreServiceOptions.MinIO;
|
||||
|
||||
|
||||
var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.EndPoint}:{minIOConfig.Port}")
|
||||
.WithCredentials(minIOConfig.AccessKeyId, minIOConfig.SecretAccessKey).WithSSL(minIOConfig.UseSSL)
|
||||
.Build();
|
||||
|
||||
|
||||
var stat = await minioClient.StatObjectAsync(new Minio.DataModel.Args.StatObjectArgs()
|
||||
.WithBucket(minIOConfig.BucketName)
|
||||
.WithObject(objectkey));
|
||||
|
||||
return stat.Size; // 文件大小(字节)
|
||||
}
|
||||
else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS")
|
||||
{
|
||||
|
||||
var awsConfig = ObjectStoreServiceOptions.AWS;
|
||||
|
||||
|
||||
// 提供awsAccessKeyId和awsSecretAccessKey构造凭证
|
||||
var credentials = new SessionAWSCredentials(AWSTempToken.AccessKeyId, AWSTempToken.SecretAccessKey, AWSTempToken.SessionToken);
|
||||
|
||||
//提供awsEndPoint(域名)进行访问配置
|
||||
var clientConfig = new AmazonS3Config
|
||||
{
|
||||
RegionEndpoint = RegionEndpoint.USEast1,
|
||||
UseHttp = true,
|
||||
};
|
||||
|
||||
var request = new Amazon.S3.Model.GetObjectMetadataRequest
|
||||
{
|
||||
BucketName = awsConfig.BucketName,
|
||||
Key = objectkey
|
||||
};
|
||||
|
||||
var amazonS3Client = new AmazonS3Client(credentials, clientConfig);
|
||||
|
||||
var response = await amazonS3Client.GetObjectMetadataAsync(request);
|
||||
|
||||
long fileSize = response.ContentLength; // 文件大小(字节)
|
||||
|
||||
return fileSize;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new BusinessValidationFailedException("未定义的存储介质类型");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ObjectStoreDTO GetObjectStoreTempToken()
|
||||
{
|
||||
|
@ -1041,7 +1115,7 @@ public class OSSService : IOSSService
|
|||
BucketName = ossOptions.BucketName,
|
||||
EndPoint = ossOptions.EndPoint,
|
||||
ViewEndpoint = ossOptions.ViewEndpoint,
|
||||
PreviewEndpoint=ossOptions.PreviewEndpoint
|
||||
PreviewEndpoint = ossOptions.PreviewEndpoint
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue