Connecting to S3 using the boto3 library
This guide describes how to connect to an S3 object storage in Jupyter Notebook using the boto3
library. To connect to an object storage, you can use an S3 connector:
Note
Avoid using S3 storage in FUSE
To set up an S3 connection from the notebook code:
-
Create the secrets:
token
(with ID) andkey_value
(with a the secret part of the static access key for the service account). -
Open the DataSphere project:
-
Select the relevant project in your community or on the DataSphere homepage
in the Recent projects tab. - Click Open project in JupyterLab and wait for the loading to complete.
- Open the notebook tab.
-
-
Import the libraries:
import boto3 import os from os import path
-
Enter the name of your bucket in the storage.
bucket_name = '<bucket_name>'
-
Establish a connection:
session = boto3.session.Session() ENDPOINT = "https://storage.yandexcloud.net" session = boto3.Session( aws_access_key_id=(os.environ['token']), aws_secret_access_key=(os.environ['key_value']), region_name="ru-central1", ) s3 = session.client( "s3", endpoint_url=ENDPOINT)
-
Enter the bucket name and retrieve a list of objects contained in it.
for key in s3.list_objects(Bucket='<bucket_name>')['Contents']: print(key['Key'])