Skip to content

Instantly share code, notes, and snippets.

@iann0036
Created December 7, 2017 04:55
Show Gist options
  • Save iann0036/9135dae701fb052cf8f0c4f1a6c581b3 to your computer and use it in GitHub Desktop.
Save iann0036/9135dae701fb052cf8f0c4f1a6c581b3 to your computer and use it in GitHub Desktop.
Download Cloudfronted Bucket
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