如何创建和上传文件到amazon-S3

时间:2017-07-07 18:41:04

标签: c# amazon-s3 lambda tmp

我有一个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);

但是,它正在返回此错误:

  

已创建文件发生了一个或多个错误。 (该过程无法访问   文件' /tmp/file.txt'因为它被另一个人使用   process。):AggregateException

有任何解决方案吗?

1 个答案:

答案 0 :(得分:0)

我将文件上传到亚马逊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);
    }
}