4

我正在编写一个应用程序,我们需要在不同的 s3 帐户之间传输文件,我们不想在上传到目标 s3 帐户之前将文件保存在本地。
因此,我们使用以下方法获取源文件流:

public Stream GetFileStream(string path)
{
    var pathPieces = path.Split('/');
    string bucket = pathPieces[0];
    string fileName = pathPieces[pathPieces.Length - 1];

    GetObjectRequest request = new GetObjectRequest();
    request.BucketName = bucket;
    request.Key = fileName;

    GetObjectResponse response = Client.GetObject(request);

    return response.ResponseStream;
}

之后我们使用这个流上传到目的地:

    TransferUtility transferUtility = new TransferUtility(Client);
    transferUtility.S3Client.PutBucket(new PutBucketRequest() { UseClientRegion = true }.WithBucketName(bucket));
    TransferUtilityUploadRequest request = new TransferUtilityUploadRequest()
    .WithBucketName(bucket)
    .WithKey(key)
    .WithPartSize(1024)
    .WithTimeout(100 * 60 * 60 * 1000)
    .WithAutoCloseStream(true)
    .WithCannedACL(S3CannedACL.PublicReadWrite)
    .WithInputStream(fileStream) as TransferUtilityUploadRequest;

    transferUtility.Upload(request);

但在最后一行“上传”函数调用我总是得到这个错误: 这个流不支持搜索操作。

有没有不同的方法来做我想做的事情?因为它接缝的方式不正确

4

3 回答 3

2

我遇到了这个问题,并发布了我用来解决它的方法。从 Jon Skeet 那里借了一些代码。它对我有用,但我知道它有点笨拙,可能可以改进。

string filename = "nameoffiletocopy"

using (Stream stream = GetS3Stream(filename))
{
    AmazonS3Config config = new AmazonS3Config 
        {CommunicationProtocol = Protocol.HTTP};
    AmazonS3 client = 
        AWSClientFactory.CreateAmazonS3Client(
            "destinationaccesskey",
            "destinationsecretacceskey", 
            config);
    string destinationBucketName = "destinationbucketname";

    PutObjectRequest request = new PutObjectRequest
    {
        BucketName = destinationBucketName,
        InputStream = stream,
        Key = filename,
        CannedACL = S3CannedACL.PublicRead
    };

    PutObjectResponse response = client.PutObject(request);     
}

private static Stream GetS3Stream(string filename)
{
    AmazonS3Config config = new AmazonS3Config 
        {CommunicationProtocol = Protocol.HTTP};
    AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(
        "sourceaccesskey",
        "sourcesecretacceskey", 
        config);
    string sourceBucketName = "sourcebucketname";

    GetObjectRequest request = new GetObjectRequest 
        {
            BucketName = sourceBucketName, 
            Key = filename
        };

    using (GetObjectResponse response = client.GetObject(request))
    {
        byte[] binaryData = 
        ReadFully(response.ResponseStream, -1);
        //ReadyFully from Jon Skeet's blog
        return new MemoryStream(binaryData);
    }
}

Jon Skeet充分解释了 ReadFully 方法

于 2014-11-11T19:34:57.377 回答
1

我真的不认为有一种方法可以在帐户之间转移。我知道的唯一可以做到这一点的协议是FXP。您可以在将来向 AWS 提出请求以执行此操作,这将是一个不错的功能。

于 2013-04-27T01:52:36.307 回答
0

亚马逊有一个复制对象请求

AmazonS3Client client = new AmazonS3Client();

CopyObjectRequest request = new CopyObjectRequest {
      SourceBucket = "fromBucket",
      SourceKey = "fromFile",
      DestinationBucket = "toBucket",
      DestinationKey = "toFilename",
      CannedACL = S3CannedACL.BucketOwnerFullControl
 };

 CopyObjectResponse response = client.CopyObject(request);
于 2013-04-24T17:07:11.427 回答