#!/usr/bin/env python # Basic setup (URL etc) import json import time #import requests from rauth import OAuth2Session base = "https://unicore.data.kit.edu:8080/DEFAULT-SITE/rest/core" print "Accessing REST API at ", base # # setup auth using rauth library # assume bearer token is saved in the oidcToken file # clientId = "portal-client" file = open("oidcToken","r") btoken = file.read().replace('\n','') rauth_session = OAuth2Session(client_id=clientId, access_token=btoken) # # check that we can access the server # headers = {'Accept': 'application/json'} r = rauth_session.get(base, headers=headers, verify=False) if r.status_code!=200: print "Error accessing the server!" else: print "Ready." print json.dumps(r.json(), indent=4) # # list the existing storages # headers = {'Accept': 'application/json'} r = rauth_session.get(base+"/storages", headers=headers, verify=False) storagesList = r.json() print json.dumps(r.json(), indent=4) # # get the third storage (in our case, this is the https s3 storage) # headers = {'Accept': 'application/json'} s3storageUrl = storagesList['storages'][2] r = rauth_session.get(s3storageUrl, headers=headers, verify=False) s3storageProps = r.json() print json.dumps(r.json(), indent=4) # # list the files (buckets) in this storage # headers = {'Accept': 'application/json'} bucketBase = s3storageProps['_links']['files']['href'] r = rauth_session.get(bucketBase, headers=headers, verify=False) bucketList = r.json() print json.dumps(r.json(), indent=4) # # list the objects in the first bucket # headers = {'Accept': 'application/json'} bucket = bucketList['children'][0] bucketUrl = bucketBase + bucket r = rauth_session.get(bucketUrl, headers=headers, verify=False) objectList = r.json() print json.dumps(r.json(), indent=4) # # download the first file in the first bucket # headers = {'Accept': 'application/octet-stream'} object2down = objectList['children'][0] object2downUrl = bucketBase + object2down r = rauth_session.get(object2downUrl, headers=headers, verify=False) objectName = object2down.split('/')[-1] fileOut = open(objectName, "wb") fileOut.write(r.content) fileOut.close() # to only get the file info, not the data headers = {'Accept': 'application/json'} r = rauth_session.get(object2downUrl, headers=headers, verify=False) print json.dumps(r.json(), indent=4) # # upload a file in the first bucket # must have file 'test' on local filesystem in current directory # can replace the name of object2up with the file you wish to upload # headers = {'Content': 'application/octet-stream'} object2up = "test" object2upUrl = bucketUrl + "/" + object2up bytes2up = open(object2up, "rb") r = rauth_session.put(object2upUrl, data=bytes2up, headers=headers, verify=False) print 'Object ' + object2up + ' uploaded' bytes2up.close() # # list the files in the first bucket again # headers = {'Accept': 'application/json'} bucket = bucketList['children'][0] bucketUrl = bucketBase + bucket r = rauth_session.get(bucketUrl, headers=headers, verify=False) objectList = r.json() print json.dumps(r.json(), indent=4) # # print information for uploaded object # headers = {'Accept': 'application/json'} r = rauth_session.get(object2upUrl, headers=headers, verify=False) print json.dumps(r.json(), indent=4) # # delete the uploaded object # headers = {'Accept': 'application/json'} r = rauth_session.delete(object2upUrl, headers=headers, verify=False) print 'Object ' + object2up + ' deleted' # # list the files in the first bucket again # headers = {'Accept': 'application/json'} bucket = bucketList['children'][0] bucketUrl = bucketBase + bucket r = rauth_session.get(bucketUrl, headers=headers, verify=False) objectList = r.json() print json.dumps(r.json(), indent=4)