I have a windows service that monitors a folder for new files and then sends to a S3 Bucket to process. The service works for the first file, but then I get this exception for any files after that.
Message: 'The process cannot access the file 'C:\PDFs\sample-2.pdf' because it is being used by another process.' when writing an object
Here is part of the code for the upload:
private static async Task UploadFileAsync()
{
try
{
var fileTransferUtility = new TransferUtility(s3Client);
//await fileTransferUtility.UploadAsync(filePath, bucketName);
Log.WriteLine("FilePath: " + filePath);
using (var fileToUpload = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
await fileTransferUtility.UploadAsync(fileToUpload, bucketName, fileToUpload.Name);
}
Log.WriteLine("File uploaded: " + filePath);
}
catch (AmazonS3Exception e)
{
Log.WriteLine("Error encountered on server. Message: '"+ e.Message +"' when writing an object");
}
catch (Exception e)
{
Log.WriteLine("Unknown encountered on pc. Message: '" + e.Message + "' when writing an object");
}
}
I originally had the fileMode set to Read and File Access set to Read as well.
Any clues?