Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 PutObject() return value to confirm success?

Approximately once\week a file upload fails when saving to Amazon S3 (1\300). The following code works well enough to confirm that the file saved correctly, but I can't help but think there's a better way. When a file does fail, no exception is thrown so I'm never really certain where the problem lies. Any suggestions for better confirmation?

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!");
like image 990
Greg Cooper Avatar asked Sep 17 '12 06:09

Greg Cooper


1 Answers

According to the API documentation it recommends you check the ETag value against the a calculated MD5 hash of the data you sent. They obviously should match.

"To ensure an object is not corrupted over the network, you can calculate the MD5 of an object, PUT it to Amazon S3, and compare the returned Etag to the calculated MD5 value."

http://docs.amazonwebservices.com/AmazonS3/latest/API/SOAPPutObject.html

Hope that helps

like image 92
SCB Avatar answered Oct 12 '22 22:10

SCB