1

I'm attempting to set up a connection to a Bluemix Object Storage for a project that is different than the default one the project created. This is a problem because:

1) When I go to add a new connection, the Object Storage instance I want to use is not under data service.

2) When I go to add a Softlayer Object Storage, the credentials I'm asked for are (Login URL, Access Key, and Secret Key), but the credentials I have for my instance are ("auth_url":"project":"projectId":"region":"userId":"username":"password":"domainId":"domainName":"role")

3) I have a good interface to a placeholder object storage, but I would like to replace it with the other instance.

Please help me access the data in a different Bluemix Object Storage instance other than the one attached to the project by default.

Ross Lewis
  • 635
  • 2
  • 7
  • 16
  • Can you post the error? – Raphael K May 26 '17 at 14:59
  • There is no code being executed in the question above, however, what I'm aiming to do is [step 8 in this tutorial](https://developer.ibm.com/recipes/tutorials/using-ibm-object-storage-in-bluemix-with-python/). Right now, when I try to connect, I get this error `Authorization Failure. Authorization failed: The resource could not be found. (HTTP 404) (Request-ID: req-861d0e7a-e89a-4c14-a198-2d9f829cb8ac)` – Ross Lewis May 26 '17 at 15:04
  • I'm confused if you're trying to use SoftLayer object storage, or Bluemix object storage. Can you please clarify? The credentials you list above seem to imply Bluemix Object storage, but you call it a SoftLayer one. – Trent Gray-Donald Jun 12 '17 at 05:40
  • I am using a Bluemix object storage. I mentioned Softlayer because that is the only option for adding a connection in DSX. – Ross Lewis Jun 13 '17 at 15:16

4 Answers4

2

In addition to what @Sumit Goyal Answered. You need to download the file in local gpfs in order to use apis or libraries that don't support reading from swift object storage or in other words only support reading from local storage/file system.

objStorCred = { "auth_url": "https://identity.open.softlayer.com", "project": "object_storage_XXXXX", "projectId": "XXXXX5a3", "region": "dallas", "userId": "XXXXXX98a15e0", "username": "admin_fXXXXX9", "password": "XXXXX", "domainId": "aXXXX5a", "domainName": "XXXX", "role": "admin" }

from io import StringIO import requests import json import pandas as pd

# @hidden_cell

# This function accesses a file in your Object Storage. The definition contains your credentials.

# You might want to remove those credentials before you share your notebook.

def get_object_storage_file(container, filename):

"""This functions returns a StringIO object containing the file content from Bluemix Object Storage."""

url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
data = {'auth': {'identity': {'methods': ['password'],
        'password': {'user': {'name': objStorCred['username'],'domain': {'id': objStorCred['domainId']},
        'password': objStorCred['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']=='dallas'):
                        url2 = ''.join([e2['url'],'/', container, '/', filename])
s_subject_token = resp1.headers['x-subject-token']
headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
resp2 = requests.get(url=url2, headers=headers2)
return resp2

Note the instead of getting a stringIO object, we get the response object.

Now you can use intermediate local storage to store the .mat file.

Then call this function.

r = get_object_storage_file("containerr1", "example.mat")

with open('example.mat', 'wb') as file:  
file.write(r.content)

Now read the file using h5py. You may need to install h5py using pip install h5py.

import h5py

f = h5py.File('example.mat') f.keys()

Thanks, Charles.

charles gomes
  • 2,105
  • 8
  • 15
1

You can use the function generated by the insert to code feature and plug in the credentials from the other object storage. For example:

from io import StringIO
import requests
import json
import pandas as pd

# @hidden_cell
# This function accesses a file in your Object Storage. The definition contains your credentials.
# You might want to remove those credentials before you share your notebook.
def get_object_storage_file_with_credentials(container, filename):
"""This functions returns a StringIO object containing
the file content from Bluemix Object Storage."""

url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
data = {'auth': {'identity': {'methods': ['password'],
        'password': {'user': {'name': 'admin_xxxx','domain': {'id': 'xxxxxxxxxxx'},
        'password': 'xxxxxxxxxx'}}}}}
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']=='dallas'):
                        url2 = ''.join([e2['url'],'/', container, '/', filename])
s_subject_token = resp1.headers['x-subject-token']
headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
resp2 = requests.get(url=url2, headers=headers2)
return StringIO(resp2.text)

Here, replace the values for user name, domain id and password from your next Bluemix Object Store Credentials. After that you can simply access the files from a container in that object storage by:

cars_df = pd.read_csv(get_object_storage_file_with_credentials('<containerName>', '<filename>.csv'))
cars_df.head()
Sumit Goyal
  • 567
  • 3
  • 15
  • The files on Object Storage are .mat files so I'd like to download them to my cloud environment on DSX. The issue is that I've tried the above code before and I get an authentication error. – Ross Lewis May 26 '17 at 14:31
1

I strongly recommend you look at https://github.com/ibm-cds-labs/ibmos2spark (which works for Python, R and Scala).

For Python + SoftLayer credentials it would specifically be this code:

slos = ibmos2spark.softlayer(sc, configuration_name, auth_url, tenant, username, password) data = sc.textFile(slos.url(container_name, object_name))

(taken from https://github.com/ibm-cds-labs/ibmos2spark/tree/master/python#softlayer )

The next question is how to load .mat files - which seems like a detour through Read .mat files in Python and using "sc.binaryFiles()" to get them into memory first.

Trent Gray-Donald
  • 2,221
  • 14
  • 16
  • 1
    @RossLewis - I suggest you delete that link and get new credentials. You're leaking live credentials to everyone. Otherwise it's ok, but it's a pure python story, not a spark one. – Trent Gray-Donald Jun 15 '17 at 17:26
0

In R:

## Credentials and libraries to write to object storage

## Install necessary library
install_github('IBMDataScience/objectStoreR')
library('objectStoreR')  

## Provide Credentials (fill in with your details from Bluemix)
credentials <-list(auth_url = "https://identity.open.softlayer.com",
         project = "object_storage_d7a568f8_ac53_4bc4_8834_f0e9962068f9",
         project_id = "e0c826f12030487493z2df3957621744",
         region = "dallas",
         user_id = "694102a676ef4252u19492c45fbebc4b",
         domain_id = "47ea410d2b51478d9f119fade708fbefe4",
         domain_name =  "1004827",
         username = "admin_9c5c874ed726b5a41c7bb4f8b55f45e3e2c35778",
         password = "Tj^d9rZoDhy5eb]U",
         container = "mycontainer", 
         filename = "myfile.csv")

To write a file out to Object Storage:

## Status '201' is a successful signal
write.csv(outputDF,'myOutputFile.csv', row.names = F)
status <- objectStore.put(credentials,'myOutputFile.csv')
paste("Status for final output CSV:", status, sep = " ")

Similarly, to save a model object (note you have to change to file name in the list of credentials or create a second credentials variable):

saveRDS(object = finalMod, file = "myModel.rds")
status <- objectStore.put(credentials, "myModel.rds")
paste("Status for model object:", status, sep = " ")

Hope this helps!

Raphael K
  • 1,771
  • 1
  • 11
  • 21
  • Thanks for the answer, but I'm looking to implement the data load in Python. Also, the data is in .mat format. – Ross Lewis May 26 '17 at 14:32