Beispiel #1
0
def download_file_from_s3(bucket_name: str, key: str, local_path: str) -> None:
    """
    Downloads file from S3 anonymously
    :param bucket_name: S3 Bucket name
    :param key: S3 File key name
    :param local_path: Local file path to download as
    """
    verify_ssl = get_verify_ssl()
    if not os.path.isfile(local_path):
        client = boto3.client("s3",
                              config=Config(signature_version=UNSIGNED),
                              verify=verify_ssl)

        try:
            logger.info("Downloading S3 data file...")
            total = client.head_object(Bucket=bucket_name,
                                       Key=key)["ContentLength"]
            with ProgressPercentage(client, bucket_name, key,
                                    total) as Callback:
                client.download_file(bucket_name,
                                     key,
                                     local_path,
                                     Callback=Callback)
        except ClientError:
            raise KeyError(
                f"File {key} not available in {bucket_name} bucket.")

    else:
        logger.info(f"Reusing cached file {local_path}...")
Beispiel #2
0
def download_private_file_from_s3(bucket_name: str, key: str, local_path: str):
    """
    Downloads file from S3 using credentials stored in ENV variables.
    :param bucket_name: S3 Bucket name
    :param key: S3 File keyname
    :param local_path: Local file path to download as
    """
    verify_ssl = get_verify_ssl()
    if not os.path.isfile(local_path):
        client = boto3.client(
            "s3",
            aws_access_key_id=os.getenv("ARMORY_PRIVATE_S3_ID"),
            aws_secret_access_key=os.getenv("ARMORY_PRIVATE_S3_KEY"),
            verify=verify_ssl,
        )
        try:
            logger.info("Downloading S3 data file...")
            total = client.head_object(Bucket=bucket_name,
                                       Key=key)["ContentLength"]
            with ProgressPercentage(client, bucket_name, key,
                                    total) as Callback:
                client.download_file(bucket_name,
                                     key,
                                     local_path,
                                     Callback=Callback)
        except ClientError:
            raise KeyError(
                f"File {key} not available in {bucket_name} bucket.")
    else:
        logger.info("Reusing cached S3 data file...")