Skip to Content
Hybrid Cloud for Developers
book

Hybrid Cloud for Developers

by Manoj Hirway
April 2018
Intermediate to advanced content levelIntermediate to advanced
280 pages
8h 37m
English
Packt Publishing
Content preview from Hybrid Cloud for Developers

Uploading files to S3

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',
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hybrid Cloud for Architects

Hybrid Cloud for Architects

Alok Shrivastwa
OpenStack: Building a Cloud Environment

OpenStack: Building a Cloud Environment

Alok Shrivastwa, Sunil Sarat, Kevin Jackson, Cody Bunch, Egle Sigler, Tony Campbell

Publisher Resources

ISBN: 9781788830874Supplemental Content