Chapter 2. Creating an Amazon S3 client using notebook cells
To interact with data in Amazon S3 buckets, you must create a local client to handle requests to that service.
Prerequisites
- Access to a Jupyter notebook server running on Red Hat OpenShift Data Science.
-
Define values for the
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
environment variables when you start your notebook server, using the values from your Amazon Web Services account under My Security Credentials.
Procedure
In a new notebook cell, import the required libraries by adding the following:
import os import boto3 from boto3 import session
In another new notebook cell, define the following to create your session and client.
Define your credentials.
key_id = os.environ.get('AWS_ACCESS_KEY_ID') secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
Define the client session.
session = boto3.session.Session(aws_access_key_id=key_id, aws_secret_access_key=secret_key)
Define the client connection.
s3_client = boto3.client('s3', aws_access_key_id=key_id, aws_secret_access_key=secret_key)
Verification
Create a new cell and run an Amazon S3 command such as the following:
s3_client.list_buckets()
A successful response includes a
HTTPStatusCode
of200
and a list ofBuckets
similar to the following:'Buckets': [{'Name': 'my-app-asdf3-image-registry-us-east-1-wbmlcvbasdfasdgvtsmkpt', 'CreationDate': datetime.datetime(2021, 4, 21, 6, 8, 52, tzinfo=tzlocal())}, {'Name': 'cf-templates-18rxasdfggawsvb-us-east-1', 'CreationDate': datetime.datetime(2021, 2, 15, 18, 35, 34, tzinfo=tzlocal())}