Amazon S3
Last updated
Was this helpful?
Last updated
Was this helpful?
This article covers all the necessary steps to access files on Amazon S3, AWS's simple storage solution, from inside DataLab.
You need to have an existing Amazon S3 bucket (which is the S3 equivalent of a file folder). If you don't have a bucket yet, follow the instructions in to create one.
To programmatically access resources on AWS you need to create an access key that consists of an Access key ID and a Secret key that has the right permissions for what you intend to do with the S3 bucket from inside DataLab.
If you don't yet have such an access key, follow the instructions in to create a new access key.
Create a new, empty workbook, or click to create a workbook in your own account that contains all the Python code you need to connect to Amazon S3.
To use the account key credentials in this workbook, you need to store them in DataLab. To do so securely, you can use Environment variables.
In your new workbook, open "Environment > Environment variables..." in the menu bar, and click on "Add". You need to create a new set with 2 environment variables:
AWS_ACCESS_KEY_ID
: Set this to the access key ID you got in the previous step.
AWS_SECRET_ACCESS_KEY
: Set this to the secret key you got in the previous step.
Set a meaningful "Environment Variable Set Name", e.g. "AWS Access Key".
After filling in all fields, click "Create", "Next" and finally, "Connect". Your workbook session will restart, and AWS_ACCESS_KEY_ID
and AWS_SECRET_KEY
will now be available as environment variables in this workbook.
You can now switch to Python to access the files. We'll use boto3
for this, which is the official Python package to create, configure, and manage AWS services, among which Amazon S3. It's already installed by default, so we only have to import it.
To verify that everything was set up okay, let's list all the objects (files) in a specific S3 bucket. Make sure to update the AWS_BUCKET_NAME
to a bucket that is available in your AWS account.
This should've worked! Note that you don't need to explicitly fetch and provide the AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environment variables; boto3
expects these environment variables to be there with those names, and loads them behind the scenes.
Other than listing files, boto3
allows you to download and upload files, manage buckets, etc. Consult the to learn more.