Skip to content

Commit

Permalink
fix: Fixed the Tencent provider (#185)
Browse files Browse the repository at this point in the history
Signed-off-by: geffzhang <[email protected]>
  • Loading branch information
geffzhang authored Nov 18, 2024
1 parent 7d607da commit 227970c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 193 deletions.
189 changes: 0 additions & 189 deletions src/BaGetter.Tencent/BucketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<byte>());
}

public byte[] DownloadFileBytes(string key)
{
try
Expand All @@ -129,136 +70,6 @@ public byte[] DownloadFileBytes(string key)
return Array.Empty<byte>();
}

public List<string> 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<string> GetDirectories(string dir)
{
var dirs = new List<string>();
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
Expand Down
13 changes: 9 additions & 4 deletions src/BaGetter.Tencent/TencentStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public TencentStorageService(TencentCosClient cosClient, IOptionsSnapshot<Tencen
public async Task DeleteAsync(string path, CancellationToken cancellationToken = default)

Check warning on line 18 in src/BaGetter.Tencent/TencentStorageService.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in src/BaGetter.Tencent/TencentStorageService.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
ArgumentException.ThrowIfNullOrEmpty(path);
var found = _cosClient.BucketClient.GetDirFiles(PrepareKey(path)).FirstOrDefault() ?? string.Empty;
if(string.IsNullOrEmpty(found))
var found = _cosClient.BucketClient.DoesObjectExist(PrepareKey(path)) ? PrepareKey(path) : string.Empty;
if (string.IsNullOrEmpty(found))
{
throw new KeyNotFoundException($"The file {path} was not found.");
}
Expand All @@ -29,7 +29,9 @@ public async Task DeleteAsync(string path, CancellationToken cancellationToken =
public async Task<Stream> GetAsync(string path, CancellationToken cancellationToken = default)

Check warning on line 29 in src/BaGetter.Tencent/TencentStorageService.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 29 in src/BaGetter.Tencent/TencentStorageService.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
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.");
Expand All @@ -41,7 +43,9 @@ public async Task<Stream> GetAsync(string path, CancellationToken cancellationTo
public async Task<Uri> GetDownloadUriAsync(string path, CancellationToken cancellationToken = default)

Check warning on line 43 in src/BaGetter.Tencent/TencentStorageService.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 43 in src/BaGetter.Tencent/TencentStorageService.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
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.");
Expand All @@ -59,6 +63,7 @@ public async Task<StoragePutResult> PutAsync(string path, Stream content, string
seekableContent.Seek(0, SeekOrigin.Begin);

var fileRet = _cosClient.BucketClient.UploadStream(PrepareKey(path), seekableContent);

if(!fileRet)
{
return StoragePutResult.AlreadyExists;
Expand Down

0 comments on commit 227970c

Please sign in to comment.