Created
December 7, 2017 04:55
-
-
Save iann0036/9135dae701fb052cf8f0c4f1a6c581b3 to your computer and use it in GitHub Desktop.
Download Cloudfronted Bucket
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 xml.etree.ElementTree | |
import pprint | |
import re | |
import os | |
import threading | |
base_address = "https://xxxxxxxxxxxxxxxx.cloudfront.net/" | |
def createAndOpen(filename): | |
try: | |
os.makedirs(os.path.dirname(filename)) | |
except: | |
pass | |
return open(filename, 'w') | |
def download_file(key): | |
content = requests.get(base_address + key).text.encode('utf-8') | |
with createAndOpen(bucket_name + "/" + key) as f: | |
f.write(content) | |
print(key) | |
return | |
r = requests.get(base_address) | |
xmlroot = xml.etree.ElementTree.fromstring(re.sub(' xmlns="[^"]+"', '', r.text, count=1)) | |
bucket_name = xmlroot.find("Name").text | |
threads = [] | |
for item in xmlroot.findall("Contents"): | |
key = item.find("Key").text | |
if not key.endswith("/"): | |
t = threading.Thread(target=download_file, args=(key,)) | |
threads.append(t) | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment