Uploading and downloading file

Now that we've created a bucket, we can upload and download some files. Writing a function for uploading a file is similar to creating a bucket. We check the documentation to see how to construct our HTTP Request, figure out what information should be collected at the command line, and then write the function.

We need to use an HTTP PUT again. We need the name of the bucket that we want to store the file in and the name that we want the file to be stored under in S3. The body of the request will contain the file data. At the command line, we'll collect the bucket name, the name we want the file to have in the S3 service, and the name of the local file to upload.

Note that we open the local file in binary mode. The file could contain any type of data, so we don't want text transforms applied. We could pull this data from anywhere, such as a database or another web API. Here, we just use a local file for simplicity.

The URL is the same endpoint that we constructed in create_bucket() with the S3 object name appended to the URL path. Later, we can use this URL to retrieve the object.

You can find the following code in the s3_upload_download_file.py file. This is the function we can use to upload a file to a specific bucket:

def upload_file(bucket, local_path):
data = open(local_path, 'rb').read()
url = 'http://{}/{}/{}'.format(endpoint, bucket, local_path)
print('upload file '+url)
response = requests.put(url, data=data, auth=auth)
if response.ok:
print('Uploaded {} OK'.format(local_path))
else:
xml_pprint(response.text)

You'll need to replace bucket with your own bucket name. Once the file gets uploaded, you will see it in the S3 Console. Downloading a file through the S3 API is similar to uploading it. We simply take the bucket name, the S3 object name, and the local filename again with GET request instead of a put request, and then write the data received to disk. This is the function we can use to download a file from a specific bucket:

def download_file(bucket, s3_name):
url = 'http://{}/{}/{}'.format(endpoint, bucket, s3_name)
print('download file '+url)
response = requests.get(url, auth=auth)
print(response)
if response.ok:
open(s3_name, 'wb').write(response.content)
print('Downloaded {} OK'.format(s3_name))
else:
xml_pprint(response.text)

The complete script is available in the s3_upload_download_file.py file. We can execute it, passing the bucket name and the file we want to upload and download as arguments:

$python s3_upload_download_file.py bucket-aux Python.png
upload file http://s3.eu-west-2.amazonaws.com/bucket-aux/Python.png
Uploaded Python.png OK
download file http://s3.eu-west-2.amazonaws.com/bucket-aux/Python.png
<Response [200]>
Downloaded Python.png OK
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset