Boto3 Installation Command¶

The local kernel pulls modules from the Python virtual environment, while ipykernel uses the global Python environment. Note: Only ipykernel supports handy magic functions like %pip!

# this notebook's kernel uses the local virtual environment
pip3 install -U boto3==1.42.30
pip3 install -U pyperclip
# ...
pip3 install --upgrade --force-reinstall boto3
pip3 install --no-cache --upgrade-strategy eager -I boto3
pip3 list
pip3 show boto3
pip3 index versions boto3

signed url for file download¶

set up env variables

In [4]:
import os, getpass
os.environ['AWS_REGION'] = getpass.getpass("enter AWS_REGION: ")
os.environ['AWS_ACCESS_KEY'] = getpass.getpass("enter AWS_ACCESS_KEY: ")
os.environ['AWS_SECRET_ACCESS'] = getpass.getpass("enter AWS_SECRET_ACCESS: ")

snippet to signed URL

In [5]:
import os
import boto3
from botocore.config import Config
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html
# https://github.com/boto/botocore/issues/2109#issuecomment-1305284157
# https://stackoverflow.com/questions/26533245/the-authorization-mechanism-you-have-provided-is-not-supported-please-use-aws4
my_config = Config(
  region_name=os.getenv('AWS_REGION'),
  signature_version = 'v4',
  retries = {
    'max_attempts': 10,
    'mode': 'standard'
  }
)

# Generate a signed S3 object URL.
s3 = boto3.client(
  's3',
  aws_access_key_id=os.getenv("AWS_ACCESS_KEY"),
  aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS"),
  config=my_config,
)
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/generate_presigned_url.html
signed_url = s3.generate_presigned_url(
  'get_object',
  Params={
    'Bucket': 'my-bucket-name',
    'Key': 'document.pdf',
  },
  HttpMethod='GET',
  ExpiresIn=3600
)
print("Signed URL:", signed_url)

import pyperclip as clip
clip.copy(signed_url)
print("Signed URL copied to clipboard.")