Hands-On: Python (boto3) & Presigned URLs

Upload, read, and safely share a file from Python code.

1 min read

boto3 is the official AWS SDK for Python. Install it with pip install boto3. It picks up credentials the same way the CLI does, and when your code runs on Lambda it uses the function's role automatically.

Try it

s3_demo.py
import boto3

s3 = boto3.client("s3")
BUCKET = "notesnap-uploads-dev-4821"

# 1. Upload text as an object
s3.put_object(
    Bucket=BUCKET,
    Key="notes/todo.txt",
    Body="Finish the S3 tutorial".encode("utf-8"),
    ContentType="text/plain",
)

# 2. Read it back
obj = s3.get_object(Bucket=BUCKET, Key="notes/todo.txt")
print("Content:", obj["Body"].read().decode("utf-8"))

# 3. Create a link anyone can use for 15 minutes, with no public bucket needed
url = s3.generate_presigned_url(
    "get_object",
    Params={"Bucket": BUCKET, "Key": "notes/todo.txt"},
    ExpiresIn=900,  # seconds
)
print("Share this link:", url)

What just happened

  • put_object and get_object are the two most-used S3 calls. Everything else builds on them.
  • generate_presigned_url doesn't call AWS at all. It signs a URL locally with your credentials. Whoever holds the link gets exactly the permission you had, for that one object, until it expires.
Tip

Presigned URLs work for uploads too ("put_object"). That lets a browser upload directly to S3 without the file passing through your server.

Key takeaways

  • boto3 uses the same credentials as the CLI, or the Lambda role when running in Lambda.
  • put_object and get_object are the core read/write calls.
  • Presigned URLs grant temporary access to one object, for both downloads and uploads.

Check your understanding

Pick an answer to see the explanation.

  1. 1. What permission does someone get from a presigned URL?

Next in “Build a serverless AI app”
You finished S3. Continue with the Lambda tutorial