Ric*_*son 7 c# httpwebrequest amazon-web-services
我试图从HTTP流直接上传到S3,而不是先存储在内存中或作为文件.我已经使用Rackspace Cloud Files作为HTTP到HTTP进行此操作,但AWS身份验证超出了我的范围,因此我尝试使用SDK.
问题是上传流失败并出现此异常:
"This stream does not support seek operations."
我尝试过,PutObject并且TransferUtility.Upload都失败了同样的事情.
是否有任何方法可以在流进入S3时流入S3,而不是将整个内容缓冲到MemoryStream或FileStream?
或者是否有使用HTTPWebRequest对S3请求进行身份验证的好例子,所以我可以复制我对Cloud Files的处理方式?
编辑: 或者 AWSSDK中是否有帮助函数用于生成授权标头?
码:
这是失败的S3部分(两个方法都包括完整性):
string uri = RSConnection.StorageUrl + "/" + container + "/" + file.SelectSingleNode("name").InnerText;
var req = (HttpWebRequest)WebRequest.Create(uri);
req.Headers.Add("X-Auth-Token", RSConnection.AuthToken);
req.Method = "GET";
using (var resp = req.GetResponse() as HttpWebResponse)
{
using (Stream stream = resp.GetResponseStream())
{
Amazon.S3.Transfer.TransferUtility trans = new Amazon.S3.Transfer.TransferUtility(S3Client);
trans.Upload(stream, config.Element("root").Element("S3BackupBucket").Value, container + file.SelectSingleNode("name").InnerText);
//Use EITHER the above OR the below
PutObjectRequest putReq = new PutObjectRequest();
putReq.WithBucketName(config.Element("root").Element("S3BackupBucket").Value);
putReq.WithKey(container + file.SelectSingleNode("name").InnerText);
putReq.WithInputStream(Amazon.S3.Util.AmazonS3Util.MakeStreamSeekable(stream));
putReq.WithMetaData("content-length", file.SelectSingleNode("bytes").InnerText);
using (S3Response putResp = S3Client.PutObject(putReq))
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我从S3到云文件成功的方法:
using (GetObjectResponse getResponse = S3Client.GetObject(new GetObjectRequest().WithBucketName(bucket.BucketName).WithKey(file.Key)))
{
using (Stream s = getResponse.ResponseStream)
{
//We can stream right from s3 to CF, no need to store in memory or filesystem.
var req = (HttpWebRequest)WebRequest.Create(uri);
req.Headers.Add("X-Auth-Token", RSConnection.AuthToken);
req.Method = "PUT";
req.AllowWriteStreamBuffering = false;
if (req.ContentLength == -1L)
req.SendChunked = true;
using (Stream stream = req.GetRequestStream())
{
byte[] data = new byte[32768];
int bytesRead = 0;
while ((bytesRead = s.Read(data, 0, data.Length)) > 0)
{
stream.Write(data, 0, bytesRead);
}
stream.Flush();
stream.Close();
}
req.GetResponse().Close();
}
}
Run Code Online (Sandbox Code Playgroud)
由于没有人回答似乎已经做过,我根据史蒂夫的回答指导我花时间解决问题:
回答这个问题"有没有使用HTTPWebRequest对S3请求进行身份验证的好例子,所以我可以复制我对Cloud Files的处理方式?",这里是如何手动生成auth头:
string today = String.Format("{0:ddd,' 'dd' 'MMM' 'yyyy' 'HH':'mm':'ss' 'zz00}", DateTime.Now);
string stringToSign = "PUT\n" +
"\n" +
file.SelectSingleNode("content_type").InnerText + "\n" +
"\n" +
"x-amz-date:" + today + "\n" +
"/" + strBucketName + "/" + strKey;
Encoding ae = new UTF8Encoding();
HMACSHA1 signature = new HMACSHA1(ae.GetBytes(AWSSecret));
string encodedCanonical = Convert.ToBase64String(signature.ComputeHash(ae.GetBytes(stringToSign)));
string authHeader = "AWS " + AWSKey + ":" + encodedCanonical;
string uriS3 = "https://" + strBucketName + ".s3.amazonaws.com/" + strKey;
var reqS3 = (HttpWebRequest)WebRequest.Create(uriS3);
reqS3.Headers.Add("Authorization", authHeader);
reqS3.Headers.Add("x-amz-date", today);
reqS3.ContentType = file.SelectSingleNode("content_type").InnerText;
reqS3.ContentLength = Convert.ToInt32(file.SelectSingleNode("bytes").InnerText);
reqS3.Method = "PUT";
Run Code Online (Sandbox Code Playgroud)
请注意添加的x-amz-date标头,因为HTTPWebRequest以不同的格式将日期发送到AWS期望的格式.
从那里开始,只是重复我已经在做的事情.