Skip to content

Instantly share code, notes, and snippets.

@gfilla
Last active Oct 18, 2017
Embed
What would you like to do?
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