我正在处理一个项目,我正在从一个文件夹位置以 .xml 或 .zip 扩展名获取文件列表,并尝试将它们存储在 AWS 云上的 S3 存储桶位置。我正在从 DirectoryWatcher 类中分配字符串变量并调用以下方法:
public static async Task UploadFilestoS3Async(string archiveDir, string storeKey, string registerType, List<string> fileNames, string logDir)
{
foreach (var file in fileNames)
{
try
{
var fileKey = storeKey + "/" + registerType + "/" + file;
string fullArchivePath = archiveDir + file;
if (File.Exists(fullArchivePath))
{
var req = new PutObjectRequest
{
// Distributing .xml & .zip files in relevant buckets
BucketName = file.EndsWith(".xml") ? "xmlBucket" : "zipBucket",
Key = fileKey,
FilePath = fullArchivePath
};
await _s3Client.PutObjectAsync(req);
string message = $"Store Key: {storeKey}, register type: {registerType}, file: {file} uploaded to S3 at {DateTime.UtcNow}";
await _logger.Info(message);
//Upload to S3
await Service.AppendLogtoFile(logDir, storeKey, message, LogType.Info);
}
else
{
await _logger.Error($"Store Key: {storeKey}, register type: {registerType}, file: {file} not found at {DateTime.UtcNow}");
//Upload to S3
await Service.AppendLogtoFile(logDir, storeKey, $"Store Key: {storeKey}, register type: {registerType}, file: {file} not found at {DateTime.UtcNow}", LogType.Error);
}
}
catch (Exception ex)
{
await _logger.Error($"UploadFilestoS3Async: General exception with Store Key: {storeKey}, register type: {registerType}");
//Upload to S3
await Service.AppendLogtoFile(logDir, storeKey, $"UploadFilestoS3Async: General exception with Store Key: {storeKey}, register type: {registerType}, error: {ex.Message} stack trace: {ex.StackTrace}", LogType.Error);
}
}
}
我遇到的问题是在 xmlBucket 中,当所有 zip 文件和其他 xml 文件都在 zipBucket 中上传时,并非所有 xmlFile 都被上传。我希望“xmlBucket”只有 .xml 文件,而“zipBucket”只有 .zip 文件。我是否以不正确的方式实现异步方法?任何建议表示赞赏。