Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy content from one S3 bucket to another S3 bucket with different keys

I have two S3 buckets with two different set of access and secret keys. Here is the set:

Bucket1, Key1, Secret1

And

Bucket2, Key2, Secret2, Token

I am trying to trigger S3-S3 copy via aws cli like this:

aws s3 cp s3://key1:secret1@Bucket1 s3://key2:secret2@Bucket2

I have few questions:

  • I am not sure how to specify token for providing write permission to bucket2
  • Does aws cli allows specifying key and secret as part of S3 Url ?

What would be the best approach to achieve this use case ?

like image 547
Sachin Avatar asked Nov 02 '16 04:11

Sachin


People also ask

Can you copy data from one S3 bucket to another?

Depending on your use case, you can perform the data transfer between buckets using one of the following options: Run parallel uploads using the AWS Command Line Interface (AWS CLI) Use an AWS SDK. Use cross-Region replication or same-Region replication.

Why can't I copy an object between two Amazon S3 buckets?

To copy an object between buckets, you must make sure that the correct permissions are configured. To copy an object between buckets in the same AWS account, you can set permissions using IAM policies.

How do you copy an object between S3 buckets using AWS lambda function?

Create S3 Bucket in Source Account, to which the logs will be uploaded. Add below Bucket Access policy to the IAM Role created in Destination account. Lambda function will assume the Role of Destination IAM Role and copy the S3 object from Source bucket to Destination.

How do I transfer a bucket from one AWS account to another S3?

You can't transfer Amazon S3 bucket ownership between AWS accounts because the bucket is always owned by the account that created it. Instead, you can copy Amazon S3 objects from one bucket to another so that you give ownership of the copied objects to the destination account.


1 Answers

To copy a file between Amazon S3 buckets, you must use credentials that have permission to access both buckets, or apply a bucket policy to the destination bucket that permits the access.

It is not possible to specify two sets of credentials because the AWS Command-Line Interface (CLI) is only calling a single AWS API, which performs the copy 'from' the source bucket directly to the destination bucket. The AWS CLI does not download the object -- it simply tells S3 to copy the object to another bucket (which can even be in a different region).

Therefore, you should create a bucket policy on the destination bucket that permits the credentials being used (User or Role) to PutObject into the destination bucket.

The policy would be similar to:

{
    "Version": "2012-10-17",
    "Id": "Policy1",
    "Statement": [
        {
            "Sid": "Stmt1",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::ACCOUNT-NUMBER:role/ROLE-NAME"
            },
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::DESTINATION-BUCKET/*"
        }
    ]
}

The above is assuming the command is being called from an Amazon EC2 instance with an assigned role. To call from User credentials, use:

"AWS": "arn:aws:iam::ACCOUNT-NUMBER:user/USER-NAME"
like image 88
John Rotenstein Avatar answered Oct 16 '22 18:10

John Rotenstein