Skip to content

Instantly share code, notes, and snippets.

@gfilla
Last active October 18, 2017 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfilla/8963a33e09f2d28ddf51a94d56b3c234 to your computer and use it in GitHub Desktop.
Save gfilla/8963a33e09f2d28ddf51a94d56b3c234 to your computer and use it in GitHub Desktop.
How to read a zip file in Python 3 from object storage in Data Science Experience
import zipfile
from io import BytesIO
import requests
import json
import pandas as pd
def get_zip_file(credentials):
url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
data = {'auth': {'identity': {'methods': ['password'], 'password': {'user': {'name': credentials['username'],'domain': {'id': credentials['domain_id']}, 'password': credentials['password']}}}}}
headers1 = {'Content-Type': 'application/json'} resp1 = requests.post(url=url1, data=json.dumps(data), headers=headers1)
resp1_body = resp1.json()
for e1 in resp1_body['token']['catalog']:
if(e1['type']=='object-store'):
for e2 in e1['endpoints']:
if(e2['interface']=='public'and e2['region']==credentials['region']): url2 = ''.join([e2['url'],'/', credentials['container'], '/', credentials['filename']]) s_subject_token = resp1.headers['x-subject-token'] headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
s_subject_token = resp1.headers['x-subject-token']
headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
r = requests.get(url=url2, headers=headers2, stream=True)
z = zipfile.ZipFile(BytesIO(r.content))
z.extractall()# save zip contents to disk
return(z)
z = get_zip_file(credentials)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment