zoukankan      html  css  js  c++  java
  • 华为云obs存储-入门

    1.购买服务

    2.创建桶:自己设置,加密方式也自己设置

     3.将桶的3个基本参数记住,后台开发要用

    4.在我的凭证里面设置访问密钥,会导出csv文件,将里面的key和keyid拿出来

     5.后台开发

      5.0 主要依赖

        

    <dependency>
        <groupId>com.huaweicloud</groupId>
        <artifactId>esdk-obs-java</artifactId>
        <version>3.19.7</version>
    </dependency>

      5.1 将之前提到的5个参数放在yml里面

        

      5.2 service层

        

    @Service
    public class UploadService {
        @Value("${huawei.accessKeyId}")
        private String accessKeyId;// 华为云的 Access Key Id
        @Value("${huawei.accessKey}")
        private String accessKeySecret;// 华为云的 Access Key Secret
        @Value("${huawei.endpoint}")
        private String endpoint; // 华为云连接的地址节点
        @Value("${huawei.obsBucketName}")
        private String obsBucketName; // 创建的桶的名称
        @Value("${huawei.url}")
        private String url; // 访问OBS文件的url
        private  ObsClient obsClient=null;
        /**
         * @Description: 文件上传
         * @Param: [bucketName, fileName, localFile]
         * @return: com.obs.services.model.PutObjectResult
         * @Author: hyy
         * @Date: 2021/6/25
         */
        public String putLocalFile()  {
            String path="D:\other\demo.jpg";
            File file=new File(path);
            UUID uuid=UUID.randomUUID();
            String originalFileName = uuid.toString().replace("-","")+"_demo.jpg";
            FileInputStream fis=null;
            ObsClient obsClient =null;
            PutObjectResult putObjectResult = null;
            String requestId=null;
            try {
                fis  = new FileInputStream(file);
                obsClient = getObsClient(this.obsClient);
                putObjectResult = obsClient.putObject(obsBucketName, originalFileName, fis);
                requestId = putObjectResult.getRequestId();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    obsClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return  requestId;
        }
        public ObsClient getObsClient(ObsClient obsClient) {
            if(obsClient == null) {
                obsClient = new ObsClient(accessKeyId, accessKeySecret, endpoint);
    
            }
            return obsClient;
        }
    
    }

      5.3 controller层

    @RestController
    public class UpLoadFile {
        @Autowired
        private UploadService uploadService;
        @RequestMapping("/upload")
        public String load(){
            return uploadService.putLocalFile();
        }
    
    }

    6.在云服务器看结果,完美储存

  • 相关阅读:
    poj3252Round Numbers
    poj2282The Counting Problem(组合)
    POJ1150he Last Non-zero Digit(组合)
    poj1715Hexadecimal Numbers(数位dp)
    Codeforces Beta Round #98 (Div. 2)(A-E)
    mysql被收购 用mariadb (转)
    vsftpd配置 (转)
    Linux文件目录结构详解 (转)
    Linux创建ftp并设置权限以及忘记ftp帐号(密码)修改 (转)
    Linux环境Nginx安装、调试以及PHP安装(转)
  • 原文地址:https://www.cnblogs.com/hyy9527/p/14930752.html
Copyright © 2011-2022 走看看