Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we copy s3 files between buckets of different account/credentials using s3 cp and different profiles?

I created two profiles (one for source and one for target bucket) and using below command to copy:

aws s3 cp --profile source_profile s3://source_bucket/file.txt --profile target_profile s3://target_profile/

But it throws below error.

fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden

Looks like we can't use multiple profiles with aws commands.

like image 381
Aman Kaushik Avatar asked Jun 11 '18 06:06

Aman Kaushik


People also ask

How do I transfer files between S3 buckets in two accounts?

Open AWS CLI and run the copy command from the Code section to copy the data from the source S3 bucket. Run the synchronize command from the Code section to transfer the data into your destination S3 bucket. Your data is then copied from the source S3 bucket to the destination S3 bucket.

How do I copy encrypted S3 buckets Cross account?

Step 1: Create an IAM policy like the one below, replace the source and destination bucket names. Step 2: Attach the above policy to the IAM user or role that is doing the copy object operation. Step 3: Change the Object ownership to Bucket owner preferred in the destination bucket.

Which S3 feature allows you to move files between different S3 storage classes?

You can also use S3 Lifecycle policies to automatically transition objects between storage classes without any application changes.


1 Answers

The simplest method is to grant permissions via a bucket policy.

Say you have:

  • Account-A with IAM User-A
  • Account-B with Bucket-B

Add a bucket policy on Bucket-B:

{
  "Id": "CopyBuckets",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "GrantAccessToUser-A",
      "Action": "s3:*",
      "Effect": "Allow",
      "Resource": [
        "arn:aws:s3:::bucket-b",
        "arn:aws:s3:::bucket-b/*"
      ],
      "Principal": {
        "AWS": [
          "arn:aws:iam::<account-a-id>:user/user-a"
        ]
      }
    }
  ]
}

Then just copy the files as User-A.

See also: aws sync between S3 buckets on different AWS accounts

like image 163
John Rotenstein Avatar answered Sep 28 '22 09:09

John Rotenstein