簡體   English   中英

將文件從亞馬遜s3帳戶轉移到另一個帳戶

[英]Transfer file from amazon s3 account to another

我正在編寫一個應用程序,我們需要在不同的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);

但在最后一行“上傳”函數調用我總是得到這個錯誤: 這個流不支持搜索操作。

是否有不同的方式來做我想做的事情? 因為它接縫不正確

我遇到了這個問題,並發布了我用來解決它的方法。 借用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方法

我真的認為沒有辦法在賬戶之間轉賬。 我知道唯一能做到這一點的協議是FXP 您可以向AWS發出請求以便將來執行此操作,這將是一個不錯的功能。

亞馬遜有一個復制對象請求

AmazonS3Client client = new AmazonS3Client();

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

 CopyObjectResponse response = client.CopyObject(request);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM