import os
import time

import boto3
from botocore.config import Config


def main():
    required = (
        "AWS_ACCESS_KEY_ID",
        "AWS_SECRET_ACCESS_KEY",
        "EODATA_ENDPOINT",
        "EODATA_KEY",
    )
    missing = [name for name in required if not os.environ.get(name)]
    if missing:
        raise RuntimeError("Missing environment variables: " + ", ".join(missing))

    s3 = boto3.client(
        "s3",
        endpoint_url=os.environ["EODATA_ENDPOINT"],
        config=Config(signature_version="s3v4", s3={"addressing_style": "path"}),
    )
    s3.download_file("eodata", os.environ["EODATA_KEY"], "/app/image.png")
    print("Downloaded /app/image.png", flush=True)
    # Allow time to inspect or copy the file before this demonstration pod exits.
    time.sleep(300)


if __name__ == "__main__":
    main()
