与Object存储的Java连接返回未授权的401

时间:2015-12-07 09:46:02

标签: ibm-cloud

我写了一个应用程序来保存对象存储中的文件。 我在连接到对象存储时遇到问题,在Bluemix上连接返回错误(AuthenticationException {message = Unauthorized,status = 401})

我正在使用openstack4j实现,这是我的代码:

String envServices = System.getenv("VCAP_SERVICES");
    if (envServices != null) {
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(envServices);
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray vcapArray = (JSONArray) jsonObject.get("Object-Storage");
        JSONObject vcap = (JSONObject) vcapArray.get(0);
        JSONObject credentials = (JSONObject) vcap.get("credentials");
        username = credentials.get("username").toString();
        password = credentials.get("password").toString();
        auth_url = credentials.get("auth_url").toString() + "/v3";
        domain = credentials.get("domainId").toString();
        project = credentials.get("projectId").toString();
    } else {
        username = "someuser";
        password = "somepassword";
        auth_url = "https://identity.open.softlayer.com";
        domain = "sfsd";
        project = "object_storage_xxxxxxx";
    }
    Identifier domainIdent = Identifier.byName(domain);
    Identifier projectIdent = Identifier.byName(project);


    OSClient os = OSFactory.builderV3().endpoint(auth_url).credentials(username, password,domainIdent).scopeToProject(projectIdent, domainIdent)
            .authenticate();
    objectStorage = os.objectStorage();
    account = objectStorage.account().get();

任何帮助将不胜感激。 阿西

3 个答案:

答案 0 :(得分:0)

您的VCAP_SERVICES是这样的:

sudo apt-get remove python-apport

所以,你必须改变这一行:

{
"Object-Storage": [
{
  "name": "Object-Storage - YP",
  "label": "Object-Storage",
  "plan": "Free",
  "credentials": {
     "auth_url": "https://identity.open.softlayer.com",
     "project": "object_storage_d049255b",
     "projectId": "0f47b41b06d047f9aae3b33f1db061ed",
     "region": "dallas",
     "userId": "ad78b2a3f843466988afd077731c61fc",
     "username": "user_202db1f8a7aa3f3ac51ec68f10dbe7dc29070bc7",
     "password": "K/jyIi2jR=1?D.TP",
     "domainId": "2df6373c549e49f8973fb6d22ab18c1a",
     "domainName": "639347"
    }
   }
  ]
}

使用:

domain = credentials.get("domainId").toString();
project = credentials.get("projectId").toString();

答案 1 :(得分:0)

这段代码将帮助您将对象存储服务连接到您的应用程序

String envServices = System.getenv("VCAP_SERVICES");
JSONParser parser = new JSONParser();

try{

Object obj = parser.parse(envServices);
JSONObject jsonObject = (JSONObject) obj;
JSONArray vcapArray = (JSONArray) jsonObject.get("Object-Storage");
JSONObject vcap = (JSONObject) vcapArray.get(0);
JSONObject credentials = (JSONObject) vcap.get("credentials");
String userId = credentials.get("userId").toString();
String password = credentials.get("password").toString();
String auth_url = credentials.get("auth_url").toString() + "/v3";
String domain = credentials.get("domainName").toString();
String project = credentials.get("project").toString();
Identifier domainIdent = Identifier.byName(domain);
Identifier projectIdent = Identifier.byName(project);

OSClient os = OSFactory.builderV3()
              .endpoint(auth_url)
              .credentials(userId, password)
              .scopeToProject(projectIdent, domainIdent)
              .authenticate();
SwiftAccount account = os.objectStorage().account().get();

}catch(Exception e){
   e.printStackTrace();
}

答案 2 :(得分:0)

问题是这一行>> Identifier domainIdent = Identifier.byName(domain);

应该是这样的>> Identifier domainIdent = Identifier.byId(domain); 因为您传递的是域ID而不是域名。