diff --git a/src/BaGetter.Tencent/BucketClient.cs b/src/BaGetter.Tencent/BucketClient.cs index 1effb79b..a8aad07f 100644 --- a/src/BaGetter.Tencent/BucketClient.cs +++ b/src/BaGetter.Tencent/BucketClient.cs @@ -23,31 +23,6 @@ public BucketClient(CosXmlServer cosXml, string fullBucketName, string appId, st _region = region; } - public bool UploadBytes(string key, byte[] fileData) - { - var result = false; - try - { - var request = new PutObjectRequest(_fullBucketName, key, fileData); - - var resultData = CosXml.PutObject(request); - - if (resultData != null && resultData.IsSuccessful()) - { - result = true; - } - } - catch (CosClientException clientEx) - { - throw new Exception(clientEx.Message); - } - catch (CosServerException serverEx) - { - throw new Exception(serverEx.Message); - } - return result; - } - public bool UploadStream(string key, Stream stream) { var result = false; @@ -73,40 +48,6 @@ public bool UploadStream(string key, Stream stream) return result; } - public (string, byte[]) DownloadDirDefaultFileBytes(string dir) - { - try - { - var request = new GetBucketRequest(_fullBucketName); - request.SetPrefix($"{dir.TrimEnd('/')}/"); - request.SetDelimiter("/"); - - var result = CosXml.GetBucket(request); - - var info = result.listBucket; - - var objects = info.contentsList; - - var objectData = objects.FirstOrDefault(o => o.size > 0); - - if (objectData != null) - { - var fileName = Path.GetFileName(objectData.key); - var fileBytes = DownloadFileBytes(objectData.key); - return (fileName, fileBytes); - } - } - catch (CosClientException clientEx) - { - throw new Exception(clientEx.Message); - } - catch (CosServerException serverEx) - { - throw new Exception(serverEx.Message); - } - return (string.Empty, Array.Empty()); - } - public byte[] DownloadFileBytes(string key) { try @@ -129,136 +70,6 @@ public byte[] DownloadFileBytes(string key) return Array.Empty(); } - public List GetDirFiles(string dir) - { - try - { - var request = new GetBucketRequest(_fullBucketName); - request.SetPrefix($"{dir.TrimEnd('/')}/"); - request.SetDelimiter("/"); - - var result = CosXml.GetBucket(request); - - var info = result.listBucket; - - var objects = info.contentsList; - - return objects.Where(o => o.size > 0).Select(o => o.key).ToList(); - - } - catch (CosClientException clientEx) - { - throw new Exception(clientEx.Message); - } - catch (CosServerException serverEx) - { - throw new Exception(serverEx.Message); - } - } - - public List GetDirectories(string dir) - { - var dirs = new List(); - try - { - var request = new GetBucketRequest(_fullBucketName); - request.SetPrefix($"{dir.TrimEnd('/')}/"); - request.SetDelimiter("/"); - - var result = CosXml.GetBucket(request); - - var info = result.listBucket; - - var objects = info.contentsList; - - var list = objects.Where(o => o.size == 0 && o.key != dir).Select(o => o.key).ToList(); - - dirs.AddRange(list); - - var commonPrefixes = info.commonPrefixesList; - - dirs.AddRange(commonPrefixes.Select(c => c.prefix)); - - return dirs; - - } - catch (CosClientException clientEx) - { - throw new Exception(clientEx.Message); - } - catch (CosServerException serverEx) - { - throw new Exception(serverEx.Message); - } - } - - public bool DirExists(string dir) - { - try - { - var request = new GetBucketRequest(_fullBucketName); - request.SetPrefix($"{dir.TrimEnd('/')}/"); - request.SetDelimiter("/"); - - var result = CosXml.GetBucket(request); - - var info = result.listBucket; - - var objects = info.contentsList; - - return objects.Count > 0 || info?.commonPrefixesList.Count > 0; - - } - catch (CosClientException clientEx) - { - throw new Exception(clientEx.Message); - } - catch (CosServerException serverEx) - { - throw new Exception(serverEx.Message); - } - } - - public void MoveDir(string sourceDir, string destDir) - { - var listRequest = new GetBucketRequest(_fullBucketName); - - listRequest.SetPrefix($"{sourceDir.TrimEnd('/')}/"); - var listResult = CosXml.GetBucket(listRequest); - - var info = listResult.listBucket; - - var objects = info.contentsList; - - foreach (var obj in objects) - { - string sourceKey = obj.key; - string destinationKey = $"{destDir.TrimEnd('/')}/{sourceKey.Substring(sourceDir.Length)}"; - - var copySource = new CopySourceStruct(_appId, _fullBucketName, _region, sourceKey); - - var request = new CopyObjectRequest(_fullBucketName, destinationKey); - - request.SetCopySource(copySource); - try - { - - var result = CosXml.CopyObject(request); - var deleteRequest = new DeleteObjectRequest(_fullBucketName, sourceKey); - var deleteResult = CosXml.DeleteObject(deleteRequest); - } - catch (CosClientException clientEx) - { - throw new Exception(clientEx.Message); - } - catch (CosServerException serverEx) - { - throw new Exception(serverEx.Message); - } - } - - } - public void DeleteDir(string dir) { try diff --git a/src/BaGetter.Tencent/TencentStorageService.cs b/src/BaGetter.Tencent/TencentStorageService.cs index c8752f20..aa6a2994 100644 --- a/src/BaGetter.Tencent/TencentStorageService.cs +++ b/src/BaGetter.Tencent/TencentStorageService.cs @@ -18,8 +18,8 @@ public TencentStorageService(TencentCosClient cosClient, IOptionsSnapshot GetAsync(string path, CancellationToken cancellationToken = default) { ArgumentException.ThrowIfNullOrEmpty(path); - var fileStorageUrl = _cosClient.BucketClient.GetDirFiles(PrepareKey(path)).FirstOrDefault() ?? string.Empty; + + var fileStorageUrl = _cosClient.BucketClient.DoesObjectExist(PrepareKey(path)) ? PrepareKey(path) : string.Empty; + if (string.IsNullOrEmpty(fileStorageUrl)) { throw new KeyNotFoundException($"The file {path} was not found."); @@ -41,7 +43,9 @@ public async Task GetAsync(string path, CancellationToken cancellationTo public async Task GetDownloadUriAsync(string path, CancellationToken cancellationToken = default) { ArgumentException.ThrowIfNullOrEmpty(path); - var fileStorageUrl = _cosClient.BucketClient.GetDirFiles(PrepareKey(path)).FirstOrDefault() ?? string.Empty; + + var fileStorageUrl = _cosClient.BucketClient.DoesObjectExist(PrepareKey(path)) ? PrepareKey(path) : string.Empty; + if (string.IsNullOrEmpty(fileStorageUrl)) { throw new KeyNotFoundException($"The file {path} was not found."); @@ -59,6 +63,7 @@ public async Task PutAsync(string path, Stream content, string seekableContent.Seek(0, SeekOrigin.Begin); var fileRet = _cosClient.BucketClient.UploadStream(PrepareKey(path), seekableContent); + if(!fileRet) { return StoragePutResult.AlreadyExists;