保存到Amazon S3(1 \ 300)时,文件上传大约每周一周。以下代码可以很好地确认文件保存正确,但我不禁想到有更好的方法。当文件失败时,不会抛出任何异常,因此我无法确定问题所在。有什么建议可以更好地确认吗?
AmazonS3Config _s3Config = new AmazonS3Config
{
ServiceURL = "s3.amazonaws.com",
CommunicationProtocol = Protocol.HTTPS,
};
using (AmazonS3 client = AWSClientFactory.CreateAmazonS3Client("accessKey", "secretAccessKey", _s3Config))
{
PutObjectRequest request = new PutObjectRequest();
request.WithBucketName("bucketName")
.WithFilePath("filePath")
.WithKey("keyName");
request.WithServerSideEncryptionMethod(ServerSideEncryptionMethod.AES256);
PutObjectResponse response = client.PutObject(request);
// what property from the response object can I check to confirm success???
}
// the following DoesObjectExist() function uses the GetObjectMetadata() function
if (!DoesObjectExist(keyName))
throw new Exception("Failed!");
答案 0 :(得分:6)
根据API文档,它建议您根据您发送的数据的计算MD5哈希检查ETag值。他们显然应该匹配。
“为了确保对象不会在网络上损坏,您可以计算对象的MD5,将其PUT到Amazon S3,并将返回的Etag与计算出的MD5值进行比较。”
http://docs.amazonwebservices.com/AmazonS3/latest/API/SOAPPutObject.html
希望有所帮助