• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

六、java操作swift对象存储(resultful方式实现)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

系列导航

一、swift对象存储环境搭建

二、swift添加存储策略

三、swift大对象--动态大对象

四、swift大对象--静态态大对象

五、java操作swift对象存储(官网样例)

六、java操作swift对象存储(resultful方式实现)

七、java操作swift对象存储(动态大对象)

八、java操作swift对象存储(静态大对象)

    上一节讲了如何通过swift官方的样例操作swift对象存储,官方样例中,种类还是有点少工作中不够用,但是swift集群的调用支持resultful这就给我们提供了另一种调用swift的方法。那么都支持那些操作类型呢请看下表。

一、 RESTful API 总结
     资源类型         URL                                   GET                                  PUT                                   POST                    DELETE         HEAD
     账户             /account/                               获取容器列表                    -                                        -                             -                    获取账户元数据
     容器             /account/container                获取对象列表                   创建容器                            更新容器元数据     删除容器         获取容器元数据
     对象             /account/container/object     获取对象内容和元数据      创建、更新或拷贝对象       更新对象元数据     删除对象         获取对象元数据   

    了解了以上知识后下面进入代码部分java使用resultful方式操作swift对象存储。

二、pom.xml依赖

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>


        <!--JSONArray-->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180130</version>
        </dependency>

三、方法类ResultFulBase.java

package swift.base;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

//resultFul接口方式实现基础操作(tempauth认证方式)
public class ResultFulBase {


    //访问swift的url地址
    public static String X_Storage_Url = "";

    //访问swift的令牌
    public static String X_Auth_Token = "";

    //分割符
    private static final String separator = System.getProperty("file.separator");

    public ResultFulBase(String url, String port, String user, String passwd) {
        get_AUTH(url, port, user, passwd);
    }

