Chapter 2. Creating an S3 client
To interact with data stored in an S3-compatible object store from a workbench, you must create a local client to handle requests to the AWS S3 service by using an AWS SDK such as Boto3.
Boto3 is an AWS SDK for Python that provides an API for creating and managing AWS services, such as AWS S3 or S3-compatible object storage.
After you have configured a Boto3 client for the S3 service from a workbench, you can connect and work with data in your S3-compatible object store.
Prerequisites
- You have access to an S3-compatible object store.
- You have stored files in a bucket on your object store.
- You have logged in to Red Hat OpenShift AI.
- You have created a data science project.
- You have added a workbench to the project using a workbench image.
- You have configured a connection for your workbench based on the credentials of your S3-compatible storage account.
Procedure
- From the OpenShift AI dashboard, click Data science projects.
- Click the name of the project that contains the workbench.
- Click the Workbenches tab.
If the status of the workbench is Running, skip to the next step.
If the status of the workbench is Stopped, in the Status column for the workbench, click Start.
The Status column changes from Stopped to Starting when the workbench server is starting, and then to Running when the workbench has successfully started.
Click the open icon (
) next to the workbench.
Your Jupyter environment window opens.
- On the toolbar, click the Git Clone icon and then select Clone a Repository.
-
In the Clone a repo dialog, enter the following URL
https://github.com/opendatahub-io/odh-doc-examples.gitand then click Clone. -
In the file browser, select the newly-created
odh-doc-examplesfolder. Double-click the newly created
storagefolder.You see a Jupyter notebook named
s3client_examples.ipynb.Double-click the
s3client_examples.ipynbfile to launch the Jupyter notebook.The Jupyter notebook opens. You see code examples for the following tasks:
- Installing Boto3 and required Boto3 libraries
- Creating an S3 client session
- Creating an S3 client connection
- Listing files
- Creating a bucket
- Uploading a file to a bucket
- Downloading a file from a bucket
- Copying files between buckets
- Deleting an object from a bucket
- Deleting a bucket
In the Jupyter notebook, locate the following instructions to install Boto3 and its required libraries, and run the code cell:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The instructions in the code cell update the Python Package Manager (pip) to the latest version, install Boto3 and its required libraries, and display the version of Boto3 installed.
Locate the following instructions to create an S3 client and session. Run the code cell.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The instructions in the code cell configure an S3 client and establish a session to your S3-compatible object store.
Verification
To use the S3 client to connect to your object store and list the available buckets, locate the following instructions to list buckets and run the code cell:
s3_client.list_buckets()
s3_client.list_buckets()Copy to Clipboard Copied! Toggle word wrap Toggle overflow A successful response includes a
HTTPStatusCodeof200and a list of buckets similar to the following output:'HTTPStatusCode': 200, 'Buckets': [{'Name': 'aqs086-image-registry', 'CreationDate': datetime.datetime(2024, 1, 16, 20, 21, 36, 244000, tzinfo=tzlocal ())}]'HTTPStatusCode': 200, 'Buckets': [{'Name': 'aqs086-image-registry', 'CreationDate': datetime.datetime(2024, 1, 16, 20, 21, 36, 244000, tzinfo=tzlocal ())}]Copy to Clipboard Copied! Toggle word wrap Toggle overflow