AWS S3 - Copy objects from one bucket to another using pre-signurl - node.js

I'm trying to copy an object from one bucket to another, using pre-signed URL with both buckets, one pre-signed URL generated with getObject permission, while the other one with putObject permission.
I want to avoid downloading the objects using HTTP GET and uploading using the HTTP PUT, I want it to be as the usual way of copying objects between buckets (processed and executed within Amazon services).

Related

AWS cannot open or download after copy files to different Amazon S3 bucket using Boto3

I have created a lambda with boto3 that copies files from one Amazon S3 bucket to a different account's Amazon S3 bucket. Everything works fine, but when the other user is trying to open or download the files gets access denied or cannot download the files.
I have the bucket location of the other account and the kms key and i have created policy role for that on my bucket. My bucket has encryption enabled.
Do i need to decrypt my files and encrypt with the kms key of the other account ? I am testing with https://docs.aws.amazon.com/kms/latest/developerguide/programming-encryption.html#reencryption is this correct ?
Thanks
This is probably an object ownership issue. You will need to grant the destination bucket bucket-owner-full-control to the object when uploading. You can set a bucket policy which blocks uploads unless the uploader grants this access:
https://docs.aws.amazon.com/AmazonS3/latest/userguide/about-object-ownership.html

Can I get information on an S3 Bucket's public access bucket Settings from boto3?

I am using boto3 to extract information about my S3 buckets.
However, I am stuck at this point. I am trying to extract information about a bucket's public access (see attached screenshot).
How can I get this information? So far I have failed to find out any boto3 function that allows me to do so.
You can use get_public_access_block():
Retrieves the PublicAccessBlock configuration for an Amazon S3 bucket.
When Amazon S3 evaluates the PublicAccessBlock configuration for a bucket or an object, it checks the PublicAccessBlock configuration for both the bucket (or the bucket that contains the object) and the bucket owner's account. If the PublicAccessBlock settings are different between the bucket and the account, Amazon S3 uses the most restrictive combination of the bucket-level and account-level settings.
If you wish to modify the settings, you can use: put_public_access_block()

AWS LAMBDA S3 Glacier - how to retrieve S3 Object if its storage Class is in Glacier

I was trying to download the file using Lambda NodeJS.
It is working fine.. but when ever we download a file which is in a GLACIER storage class it ends up having an error..
I have been looking on how to get the attributes of the object from lambda, so that we could do logic if we find the file is in what time of storage class..
Files in Glacier cannot be retrieved in real time, once they're in Glacier they're archived.
To retrieve these files you first need to restore them.
Restoring archived objects: https://docs.aws.amazon.com/AmazonS3/latest/dev/restoring-objects.html

Uploading a file through boto3 upload_file api to AWS S3 bucket gives "Anonymous users cannot initiate multipart uploads. Please authenticate." error

I have a file of around 16mb in size and am using python boto3 upload_file api to upload this file into the S3 bucket. However, I believe the API is internally choosing multipart upload and gives me an "Anonymous users cannot initiate multipart upload" error.
In some of the runs of the application, the file generated may be smaller (few KBs) in size.
What's the best way to handle this scenario in general or fix the error I mentioned above?
I currently have a Django application that generates a file when run and uploads this file directly into an S3 bucket.
Ok, so unless you've opened your S3 bucket up for the world to upload to (which is very much NOT recommended), it sounds like you need to setup the permissions for access to your S3 bucket correctly.
How to do that will vary a little depending on how you're running this application - so let's cover off a few options - in all cases you will need to do two things:
Associate your script with an IAM Principal (an IAM User or an IAM Role depending on where / how this script is being run).
Add permissions for that principal to access the bucket (this can be accomplished either through an IAM Policy, or via the S3 Bucket Policy)
Lambda Function - You'll need to create an IAM Role for your application and associate it with your Lambda function. Boto3 should be able to assume this role transparently for you once configured.
EC2 Instance or ECS Task - You'll need to create an IAM Role for your application and associate it with your EC2 instance/ECS Task. Boto3 will be able to access the credentials for the role via instance metadata and should automatically assume the role.
Local Workstation Script - If you're running this script from your local workstation, then boto3 should be able to find and use the credentials you've setup for the AWS CLI. If those aren't the credentials you want to use you'll need to generate an access key and secret access key (be careful how you secure these if you go this route, and definitely follow least privilege).
Now, once you've got your principal you can either attach an IAM policy that grants Allow permissions to upload to the bucket to the IAM User or Role, or you can add a clause to the Bucket Policy that grants that IAM User or Role access. You only need to do one of these.
Multi-part uploads are performed via the same S3:PutObject call as single part uploads (though if your files are small I'd be surprised it was using multi-part for them). If you're using KMS one small trick to be aware of is that you need permission to use the KMS key for both Encrypt and Decrypt permissions if encrypting a multi-part upload.

How to copy from S3 production to S3 development using Python with different roles?

I need to copy files from S3 Production(where i have only read access) to S3 development (i have write access). The change which i face is switching the roles.
While coping i need use prod role and while writing i need to use developer role.
I am trying with below code:
import boto3
boto3.setup_default_session(profile_name='prod_role')
s3 = boto3.resource('s3')
copy_source = {
'Bucket': 'prod_bucket',
'Key': 'file.txt'
}
bucket = s3.Bucket('dev_bucket')
bucket.copy(copy_source, 'file.txt')
I need to know how to switch the role.
The most efficient way to move data between buckets in Amazon S3 is to use the resource.copy() or client.copy_object() command. This allows the two buckets to directly communicate (even between different regions), without the need to download/upload the objects themselves.
However, the credentials used to call the command require both read permission from the source and write permission to the destination. It is not possible to provide two different sets of credentials for this copy.
Therefore, you should pick ONE set of credentials and ensure it has the appropriate permissions. This means either:
Give the Prod credentials permission to write to the destination, or
Give the non-Prod credentials permission to read from the Prod bucket
This can be done either by creating a Bucket Policy, or by assigning permissions directly to the IAM Role/User being used.
If this is a regular task that needs to happen, you could consider automatically copying the files by using an Amazon S3 event on the source bucket to trigger a Lambda function that copies the object to the non-Prod destination immediately. This avoids the need to copy files in a batch at some later time.

Resources