文件上传obs服务器

obs工具类:


import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.HeaderResponse;
import com.ruoyi.common.utils.DictUtils;

import java.io.File;

/**
 * 华为obs对象存储api
 */
public class ObsTemplate {
    public static String endPoint = "https://your-endpoint";
    public static String ak = "*** Provide your Access Key ***";
    public static String sk = "*** Provide your Secret Key ***";
    public static String bucketName="";
    public static ObsTemplate obsTemplate;
    public static ObsClient obsClient =  null;// 创建ObsClient实例

    public static ObsTemplate getInstance()
    {
        if(obsTemplate == null)
        {
            init();
            obsTemplate = new ObsTemplate();
        }
        return obsTemplate;
    }

    public static void init(){
        endPoint= DictUtils.getDictLabel("obs","endpoint");
        ak=DictUtils.getDictLabel("obs","accesskey");
        sk=DictUtils.getDictLabel("obs","secretKey");
        bucketName=DictUtils.getDictLabel("obs","bucketName");
        obsClient =  new ObsClient(ak, sk, endPoint);// 创建ObsClient实例
    }

    public String putObs(String filePath,String objectName){
        // 您的工程中可以只保留一个全局的ObsClient实例
        // ObsClient是线程安全的,可在并发场景下使用
        try
        {
            // 调用接口进行操作,例如上传对象
            HeaderResponse response = obsClient.putObject(bucketName, objectName,
                    new File(filePath));  // localfile为待上传的本地文件路径,需要指定到具体的文件名
            System.out.println(response);
            return null;
        }
        catch (ObsException e)
        {
            System.out.println("HTTP Code: " + e.getResponseCode());
            System.out.println("Error Code:" + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());

            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            return e.getMessage();
        }
    }
}

上传到obs:

 String result= ObsTemplate.getInstance().putObs('源文件地址','上传到obs的地址');

相关推荐

  1. 文件obs服务器

    2024-07-18 16:42:03       23 阅读
  2. 【华为云】SpringBoot + OBS 文件

    2024-07-18 16:42:03       58 阅读
  3. 华为云OBS-文件

    2024-07-18 16:42:03       51 阅读
  4. 如何在服务器/下载文件

    2024-07-18 16:42:03       30 阅读
  5. golang文件到ftp服务器

    2024-07-18 16:42:03       38 阅读
  6. 文件给Ubuntu服务器

    2024-07-18 16:42:03       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 16:42:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 16:42:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 16:42:03       57 阅读
  4. Python语言-面向对象

    2024-07-18 16:42:03       68 阅读

热门阅读

  1. C++运算符重载(+)

    2024-07-18 16:42:03       21 阅读
  2. 使用Dockerfile构建镜像

    2024-07-18 16:42:03       20 阅读
  3. python开发基础——day14 模块与包

    2024-07-18 16:42:03       23 阅读
  4. 【国内当前可用pip&conda源刷新】

    2024-07-18 16:42:03       19 阅读
  5. 解决浏览器缓存导致获取不到最新前端代码问题

    2024-07-18 16:42:03       21 阅读
  6. 6Python的Pandas:数据读取与输出

    2024-07-18 16:42:03       22 阅读
  7. linux修改时区为CST

    2024-07-18 16:42:03       17 阅读
  8. 请求通过Spring Cloud Gateway 503

    2024-07-18 16:42:03       18 阅读
  9. 使用小皮面版的Nginx服务搭建本地服务器

    2024-07-18 16:42:03       21 阅读
  10. Jenkins 安装、部署与配置

    2024-07-18 16:42:03       23 阅读