    //获取X-Storage-Url 和 X-Auth-Token begin
    //curl http://127.0.0.1:8080/auth/v1.0 -v -H \'X-Storage-User: admin:admin\' -H \'X-Storage-Pass: admin\'
    public void get_AUTH(String url, String port, String user, String passwd) {
        try {
            HttpGet req = new HttpGet("http://" + url + ":" + port + "/auth/v1.0");
            req.addHeader("X-Storage-User", user);
            req.addHeader("X-Storage-Pass", passwd);

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);
            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

            Header h1 = rsp.getFirstHeader("X-Storage-Url");
            Header h2 = rsp.getFirstHeader("X-Auth-Token");
            X_Storage_Url = h1.getValue();
            X_Auth_Token = h2.getValue();
            System.out.println(h1.getValue() + "---------" + h2.getValue());
            System.out.println("----------------------------------------");
            //获取X-Storage-Url 和 X-Auth-Token end
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查看账户信息
    //curl http://127.0.0.1:8080/v1/AUTH_admin -v -H \'X-Auth-Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36\'
    public void get_acount_info() {
        try {
            HttpGet req = new HttpGet(X_Storage_Url);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //创建 container(container2为创建的容器名称) 按照默认策略创建容器
    //curl http://127.0.0.1:8080/v1/AUTH_admin/container2 -X PUT -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36"
    public void create_container(String containerName) {
        try {
            HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //创建 container(containerpolicy2为创建的容器名称) 按照存储策略创建容器
    //curl http://127.0.0.1:8080/v1/AUTH_admin/containerpolicy2 -X PUT -H "X-Auth_Token: AUTH_tk7c21eddae31d449db36a5e8596d0cb71" -H "X-Storage-Policy: silver"

    //Policy是存储策略的名称
    public void create_container(String containerName, String Policy) {
        try {
            HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            req.addHeader("X-Storage-Policy", Policy);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取AUTH_admin账户下的容器container1的对象列表
    //curl http://127.0.0.1:8080/v1/AUTH_admin/container1 -X GET -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36"
    public void get_container_object_list(String containerName) {
        try {
            HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            HttpEntity entity = rsp.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity);
                String[] objectList = result.split("\n");
                for (int i = 0; i < objectList.length; i++) {
                    System.out.println("--" + objectList[i]);
                }
            }
            System.out.println("----------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //获取容器下指定条数的对象列表(有的容器下对象太多,对象都拿出来服务器压力很大)
    //limit  整数n,指明返回结果只包含n个值
    public void get_container_object_limit_list(String containerName, int limit) {
        try {
            HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName + "/?limit=" + limit);

            req.addHeader("X-Auth-Token", X_Auth_Token);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            HttpEntity entity = rsp.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity);
                String[] objectList = result.split("\n");
                for (int i = 0; i < objectList.length; i++) {
                    System.out.println("--" + objectList[i]);
                }
            }
            System.out.println("----------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //获取符合查询条件的对象列表
    //marker           字符串x,返回名称比这个特定的字符串x大的container列表  >=
    //end_marker   字符串x,返回名称比这个特定字符串小的container列表 <
    // 注: 对于md5码 对比的是 第一个字母(经验观察,不是官网内容)
    public void get_container_object_marker_list(String containerName, String marker, String end_marker) {
        try {
            HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName + "/?marker=" + marker + "&end_marker=" + end_marker);

            req.addHeader("X-Auth-Token", X_Auth_Token);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            HttpEntity entity = rsp.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity);
                String[] objectList = result.split("\n");
                for (int i = 0; i < objectList.length; i++) {
                    System.out.println("--" + objectList[i]);
                }
            }
            System.out.println("----------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //创建、更新或拷贝对象
    //curl http://127.0.0.1:8080/v1/AUTH_admin/container1/1.txt  -X PUT -T 1.txt -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36"
    public void create_object(String containerName, String objectName, String filePath) {
        try {
            HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);

            File file = new File(filePath);
            FileEntity entity1 = new FileEntity(file, "text/plain; charset=\"UTF-8\"");
            req.addHeader("Content-Type", "image/jpeg");
            req.addHeader("Content-Type", "text/plain; charset=\"UTF-8\"");
            req.setEntity(entity1);

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

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

    //创建对象及Meta
    //curl http://127.0.0.1:8080/v1/AUTH_admin/container1/1.txt  -X PUT -T 1.txt -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36"
    public void create_object_meta(String containerName, String objectName, String filePath) {
        try {
            HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            req.addHeader("X-Object-Meta-idNum", "123123123");

            File file = new File(filePath);
            FileEntity entity1 = new FileEntity(file, "text/plain; charset=\"UTF-8\"");
            req.addHeader("Content-Type", "image/jpeg");
            req.addHeader("Content-Type", "text/plain; charset=\"UTF-8\"");
            req.setEntity(entity1);

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

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


    //获取对象内容和元数据
    //curl http://127.0.0.1:8080/v1/AUTH_admin/container1/2.txt  -X GET  -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36"
    public void get_container_object(String containerName, String objectName, String path) {
        try {
            HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);
            if (HttpStatus.SC_OK == rsp.getStatusLine().getStatusCode()) {
                HttpEntity entity = rsp.getEntity();
                if (entity != null) {
                    InputStream input = entity.getContent();
                    write(input, path);
                }
            }

            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //判断文件是否存在
    //curl http://127.0.0.1:8080/v1/AUTH_admin/container1/2.txt  -X GET  -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36"
    public boolean exist_container_object(String containerName, String objectName) {
        boolean result = false;
        try {
            HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);
            if (HttpStatus.SC_OK == rsp.getStatusLine().getStatusCode()) {
                HttpEntity entity = rsp.getEntity();
                if (entity != null) {
                    result = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    //删除对象
    // curl http://127.0.0.1:8080/v1/AUTH_admin/container1/2.txt  -X DELETE -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36"
    public void delete_object(String containerName, String objectName) {
        try {
            HttpDelete req = new HttpDelete(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //写文件
    private void write(InputStream is, String path) {
        try {
            OutputStream stream = new BufferedOutputStream(new FileOutputStream(path));
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                stream.write(buffer, 0, len);
            }
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }

    }

    //以文件地址方式向容器中放入数据,数据名称为文件的md5码
    public void create_md5_object(String containerName, String filePath) {
        try {
            String objectName = " ";

            objectName = DigestUtils.md5Hex(new FileInputStream(filePath));
            System.out.println(objectName);

            HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);

            File file = new File(filePath);


            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, file.length());

            req.addHeader("Content-Type", "application/octet-stream");
            req.setEntity(reqEntity);

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

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

    //以文件base64编码方式向容器中放入数据,数据名称为文件的md5码
    public void create_base64_object(String containerName, String base64String) {
        try {
            String objectName = " ";


            objectName = DigestUtils.md5Hex(base64String);
            System.out.println(objectName);

            HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);

            byte[] content = Base64.decodeBase64(base64String);

            ByteArrayEntity reqEntity = new ByteArrayEntity(content);
            req.addHeader("Content-Type", "application/octet-stream");
            req.setEntity(reqEntity);

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

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

    //以文件base64编码方式向容器中放入数据,数据名称为文件的md5码
    public void create_base64_object(String containerName, String base64String, String objectName) {
        try {
            HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);

            byte[] content = Base64.decodeBase64(base64String);

            ByteArrayEntity reqEntity = new ByteArrayEntity(content);
            req.addHeader("Content-Type", "application/octet-stream");
            req.setEntity(reqEntity);

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

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

    //以文件base64编码方式向容器中放入数据,数据名称为文件的md5码
    public void create_base64_object(String containerName, String base64String, String objectName, String metaData) {
        try {
            HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            req.addHeader("X-Object-Meta-idNum", metaData);

            byte[] content = Base64.decodeBase64(base64String);

            ByteArrayEntity reqEntity = new ByteArrayEntity(content);
            req.addHeader("Content-Type", "application/octet-stream");
            req.setEntity(reqEntity);

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

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

    //写入Temp-URL-Key
    public void create_key(String key) {
        try {
            HttpPut req = new HttpPut(X_Storage_Url);
            req.addHeader("X-Auth-Token", X_Auth_Token);
            req.addHeader("Content-Type", "image/jpeg");

            req.addHeader("X-Account-Meta-Temp-Url-Key", key);
            req.addHeader("Content-Type", "text/plain; charset=\"UTF-8\"");


            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse rsp = httpclient.execute(req);

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

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


}
View Code

四、方法调用的主类ResultFulBaseMain.java


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift3.0:CoreData的使用发布时间:2022-07-13
下一篇:
SwiftLint——Swift代码检查及自动格式化工具发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap