1

I need to upload files to S3 and I was wondering which boto3 api call I should use?

I have found two methods in the boto3 documentation:

Do I use the client.upload_file() ...

#!/usr/bin/python
import boto3
session = Session(aws_access_key_id, aws_secret_access_key, region)
s3 = session.resource('s3')
s3.Bucket('my_bucket').upload_file('/tmp/hello.txt', 'hello.txt')

or do I use S3Transfer.upload_file() ...

#!/usr/bin/python
import boto3
session = Session(aws_access_key_id, aws_secret_access_key, region)
S3Transfer(session).upload_file('/tmp/hello.txt', 'my_bucket', 'hello.txt')

Any suggestions would be appreciated. Thanks in advance.

. . .

possible solution...

# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#examples
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.put_object
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.get_object

client = boto3.client("s3", "us-west-1", aws_access_key_id = "xxxxxxxx", aws_secret_access_key = "xxxxxxxxxx")


with open('drop_spot/my_file.txt') as file:
  client.put_object(Bucket='s3uploadertestdeleteme', Key='my_file.txt', Body=file)


response = client.get_object(Bucket='s3uploadertestdeleteme', Key='my_file.txt')

print("Done, response body: {}".format(response['Body'].read()))
aidanmelen
  • 6,194
  • 1
  • 23
  • 24
  • If it's suitable, you could use the [AWS Command-Line Interface (CLI)](http://aws.amazon.com/cli/) to copy files. Use the `aws s3 cp` or `aws s3 sync` commands and it will do all the hard work for you. – John Rotenstein Sep 17 '16 at 03:52
  • You can also you Minio Client's `mc mirror` or `mc cp` for the same [https://docs.minio.io/docs/minio-client-complete-guide#mirror](https://docs.minio.io/docs/minio-client-complete-guide#mirror) Hope it helps. – koolhead17 Sep 17 '16 at 17:07

2 Answers2

1

It's better to use the method on the client. They're the same, but using the client method means you don't have to setup things yourself.

Jordon Phillips
  • 14,963
  • 4
  • 35
  • 42
0

You can use Client: low-level service access : I saw a sample code in https://www.techblog1.com/2020/10/python-3-how-to-communication-with-aws.html

Sachin Sukumaran
  • 707
  • 2
  • 9
  • 25