Created
September 8, 2017 20:28
-
-
Save kefo/182c0ac2ee2dc45812c11fe3e6e457d6 to your computer and use it in GitHub Desktop.
Bootstrap 'glacier' moto and execute some jobs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import sys | |
import io | |
from io import BytesIO | |
def do_request(method, url, headers={}, data={}): | |
r = requests.request(method, url, headers=headers, data=data) | |
if r.status_code > 399: | |
print() | |
print("Something went wrong with request: " + url) | |
print("Status: " + str(r.status_code)) | |
print() | |
sys.exit(1) | |
else: | |
print() | |
print("Request succeeded: " + method + " " + url) | |
print("Status: " + str(r.status_code)) | |
for h in r.headers: | |
print(h + ": " + r.headers[h]) | |
try: | |
print(json.dumps(r.json(), sort_keys=True, indent=4)) | |
except: | |
print(r.text) | |
return r | |
def get_file(url): | |
content = requests.get(url).content | |
fopen = io.BytesIO(content) | |
return fopen | |
service_url = "http://localhost:5000/" | |
account_id = "kefo" | |
print("Adding some vaults...") | |
vname01 = 'v01' | |
vname02 = 'v02' | |
do_request("PUT", service_url + account_id + "/vaults/"+ vname01) | |
do_request("PUT", service_url + account_id + "/vaults/"+ vname02) | |
print() | |
print() | |
print("List vaults...") | |
vs = do_request("GET", service_url + account_id + "/vaults") | |
print("Adding some archives...") | |
archives = [ | |
{ | |
"name": "a01", | |
"file_url": "https://lakeimagesweb.artic.edu/iiif/c30ee48a-b484-265c-3368-c026a309a1a1/full/!800,800/0/default.jpg", | |
"description": '{"Content-type": "image/jpeg"}' | |
}, | |
{ | |
"name": "a02", | |
"file_url": "https://lakeimagesweb.artic.edu/iiif/4dfe3f4e-5120-1f46-ab39-6100f95efbb2/full/!800,800/0/default.jpg", | |
"description": '{"Content-type": "image/jpeg"}' | |
}, | |
{ | |
"name": "a03", | |
"file_url": "https://lakeimagesweb.artic.edu/iiif/16a5743a-c933-cd7f-87aa-88b3456db07b/full/!800,800/0/default.jpg", | |
"description": '{"Content-type": "image/jpeg"}' | |
}, | |
{ | |
"name": "a04", | |
"file_url": "https://lakeimagesweb.artic.edu/iiif/73a77f09-19a7-37af-bfae-1e9ccdc5f417/full/!800,800/0/default.jpg", | |
"description": '{"Content-type": "image/jpeg"}' | |
}, | |
{ | |
"name": "a05", | |
"file_url": "https://lakeimagesweb.artic.edu/iiif/70524426-f26f-b001-5e1f-a30683b3b02b/full/!800,800/0/default.jpg", | |
"description": '{"Content-type": "image/jpeg"}' | |
} | |
] | |
for a in archives: | |
f = get_file(a["file_url"]) | |
headers = {"x-amz-archive-description": a["description"]} | |
do_request("POST", service_url + account_id + "/vaults/"+ vname02 + "/archives", headers, f) | |
print() | |
print() | |
print("List vaults...") | |
vs = do_request("GET", service_url + account_id + "/vaults") | |
print() | |
print() | |
print("Delete vault v01....") | |
do_request("DELETE", service_url + account_id + "/vaults/" + vname01) | |
print() | |
print() | |
print("List vaults...") | |
vs = do_request("GET", service_url + account_id + "/vaults") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import sys | |
import time | |
import io | |
from io import BytesIO | |
def do_request(method, url, headers={}, data={}): | |
r = requests.request(method, url, headers=headers, data=data) | |
if r.status_code > 399: | |
print() | |
print("Something went wrong with request: " + url) | |
print("Status: " + str(r.status_code)) | |
print() | |
sys.exit(1) | |
else: | |
print() | |
print("Request succeeded: " + method + " " + url) | |
print("Status: " + str(r.status_code)) | |
for h in r.headers: | |
print(h + ": " + r.headers[h]) | |
try: | |
if not url.endswith("output"): | |
print(json.dumps(r.json(), sort_keys=True, indent=4)) | |
else: | |
print("Binary content.") | |
except: | |
print(r.text) | |
return r | |
def wait_a_minute(url): | |
interval = 15 | |
time.sleep(2) | |
print() | |
print("Sleeping for " + str(interval) + " seconds") | |
time.sleep(interval) | |
print() | |
print() | |
print("Job status check (should still be in 'InProgress')...") | |
do_request("GET", url) | |
print() | |
print("Sleeping for " + str(interval) + " seconds") | |
time.sleep(interval) | |
print() | |
print() | |
print("Job status check (should still be in 'InProgress')...") | |
do_request("GET", url) | |
print() | |
print("Sleeping for " + str(interval) + " seconds") | |
time.sleep(interval) | |
print() | |
print() | |
print("Job status check (should still be in 'InProgress')...") | |
do_request("GET", url) | |
print() | |
print("Sleeping for " + str(interval) + " seconds") | |
time.sleep(interval) | |
print() | |
print() | |
print("Job status check (should be 'Succeeded')...") | |
do_request("GET", url) | |
def get_file(url): | |
content = requests.get(url).content | |
fopen = io.BytesIO(content) | |
return fopen | |
service_url = "http://localhost:5000/" | |
account_id = "kefo" | |
inventory_job = { | |
'Description': 'An Inventory Job, booya', | |
'Format': 'JSON', | |
'Type': 'inventory-retrieval', | |
} | |
vname = 'v02' | |
print() | |
print("Initiating job (Inventory Retrieval)...") | |
i = do_request("POST", service_url + account_id + "/vaults/" + vname + "/jobs", {"Content-type": "application/json"}, json.dumps(inventory_job)) | |
jobid = i.headers["x-amz-job-id"] | |
wait_a_minute(service_url + account_id + "/vaults/" + vname + "/jobs/" + jobid) | |
print() | |
print() | |
print("Getting job output...") | |
j = do_request("GET", service_url + account_id + "/vaults/" + vname + "/jobs/" + jobid + "/output" ) | |
jjson = j.json() | |
archive = jjson["ArchiveList"][2] | |
print() | |
print() | |
print("Requesting this archive...") | |
print( json.dumps(archive, sort_keys=True, indent=4) ) | |
archive_job = { | |
'ArchiveId': archive["ArchiveId"], | |
'Description': 'An Archive Job, booya', | |
'Type': 'archive-retrieval', | |
} | |
a = do_request("POST", service_url + account_id + "/vaults/" + vname + "/jobs", {"Content-type": "application/json"}, json.dumps(archive_job)) | |
jobid = a.headers["x-amz-job-id"] | |
wait_a_minute(service_url + account_id + "/vaults/" + vname + "/jobs/" + jobid) | |
print() | |
print() | |
print("Getting job output...") | |
j = do_request("GET", service_url + account_id + "/vaults/" + vname + "/jobs/" + jobid + "/output" ) | |
print() | |
print() | |
print("Deleting archive...") | |
do_request("DELETE", service_url + account_id + "/vaults/" + vname + "/archives/" + archive["ArchiveId"]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment