- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Amazon S3
进行实现。我使用 Amazon C# SDK
,并尝试使用 putObject 方法上传创建的 ZIP 文件。
当我上传文件时,出现以下错误:
{Amazon.S3.AmazonS3Exception: The Content-MD5 you specified was invalid
我生成了一个正常工作的内存流,我可以将它无误地上传到 Amazon S3。但是,当我提供以下行时,它给我带来了问题:
request.MD5Digest = md5;
我是否以正确的方式证明了 MD5?我的MD5生成正确吗?还是有其他问题?
对我的上传代码的要求:
Once the Zip file has been created and you have calculated an MD5 sum value of that file, you should
transfer the file to the AWS S3 bucket identified in the S3Access XML.
Transfer the file using the AmazonS3, PutObjectRequest and TransferManagerclasses.
Ensure the following meta data attributes are included via adding an ObjectMetaDataclass instance to the
PutObjectRequest:
• MD5Sum (via setContentMD5)
• Mime ContentType (setContentType)
我的上传代码
client.PutObject() 给出错误:
public void UploadFile(string bucketName, Stream uploadFileStream, string remoteFileName, string md5)
{
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, config))
{
try
{
StringBuilder stringResp = new StringBuilder();
PutObjectRequest request = new PutObjectRequest();
// request.MD5Digest = md5;
request.BucketName = bucketName;
request.InputStream = uploadFileStream;
request.Key = remoteFileName;
request.MD5Digest = md5;
using (S3Response response = client.PutObject(request))
{
WebHeaderCollection headers = response.Headers;
foreach (string key in headers.Keys)
{
stringResp.AppendLine(string.Format("Key: {0}, value: {1}", key,headers.Get(key).ToString()));
//log headers ("Response Header: {0}, Value: {1}", key, headers.Get(key));
}
}
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
//log exception - ("Please check the provided AWS Credentials.");
}
else
{
//log exception -("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message);
}
}
}
}
整体方法
我的流程方法(看整体流程)。对于代码的当前状态,伪代码多于生产代码,我们深表歉意:
public void Process(List<Order> order)
{
var zipName = UserName + "-" + DateTime.Now.ToString("yy-MM-dd-hhmmss") + ".zip";
var zipPath = HttpContext.Current.Server.MapPath("~/Content/zip-fulfillment/" + zipName);
CreateZip(order, zipPath);
var s3 = GetS3Access();
var amazonService = new AmazonS3Service(s3.keyid, s3.secretkey, "s3.amazonaws.com");
var fileStream = new MemoryStream(HelperMethods.GetBytes(zipPath));
var md5val = HelperMethods.GetMD5HashFromStream(fileStream);
fileStream.Position = 0;
amazonService.UploadFile(s3.bucket, fileStream, zipName, md5val);
var sqsDoc = DeliveryXml(md5val, s3.bucket, "Test job");
amazonService.SendSQSMessage(sqsDoc.ToString(), s3.postqueue);
}
MD5 哈希方法:
此方法用于从我的内存流中创建 MD5 哈希:
public static string GetMD5HashFromStream(Stream stream)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
编辑:
刚刚在整体方法中添加了fileStream.Position = 0。仍然是完全相同的问题。
最佳答案
我怀疑问题可能是在计算流的散列之后,流留在数据的末尾 ...没有数据。尝试在调用 GetMD5HashFromStream
fileStream.Position = 0;
这将“倒回”流,以便您可以再次阅读。
编辑:刚刚查看了文档,虽然以上是一个问题,但它不是唯一的问题。目前,您正在附加 MD5 哈希的 hex 表示 - 但是 documentation状态:
Content-MD5: The base64-encoded 128-bit MD5 digest of the message
注意“base64 编码”部分。因此,您需要将 MD5 代码更改为:
public static string GetMD5HashFromStream(Stream stream)
{
using (MD5 md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(stream);
return Convert.ToBase64String(hash);
}
}
关于c# - 上传带有 MD5 哈希结果的流在 "The Content-MD5 you specified was invalid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19877891/
我是一名优秀的程序员,十分优秀!