Ceph Swift Api 配置与使用(三)(下)

简介: Ceph Swift Api 配置与使用(三)(下)

六、Ceph Swift Api 调用验证


1、创建ceph-demo工程:



image.png


增加SwiftOperator接口:


@Component
public class SwiftOperator {
    /**
     * 用户名信息, 格式: 主用户名:子用户名
     */
    private String username ="cephtester:subtester";
    /**
     * 用户密码
     */
    private String password = "654321";
    /**
     * 接口访问地址
     */
    private String authUrl = "http://192.168.88.161:7480/auth/1.0";
    /**
     * 默认存储的容器名称(bucket)
     */
    private String defaultContainerName = "user_datainfo";
    /**
     * Ceph的账户信息
     */
    private Account account = null;
    /**
     * Ceph的容器信息
     */
    private Container container;
    /**
     * 进行Ceph的初始化配置
     */
    public SwiftOperator() {
        // 1. Ceph的账户信息配置
        AccountConfig config = new AccountConfig();
        config.setUsername(username);
        config.setPassword(password);
        config.setAuthUrl(authUrl);
        config.setAuthenticationMethod(AuthenticationMethod.BASIC);
        account = new AccountFactory(config).createAccount();
        // 2.获取容器信息
        Container newContainer = account.getContainer(defaultContainerName);
        if(!newContainer.exists()) {
            container = newContainer.create();
            System.out.println("container create ==> " + defaultContainerName);
        }else {
            container = newContainer;
        }
    }
    /**
     * 文件上传处理
     * @param remoteName
     * @param filePath
     */
    public void createObject(String remoteName, String filePath) {
        // 1. 从容器当中获取远程存储对象信息
        StoredObject object = container.getObject(remoteName);
        // 2. 执行文件上传处理
        object.uploadObject(new File(filePath));
    }
    /**
     * 文件的下载处理
     * @param objectName
     * @param outPath
     */
    public void retrieveObject(String objectName, String outPath) {
        // 1. 从容器当中获取远程存储对象信息
        StoredObject object = container.getObject(objectName);
        // 2. 执行文件的下载方法
        object.downloadObject(new File(outPath));
    }
    /**
     * 获取用户下面的所有容器信息
     * @return
     */
    public List listContainer() {
        List list = new ArrayList();
        Collection<Container> containers = account.list();
        for(Container container : containers) {
            list.add(container.getName());
            System.out.println("current container name : " + container.getName());
     }
        return list;
    }
}


这里的用户名和密码填写上面我们所生成的信息。注意路径地址后缀为: /auth/1.0


CephDemoApplication启动类,测试验证:


@SpringBootApplication
@ComponentScan(basePackages = {"cn.it"})
public class CephDemoApplication {
    public static void main(String[] args) throws Exception  {
        // Swift Api接口调用验证
        swiftApi();
    }
    /**
     * 通过Swift接口操作ceph集群
     * @throws Exception
     */
    public static void swiftApi() throws Exception  {
        ConfigurableApplicationContext appContext =  SpringApplication.run(CephDemoApplication.class);
        // 1. 先打印出用户的容器信息
        SwiftOperator swiftOperator = appContext.getBean(SwiftOperator.class);
        swiftOperator.listContainer();
        String objName = "test_ceph";
        // 2. 上传指定的文件
        swiftOperator.createObject(objName, "E:\\test\\upload\\test_swift_ceph.txt");
        // 3. 从ceph下载文件到指定的路径下面
        swiftOperator.retrieveObject(objName, "E:\\test\\download\\test_swift_ceph.txt");
        System.out.println("complete");
    }
}


目录
打赏
0
0
0
0
19
分享
相关文章
1688 商品列表 API 深度拆解:从参数配置到数据获取
1688 是重要的批发采购平台,其商品列表 API 接口为开发者、商家和数据分析人员提供批量获取商品基础信息(如名称、价格、销量等)的能力。该接口支持市场调研、竞品分析等场景,助力商业决策与效率提升。接口基于 HTTPS 协议,采用 GET 或 POST 请求方式,需提供通用参数(如 app_key、timestamp 等)和业务参数(如 category_id、page_no 等)。响应数据以 JSON 格式返回,包含商品详情及分页信息。
114 13
|
9月前
|
构建RESTful API已经成为现代Web开发的标准做法之一。Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐。
【10月更文挑战第11天】本文介绍如何使用Spring Boot构建在线图书管理系统的RESTful API。通过创建Spring Boot项目,定义`Book`实体类、`BookRepository`接口和`BookService`服务类,最后实现`BookController`控制器来处理HTTP请求,展示了从基础环境搭建到API测试的完整过程。
134 4
实战阿里qwen2.5-coder 32B,如何配置Cline的Ollama API接口。
阿里Qwen2.5大模型开源免费,适合编程应用。在Ollama平台下载时,推荐选择带有“cline”字样的Qwen2.5-Coder版本,仅需额外下载适配文件,无需重复下载模型文件。Ollama环境永久免费,配置简单,效果出色,适合开发者使用。
4659 77
【03】支付宝支付商户申请下户到配置完整流程-对签约申请已通过商户进行开通API支付-创建应用-申请支付宝公钥-应用公钥-支付宝密钥-配合支付宝官方证书生成工具+配置完整流程-优雅草卓伊凡
【03】支付宝支付商户申请下户到配置完整流程-对签约申请已通过商户进行开通API支付-创建应用-申请支付宝公钥-应用公钥-支付宝密钥-配合支付宝官方证书生成工具+配置完整流程-优雅草卓伊凡
377 0
【03】支付宝支付商户申请下户到配置完整流程-对签约申请已通过商户进行开通API支付-创建应用-申请支付宝公钥-应用公钥-支付宝密钥-配合支付宝官方证书生成工具+配置完整流程-优雅草卓伊凡
|
10月前
|
Ceph Reef(18.2.X)之Swift操作对象存储网关
这篇文章详细介绍了Ceph Reef(18.2.X)中通过Swift API操作对象存储网关的方法,包括创建用户、子用户、配置环境变量、以及使用swift命令行工具进行存储桶和对象的管理。
117 7
Ceph Reef(18.2.X)之Swift操作对象存储网关
【Azure 应用服务】能否通过 Authentication 模块配置 Azure AD 保护 API 应用?
【Azure 应用服务】能否通过 Authentication 模块配置 Azure AD 保护 API 应用?
【Azure API 管理】APIM如何配置客户端证书的CRL检测策略
【Azure API 管理】APIM如何配置客户端证书的CRL检测策略
|
12月前
|
API
zookeeper 使用api 进行节点增删改查及实现简易的配置中心
zookeeper 使用api 进行节点增删改查及实现简易的配置中心
95 2
【Azure API 管理】APIM 配置Validate-JWT策略,验证RS256非对称(公钥/私钥)加密的Token
【Azure API 管理】APIM 配置Validate-JWT策略,验证RS256非对称(公钥/私钥)加密的Token
133 0

相关课程

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问