April 2018
Intermediate to advanced
280 pages
8h 37m
English
Once we create a bucket, we can the upload files to it using the put_object() function of the S3 object. It accepts the file handle of the file to be uploaded. The following code demonstrates how we can upload a file to the S3 bucket:
import boto3s3 = boto3.resource('s3')file_handle = open('/home/packt-pub/test.txt', 'r')s3.Bucket('packt-pub').put_object(Key='test.txt', Body=file_handle)
There is also another function to upload a file to S3 called upload_file(). It requires two parameters - the location of the file to be uploaded and the name to be given to the file when it is uploaded to the bucket.
It can be used as follows:
import boto3 s3 = boto3.resource('s3') s3.Bucket('mybucket').upload_file('/tmp/test.txt',