Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zh4n7wm/0aa748ad4d6ccda084a4bf51c95db269 to your computer and use it in GitHub Desktop.
Save zh4n7wm/0aa748ad4d6ccda084a4bf51c95db269 to your computer and use it in GitHub Desktop.
list and download the object in the aws s3 bucket

import json import pathlib

import boto3

def download_s3_obj_versions(_bucket, _key_prefix, _filename, is_save=True): client = boto3.client("s3")

_key = (pathlib.Path(_key_prefix) / _filename).as_posix()

response = client.list_object_versions(Bucket=_bucket, Prefix=_key)

for v in response["Versions"]:
    print(v["LastModified"])
    if is_save:
        _local = (pathlib.Path("/tmp") / _filename).as_posix()
        client.download_file(
            _bucket,
            _key,
            _local + "_" + v["LastModified"].strftime("%Y%m%d%H%M%S"),
            ExtraArgs={"VersionId": v["VersionId"]},
        )

if name == "main": _bucket = "" _filename = "" # e.g.: mysql.pdf _key_prefix = "" # end with '/', e.g.: books/db/mysql

download_s3_obj_versions(_bucket, _key_prefix, _filename, is_save=True)
# check_stream_url("/tmp", "ottera.tcl_tv_plus.all_vod.json_")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment