问题描述
我有一些麻烦上传的文件保存到S3。我的第一个尝试是:
I'm having some trouble getting uploaded files to save to S3. My first attempt was:
Result SaveFile(System.Web.HttpPostedFileBase file, string path)
{
//Keys are in web.config
var t = new Amazon.S3.Transfer.TransferUtility(Amazon.RegionEndpoint.USWest2);
try
{
t.Upload(new Amazon.S3.Transfer.TransferUtilityUploadRequest
{
BucketName = Bucket,
InputStream = file.InputStream,
Key = path
});
}
catch (Exception ex)
{
return Result.FailResult(ex.Message);
}
return Result.SuccessResult();
}
这将引发异常的消息:。我们计算请求签名不您提供的签名匹配检查你的密钥和签名法我也试图复制file.InputStream到一个MemoryStream,然后上传,与同样的错误。
如果我设置的InputStream:
This throws an exception with the message: "The request signature we calculated does not match the signature you provided. Check your key and signing method." I also tried copying file.InputStream to a MemoryStream, then uploading that, with the same error.
If I set the InputStream to:
new FileStream(@"c:\folder\file.txt", FileMode.Open)
然后将文件上传罚款。我真的需要上传之前将文件保存到磁盘?
then the file uploads fine. Do I really need to save the file to disk before uploading it?
推荐答案
这是我的工作版本第一次上传方式:
This is my working version first the upload method:
public bool Upload(string filePath, Stream inputStream, double contentLength, string contentType)
{
try
{
var request = new PutObjectRequest();
request.WithBucketName(_bucketName)
.WithCannedACL(S3CannedACL.PublicRead)
.WithKey(filePath).InputStream = inputStream;
request.AddHeaders(AmazonS3Util.CreateHeaderEntry("ContentType", contentType));
_amazonS3Client.PutObject(request);
}
catch (Exception exception)
{
// log or throw;
return false;
}
return true;
}
我刚刚得到 HttpPostedFileBase.InputStream
(注意,这是在API的旧版本中, WithBucketName
语法不再支持,而只是直接设置属性)
(Note, this is on an older version of the Api, the WithBucketName
syntax is no longer supported, but just set the properties directly)
这篇关于如何通过AWS SDK发送HttpPostedFileBase到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!