2017-07-07 158 views
1

我有一個C#lambda函數創建一個簡單的.txt文件是這樣的:如何創建和上傳文件到亞馬遜S3

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

任何解決方案嗎?

+0

是/tmp/file.txt在文本編輯器或其它程序打開文件? – Ehz

回答

0

我SIMPL解決上傳文件到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); 
    } 
}