1

我正在使用适用于 .NET 1.5.9.0 的 AWS 开发工具包并进行了更改

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)
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 结果)
在 Amazon.S3.AmazonS3Client.EndPutObject(IAsyncResult asyncResult)

通过建议_

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

很明显我要做什么,但我如何访问所需的信息?谁能指出我正确的方向?

4

1 回答 1

3

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

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

uploadRequest.UploadProgressEvent += (source, progress) =>
{
    Console.WriteLine("{0}% - {1} / {2}",
        progress.PercentDone,
        progress.TransferredBytes,
        progress.TotalBytes);
};
于 2012-11-28T23:47:06.877 回答