目 录CONTENT

文章目录

华为云OBS的学习

在水一方
2022-12-17 / 0 评论 / 0 点赞 / 71 阅读 / 2,609 字 / 正在检测是否收录...

前言

OBS:即对象存储服务,基于对象的海量存储云服务,适合存储任意大小和类型的数据

登录OBS方式

1 控制台进入
2 通过客户端(推荐,比较方便)
下载地址: https://support.huaweicloud.com/browsertg-obs/obs_03_1003.html?utm_source=obs_Growth_map&utm_medium=display&utm_campaign=help_center&utm_content=Growth_map
3、obsutil的方式(未了解)

图解OBS

https://support.huaweicloud.com/productdesc-obs/obs_03_0370.html

OBS的API地址

https://obssdk.obs.cn-north-1.myhuaweicloud.com/apidoc/cn/java/index.html

示例程序地址(重要)

https://support.huaweicloud.com/sdk-java-devg-obs/obs_21_0002.html

java中的maven依赖引入


<dependency>
   <groupId>com.huaweicloud</groupId>
   <artifactId>esdk-obs-java-bundle</artifactId>
   <version>[3.21.11,)</version>
</dependency>

开发中编写的示例

下载某个具体文件


    @GetMapping("download")
    @ApiOperation("文件下载测试")
    public void downloadFileByPath(HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition",
 "attachment;filename=" + new String("测试.jpg".getBytes("utf-8"), "iso8859-1"));

        String endPoint = "XXXX";
        String ak = "XXXXXX";
        String sk = "XXXXXX";
        String bucketName = "XXXX";  // 桶的名称
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        // 文件的具体路径(不包含Bucket的路径)
        String objectName = "dev/picture/picture.jpg";  
        ObsObject obsObject = obsClient.getObject(bucketName,objectName);
        InputStream input = obsObject.getObjectContent();
        // 读取对象内容
        byte[] b = new byte[1024];
        OutputStream bos = response.getOutputStream();
        int len;
        while ((len=input.read(b)) != -1){
            bos.write(b, 0, len);
        }

        bos.close();
        input.close();

    }

获取某个文件的临时授权url

  @GetMapping("getPictureUrl")
    @ApiOperation("获取文件临时授权访问的url")
    public ResponseResult<ModelFileUpload> getPictureInfoByIndexId(long taskId,String indexId) throws IOException {
        String obsPath = "dev";
        ObsClient obsClient = new ObsClient(config.getAccessKeyId(), config.getAccessKeySecret(), config.getEndPoint());
        ModelFileUpload upload = iBasicparameterService.getPictureInfoByIndexId(taskId, indexId);
        if(upload !=null) {
            // 设置链接的失效时间为一小时
            TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, 3600000);
            request.setBucketName(config.getBucketName());
            request.setObjectKey(obsPath + upload.getFileName());
            TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
            String url = response.getSignedUrl();
            upload.setUrl(url);

        }
        obsClient.close();
        return ResponseResult.succeed(upload);
    }

上传(比较重要)

这里需要注意上传的几种形式:分为流式上传和文件上传

🔥🔥🔥🔥🔥🔥示例可参考官网:
https://support.huaweicloud.com/sdk-java-devg-obs/obs_21_0602.html

未完待续.........

0

评论区