1
votes

I am using AWS SDK for .NET 1.5.9.0 and changed

var putObjectRequest = new PutObjectRequest
{
    BucketName = bucketName,
    Key = key,
    FilePath = filePath,
    CannedACL = S3CannedACL.PublicRead
};
try
{
    using (var pubtObjectResponse = client.PutObject(putObjectRequest))
    {
        Logger.Application.DebugFormat("uploaded {0} to {1} on bucket {2}, got id {3}", filePath, remotePath, bucketName, pubtObjectResponse.AmazonId2);
    }
}
catch (Exception ex)
{
    Logger.Application.Fatal(string.Format("could not upload {0} to {1} on bucket {2}", filePath, remotePath, bucketName), ex);
}

due to

System.Net.WebException: The request was aborted: The request was canceled. --->
System.IO.IOException: Cannot close stream until all bytes are written.
at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
--- End of inner exception stack trace ---
at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
at System.Net.ConnectStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at Amazon.S3.AmazonS3Client.getRequestStreamCallback[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.endOperation[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.EndPutObject(IAsyncResult asyncResult)

by suggestion to

var transferUtilityUploadRequest = new TransferUtilityUploadRequest()
    .WithBucketName(bucketName)
    .WithKey(key)
    .WithFilePath(filePath)
    .WithCannedACL(S3CannedACL.PublicRead);
try
{
    transferUtility.Upload(transferUtilityUploadRequest);

    // how do I get success-state in here?
    // how do I get AmazonId2 in here?
}
catch (Exception ex)
{
    Logger.Application.Fatal(string.Format("could not upload {0} to {1} on bucket {2}", filePath, remotePath, bucketName), ex);
}

It's pretty obvious what I am trying to do, but how do I access the needed information? Can anyone point me in the right direction?

1

1 Answers

3
votes

While using TransferUtility, you won't have access to AmazonId2, since the utility discards all responses.

TransferUtility.Upload is a blocking operation, so it will not return until the request is completed. However, you can monitor your upload by subscribing to an event on the request, like so:

uploadRequest.UploadProgressEvent += (source, progress) =>
{
    Console.WriteLine("{0}% - {1} / {2}",
        progress.PercentDone,
        progress.TransferredBytes,
        progress.TotalBytes);
};