Connecting to S3 using boto3
Follow this guide to connect to an S3 object storage in Jupyter Notebook using boto3. To connect to an object storage, use an S3 connector:
Note
Avoid using your S3 storage in FUSE
To set up an S3 connection from the notebook code:
-
Create secrets:
token(with ID) andkey_value(with a secret part of the static access key for the service account). -
Open the DataSphere project:
-
Select the project in your community or on the DataSphere home page
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 name of the bucket and retrieve a list of objects it contains:
for key in s3.list_objects(Bucket='<bucket_name>')['Contents']: print(key['Key'])