如何获得TransferUtilityUploadRequest的AmazonId2

时间:2012-11-28 09:00:01

标签: c# amazon-s3

我正在使用AWS SDK for .NET 1.5.9.0并更改了

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);
}

由于

  

System.Net.WebException:请求已中止:请求已取消。 --->
  System.IO.IOException:在写入所有字节之前无法关闭流     在System.Net.ConnectStream.CloseInternal(Boolean internalCall,Boolean aborting)
    ---内部异常堆栈跟踪结束---
    在System.Net.ConnectStream.CloseInternal(Boolean internalCall,Boolean aborting)
    在System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
    在System.Net.ConnectStream.Dispose(布尔处理)
    在System.IO.Stream.Close()
    在Amazon.S3.AmazonS3Client.getRequestStreamCallback [T](IAsyncResult结果)
    在Amazon.S3.AmazonS3Client.endOperation [T](IAsyncResult结果)
    在Amazon.S3.AmazonS3Client.EndPutObject(IAsyncResult asyncResult)

suggestion

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);
}

我正在尝试做的很明显,但如何获取所需信息? 有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:3)

使用TransferUtility时,您将无法访问AmazonId2,因为该实用程序会丢弃所有响应。

TransferUtility.Upload是一个阻塞操作,因此在请求完成之前它不会返回。但是,您可以通过订阅请求中的事件来监控上传,如下所示:

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