Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

How-to manage S3 cloud object storage with AWS CLI

This instructional guide shows you how to upload files from your hub to AWS S3 cloud object storage. In this example, we cover some basic AWS CLI commands for managing S3 objects within cloud object storage for your hub.

Basic AWS CLI commands in the Terminal

In the Terminal, check that the AWS CLI commands are available in your image with

$ which aws
/srv/conda/envs/notebook/bin/aws

If this returns nothing, then you can temporarily install the package with

curl https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o $HOME/.local/awscliv2.zip
unzip $HOME/.local/awscliv2.zip
export PATH=$HOME/.local/aws/dist:$PATH

List prefixes within an S3 bucket

Prefix
There is no concept of “folders” in flat cloud object storage and every object is instead indexed with a key-value pair. Prefixes are a string of characters at the beginning of the object key name used to organize objects in a similar way to folders.

Storage buckets on a 2i2c hub are organized into prefixes named after a hub user’s username. To list the prefixes of users that have stored files in cloud object storage, use the command

$ aws s3 ls $SCRATCH_BUCKET
                           PRE <username1>/
                           PRE <username2>/

where the label PRE indicates the item listed is a prefix and not an object.

List the contents of your prefix

List the contents of files stored under your own prefix with the command

aws s3 ls $SCRATCH_BUCKET/

Copy files on the hub to and from a bucket

Copy a file on the hub to your prefix in the scratch bucket with the command

$ aws s3 cp <filepath> $SCRATCH_BUCKET/
upload: ./<filepath> to s3://2i2c-aws-us-scratch-showcase/<username>/<filepath>

and copy a file from your prefix in the scratch bucket with the command

$ aws s3 cp $SCRATCH_BUCKET/<source_filepath> <target_filepath>
download: s3://2i2c-aws-us-scratch-showcase/<username>/<source_filepath> to ./<target_filepath>

Delete a file from a bucket

Delete a file from your prefix in a bucket with the command

$ aws s3 rm $SCRATCH_BUCKET/<filepath>
delete: s3://2i2c-aws-us-scratch-researchdelight/<username>/<filepath>

Upload files to an S3 bucket from outside the hub

We outline a workflow for how to transfer datasets to the AWS bucket from outside the hub, such as your local machine or a remote server. This is done by generating a temporary access token that is valid for up to 1 hour.

  1. Set up a new software environment on your local machine

    mamba create --name aws_transfer aws-cli
  2. Activate the environment

    mamba activate aws_transfer
    
  3. Generate a temporary access token from your 2i2c hub

    aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name $JUPYTERHUB_CLIENT_ID --web-identity-token "$(cat $AWS_WEB_IDENTITY_TOKEN_FILE)" --duration-seconds 1000 
  4. Note the key-values returned for AccessKeyId, SecretAccessKey and SessionToken

  5. Configure the ~/.aws/credentials file on your local machine with a new profile using the following commands

    aws configure set aws_access_key_id <AccessKeyId> --profile <profile_name>
    aws configure set aws_secret_access_key <SecretAccessKey> --profile <profile_name>
    aws configure set aws_session_token <SessionToken> --profile <profile_name>
  6. Set the region in your ~/.aws/config file on your local machine using the following command

    aws configure set region <data_center_location>
  7. Define the AWS_PROFILE environment variable on your local machine

    AWS_PROFILE=<profile_name>
  8. Define the $SCRATCH_BUCKET environment variable

    SCRATCH_BUCKET=s3://<bucket_name>/<username> 
  9. Upload the data to the storage bucket

    $ aws s3 cp <your-data> $SCRATCH_BUCKET
    upload: ./<your-data> to s3://<bucket_name>/<username>/<your-data>
  10. Check the contents of your prefix

    $ aws s3 ls $SCRATCH_BUCKET/
    2024-07-04 17:01:54          4 <your-data>

FAQs