如何创建文件并将其上传到 amazon-S3
How to create and upload files to amazon-S3
我有一个 C# Lambda 函数可以创建一个简单的 .txt 文件,如下所示:
string file = Path.DirectorySeparatorChar + "tmp" + Path.DirectorySeparatorChar + "file.txt";
File.Create(file);
然后我尝试将其上传到 S3
TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.EUWest1));
fileTransferUtility.Upload(file, MyBucketNameInString);
但是,它返回了这个错误:
File createdOne or more errors occurred. (The process cannot access
the file '/tmp/file.txt' because it is being used by another
process.): AggregateException
有什么解决办法吗?
我将文件上传到 amazon S3 的简单解决方案
private void AddFile(Stream image)
{
InitializeKey();
var uploadRequest = new TransferUtilityUploadRequest();
uploadRequest.CannedACL = S3CannedACL.PublicRead;
uploadRequest.InputStream = image;
uploadRequest.BucketName = AppConfiguration.AmazonS3FileBucketName;
uploadRequest.Key = _key;
using (TransferUtility fileTransferUtility = new TransferUtility(_client))
{
fileTransferUtility.Upload(uploadRequest);
}
}
我有一个 C# Lambda 函数可以创建一个简单的 .txt 文件,如下所示:
string file = Path.DirectorySeparatorChar + "tmp" + Path.DirectorySeparatorChar + "file.txt";
File.Create(file);
然后我尝试将其上传到 S3
TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.EUWest1));
fileTransferUtility.Upload(file, MyBucketNameInString);
但是,它返回了这个错误:
File createdOne or more errors occurred. (The process cannot access the file '/tmp/file.txt' because it is being used by another process.): AggregateException
有什么解决办法吗?
我将文件上传到 amazon S3 的简单解决方案
private void AddFile(Stream image)
{
InitializeKey();
var uploadRequest = new TransferUtilityUploadRequest();
uploadRequest.CannedACL = S3CannedACL.PublicRead;
uploadRequest.InputStream = image;
uploadRequest.BucketName = AppConfiguration.AmazonS3FileBucketName;
uploadRequest.Key = _key;
using (TransferUtility fileTransferUtility = new TransferUtility(_client))
{
fileTransferUtility.Upload(uploadRequest);
}
}