sts54
sts54

Reputation: 49

How to copy an AWS S3 file from one bucket to another using iOS SDK

I'm using the AWS iOS SDK and have been able to do the upload / download file operations as outlined in the tutorials. Now I'm trying to copy a file from one bucket and paste it into another. I own both buckets and have access to them. I also want to delete the file from the first bucket after copying it (so technically this is a cut-paste operation) but I'm assuming the way to do that is copy, paste, delete the original.

After some digging it seems like the way to do this is through the AWSS3 -uploadPartCopy: function. It seems like this function uses an AWSS3UploadPartCopyRquest object which has 3 relevant input properties, the destination bucket (bucket), the destination key (key) and the source location (replicateSource), which seems to be a URL for the location of the object to be copied.

This seems to me like a really strange format for such a function, and I'm also not familiar with what Uploading a part means, i.e. does this have to be part of a multi-part upload? Do I need to start a multi-part upload before calling uploadPartCopy?

I'm also not sure this is the way to go about this. It seems like an overcomplicated solution to a relatively simple task. Am I on the right track here?

Upvotes: 0

Views: 762

Answers (2)

Protocol
Protocol

Reputation: 1792

Just Refer below code. It gives you idea in detail for copy data from one bucket to another. In my case i want to copy multiple images from same bucket.

NSString *sourceBucket = @"treedev1234";

NSString *destinationBucket = @"treedev1234";

AWSS3 *s3 = [AWSS3 defaultS3];
AWSS3ReplicateObjectRequest *replicateRequest = [AWSS3ReplicateObjectRequest new];

for(int i = 0;i<feedModel.imageCount;i++){
    replicateRequest.bucket = destinationBucket;
    replicateRequest.key = [NSString stringWithFormat:@"posts/%d/%d.jpg",newpostid,i];
    replicateRequest.replicateSource = [NSString stringWithFormat:@"%@/posts/%d/%d.jpg",sourceBucket,oldpostid,i];
    replicateRequest.ACL = AWSS3ObjectCannedACLPublicReadWrite;

    [[s3 replicateObject:replicateRequest] continueWithBlock:^id(AWSTask *task) {
        if(task.error)
        NSLog(@"The  share request failed. error: [%@]", task.error);
        return nil;
    }];

Upvotes: 4

Yosuke
Yosuke

Reputation: 3759

There are two operations to copy an object: PUT Object - Copy and Upload Part - Copy. If the object is not too large, "PUT Object - Copy", which is mapped to - replicateObject:, is easier to implement.

Also, Amazon S3 has the Cross-Region Replication feature, which automatically replicates objects if your two buckets are not in the same region.

Upvotes: 0

Related Questions