[英]How to upload memory file to amazon S3?
I want to upload a file from my memoryStream to amazon S3 server. 我想从memoryStream上传文件到亚马逊S3服务器。
Here is the code: 这是代码:
public static bool memUploadFile(AmazonS3 client, MemoryStream memFile, string toPath)
{
try
{
Amazon.S3.Transfer.TransferUtility tranUtility = new Amazon.S3.Transfer.TransferUtility(client);
tranUtility.Upload(filePath, toPath.Replace("\\", "/"));
return true;
}
catch (Exception ex)
{
return false;
}
}
Then the error says, 然后错误说,
"the best overload method match for 'Amazon.S3.Transfer.TransferUtility.Uplaod(string,string)' has some invalid arguments" “'Amazon.S3.Transfer.TransferUtility.Uplaod(string,string)'的最佳重载方法匹配'有一些无效的参数”
Look at the Upload Method (stream, bucketName, key) 查看上传方法(stream,bucketName,key)
public static bool memUploadFile(AmazonS3 client, MemoryStream memFile, string toPath)
{
try
{
using(Amazon.S3.Transfer.TransferUtility tranUtility =
new Amazon.S3.Transfer.TransferUtility(client))
{
tranUtility.Upload(memFile, toPath.Replace("\\", "/"), <The key under which the Amazon S3 object is stored.>);
return true;
}
}
catch (Exception ex)
{
return false;
}
}
Hamlet is right. 哈姆雷特是对的。 This is an example TransferUtilityUploadRequest
这是TransferUtilityUploadRequest的示例
[Test]
public void UploadMemoryFile()
{
var config = CloudConfigStorage.GetAdminConfig();
string bucketName = config.BucketName;
string clientAccessKey = config.ClientAccessKey;
string clientSecretKey = config.ClientSecretKey;
string path = Path.GetFullPath(@"dummy.txt");
File.WriteAllText(path, DateTime.Now.ToLongTimeString());
using (var client = AWSClientFactory.CreateAmazonS3Client(clientAccessKey, clientSecretKey))
using (var transferUtility = new TransferUtility(client))
{
var request = new TransferUtilityUploadRequest
{
BucketName = bucketName,
Key = "memory.txt",
InputStream = new MemoryStream(File.ReadAllBytes(path))
};
transferUtility.Upload(request);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.