3This is a rudimentary script for restoring S3 objects from Glacier.
5You pass it a text file with a list of S3 URIs to restore, and it will
6initiate a Glacier restore for each of them.
8You can also use it to track the progress of a restore operation -- it
9reports a count of how many objects are in-progress/already restored.
15from botocore.exceptions import ClientError
19from _common import create_s3_session
21sys.path.append(os.path.join(os.environ["HOME"], "repos", "concurrently"))
22from concurrently import concurrently # noqa: E402
25def restore_object(s3_client, s3_uri):
26 uri = hyperlink.URL.from_text(s3_uri)
29 key = "/".join(uri.path)
31 head_resp = s3_client.head_object(Bucket=bucket, Key=key)
33 if head_resp.get("Restore") == 'ongoing-request="true"':
34 return "RestoreInProgress"
36 if 'ongoing-request="false"' in head_resp.get("Restore", ""):
37 return "RestoredSuccessfully"
40 resp = s3_client.restore_object(
43 RestoreRequest={"Days": 7, "GlacierJobParameters": {"Tier": "Standard"}},
45 except ClientError as err:
46 if err.response["Error"]["Code"] == "RestoreAlreadyInProgress":
47 return "RestoreInProgress"
51 if resp["ResponseMetadata"]["HTTPStatusCode"] == 200:
52 return "RestoredSuccessfully"
54 return "RestoreInProgress"
57if __name__ == "__main__":
61 sys.exit(f"Usage: {__file__} <LIST_OF_KEYS>")
64 "RestoredSuccessfully": 0,
65 "RestoreInProgress": 0,
68 with open(path) as infile:
69 s3_uris = [line.strip() for line in infile]
71 s3 = create_s3_session(s3_uris[0], role_name="developer").client("s3")
73 for _, output in tqdm.tqdm(
74 concurrently(inputs=s3_uris, handler=lambda s3_uri: restore_object(s3, s3_uri)),
79 from pprint import pprint