如何使用JAVA在对象存储中创建对象?

时间:2016-11-21 13:07:49

标签: java ibm-cloud-infrastructure object-storage

我知道如何通过get authenticate创建对象,而不是调用API

以下链接是有关如何进行身份验证的信息, Unauthorized error while connecting Object Storage from API from Postman

JAVA中是否有可用于执行这些操作的库?

我从下面的链接获得了图书馆,但它似乎很旧而且没有工作, https://github.com/softlayer/softlayer-object-storage-java/tree/master/sl-objectstorage

如果有人知道您能否发布如何操作?

使用库获得以下错误,

Error while handling an HTTP client call
java.lang.ClassCastException: org.restlet.data.Parameter incompatible with org.restlet.data.Header
    at org.restlet.engine.header.HeaderUtils.addExtensionHeaders(HeaderUtils.java:220)
    at org.restlet.engine.header.HeaderUtils.addRequestHeaders(HeaderUtils.java:479)
    at org.restlet.engine.adapter.ClientAdapter.toSpecific(ClientAdapter.java:163)
    at org.restlet.engine.adapter.HttpClientHelper.handle(HttpClientHelper.java:118)
    at org.restlet.Client.handle(Client.java:153)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1092)
    at org.restlet.resource.ClientResource.handleOutbound(ClientResource.java:1176)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1047)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1023)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:928)
    at org.restlet.resource.ClientResource.get(ClientResource.java:636)
    at com.softlayer.objectstorage.Client.httpRequest(Client.java:245)
    at com.softlayer.objectstorage.Client.get(Client.java:177)
    at com.softlayer.objectstorage.Client.auth(Client.java:128)
    at com.softlayer.objectstorage.Client.<init>(Client.java:107)
    at com.softlayer.objectstorage.Container.<init>(Container.java:77)
    at com.wrakafka.consumer.ObjectStorageUtil.createContainer(ObjectStorageUtil.java:24)
    at spark.KafkaConsumer.main(KafkaConsumer.java:51)

Nov 21, 2016 1:24:18 PM org.restlet.resource.ClientResource retry
INFO: A recoverable error was detected (1002), attempting again in 2000 ms.
Nov 21, 2016 1:24:20 PM org.restlet.engine.adapter.HttpClientHelper handle
INFO: Error while handling an HTTP client call
java.lang.ClassCastException: org.restlet.data.Parameter incompatible with org.restlet.data.Header
    at org.restlet.engine.header.HeaderUtils.addExtensionHeaders(HeaderUtils.java:220)
    at org.restlet.engine.header.HeaderUtils.addRequestHeaders(HeaderUtils.java:479)
    at org.restlet.engine.adapter.ClientAdapter.toSpecific(ClientAdapter.java:163)
    at org.restlet.engine.adapter.HttpClientHelper.handle(HttpClientHelper.java:118)
    at org.restlet.Client.handle(Client.java:153)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1092)
    at org.restlet.resource.ClientResource.retry(ClientResource.java:1650)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1100)
    at org.restlet.resource.ClientResource.handleOutbound(ClientResource.java:1176)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1047)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1023)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:928)
    at org.restlet.resource.ClientResource.get(ClientResource.java:636)
    at com.softlayer.objectstorage.Client.httpRequest(Client.java:245)
    at com.softlayer.objectstorage.Client.get(Client.java:177)
    at com.softlayer.objectstorage.Client.auth(Client.java:128)
    at com.softlayer.objectstorage.Client.<init>(Client.java:107)
    at com.softlayer.objectstorage.Container.<init>(Container.java:77)
    at com.wrakafka.consumer.ObjectStorageUtil.createContainer(ObjectStorageUtil.java:24)
    at spark.KafkaConsumer.main(KafkaConsumer.java:51)

Nov 21, 2016 1:24:20 PM org.restlet.resource.ClientResource retry
INFO: A recoverable error was detected (1002), attempting again in 2000 ms.
Nov 21, 2016 1:24:22 PM org.restlet.engine.adapter.HttpClientHelper handle
INFO: Error while handling an HTTP client call
java.lang.ClassCastException: org.restlet.data.Parameter incompatible with org.restlet.data.Header
    at org.restlet.engine.header.HeaderUtils.addExtensionHeaders(HeaderUtils.java:220)
    at org.restlet.engine.header.HeaderUtils.addRequestHeaders(HeaderUtils.java:479)
    at org.restlet.engine.adapter.ClientAdapter.toSpecific(ClientAdapter.java:163)
    at org.restlet.engine.adapter.HttpClientHelper.handle(HttpClientHelper.java:118)
    at org.restlet.Client.handle(Client.java:153)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1092)
    at org.restlet.resource.ClientResource.retry(ClientResource.java:1650)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1100)
    at org.restlet.resource.ClientResource.retry(ClientResource.java:1650)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1100)
    at org.restlet.resource.ClientResource.handleOutbound(ClientResource.java:1176)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1047)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1023)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:928)
    at org.restlet.resource.ClientResource.get(ClientResource.java:636)
    at com.softlayer.objectstorage.Client.httpRequest(Client.java:245)
    at com.softlayer.objectstorage.Client.get(Client.java:177)
    at com.softlayer.objectstorage.Client.auth(Client.java:128)
    at com.softlayer.objectstorage.Client.<init>(Client.java:107)
    at com.softlayer.objectstorage.Container.<init>(Container.java:77)
    at com.wrakafka.consumer.ObjectStorageUtil.createContainer(ObjectStorageUtil.java:24)
    at spark.KafkaConsumer.main(KafkaConsumer.java:51)

1 个答案:

答案 0 :(得分:1)

不幸的是,您只能使用以下内容上传文件:Softlayer Object Storage Java Client(无法创建对象),这里有一个验证示例,创建容器并使用客户端上传文件:

package com.softlayer.objectstorage.main;

import java.util.HashMap;
import java.util.Map;
import com.softlayer.objectstorage.Container;
import com.softlayer.objectstorage.ObjectFile;


public class ObjectStorage {
    String baseUrl;
    String user;
    String password;

    public ObjectStorage(String baseUrl, String user, String password){
        this.baseUrl = baseUrl;
        this.user = user;
        this.password = password;
    }

    public void createContainer(String containerName){

        try{
            Container containerCreate = new Container(containerName, baseUrl, user,
                    password, true);
            containerCreate.create();

        }catch (Exception e)
        {
            System.out.println(e);
        }
    }

    public void UploadFile(String containerName, String fileName, String path)
    {
        try{
            ObjectFile oFile = new ObjectFile(fileName, containerName, baseUrl, user, password, true);
            Map<String, String> tags = new HashMap<String, String>();
            tags.put("testtag", "Test Value");
            String newOb = oFile.uploadFile(path, tags);
        }catch (Exception e)
        {
            System.out.println(e);
        }


    }

    public static void main(String[] args) {

        /**
         * Define Object Storage's parameters
         */
        String baseUrl = "https://dal05.objectstorage.softlayer.net/auth/v1.0/";
        String user = "set me";
        String password = "set me";

        // Define the container name to create
        String containerName = "containerTest";
        // Define the file name to create in the object storage
        String fileName = "newTest.txt";
        // Define the location path from file that you wish to upload
        String pathFile = "C:\\Users\\Ruber Cuellar\\Documents\\test.txt";

        // Create Object Storage connection
        ObjectStorage objectStorage = new ObjectStorage(baseUrl, user, password);
        // Create Container
        objectStorage.createContainer(containerName);
        // Upload file
        objectStorage.UploadFile(containerName, fileName, pathFile);

    }
}

我希望它有所帮助,让我知道任何疑问或评论