华为云对象存储连接测试

华为云
对象存储服务 OBS

//<!--        华为云-->
//<dependency>
//<groupId>com.huaweicloud</groupId>
//<artifactId>esdk-obs-java</artifactId>
//<version>[3.21.4,)</version>
//</dependency>

server

package test;


import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;

@Service
public class HuaweiServer {

    public  ObsClient obsClient;
    @Value("${hw.accessKey}")
    private String accessKey;
    @Value("${hw.secretKey}")
    private String secretKey;
    @Value("${hw.endPoint}")
    private String endPoint;
    @Value("${hw.bucketName}")
    private String bucketName;
//    @Value("${s3.donwloadpath}")
//    private String filepath;

    @PostConstruct
    private void init(){
        obsClient = new ObsClient(accessKey, secretKey, endPoint);
        obsClient.createBucket(bucketName,"cn-south-1");

    }


    public  void upload(String objectKey, String fileparent){
        File list=new File(fileparent);
        for(File file:list.listFiles()){
            if(!file.getName().endsWith(".zip")){
                continue;
            }
            FileInputStream fis;
            try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return;
            }
            System.out.println(">>>> start upload file <<<< time:"+ new Date() + " ,文件名:" + objectKey + " ,文件大小:" + file.length() / (1024 * 1024));
            obsClient.putObject(bucketName, objectKey, fis);
            PutObjectRequest request = new PutObjectRequest(bucketName, objectKey);
            request.setFile(file);
            request.setProgressListener(new ProgressListener() {
                @Override
                public void progressChanged(ProgressStatus status) {
                    // 获取上传平均速率
                    System.out.println("AverageSpeed:" + status.getAverageSpeed());
                    // 获取上传进度百分比
                    System.out.println("TransferPercentage:" + status.getTransferPercentage());
                }
            });
            // 每上传1MB数据反馈上传进度
            request.setProgressInterval(20*1024 * 1024L);
            System.out.println(">>>> end upload file <<<< time:"+ new Date());
            obsClient.putObject(request);

        }

    }


    public void download(String objectKey) {
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(accessKey, secretKey, endPoint);
        DownloadFileRequest request = new DownloadFileRequest(bucketName, objectKey);
// 设置下载对象的本地文件路径
        request.setDownloadFile("localfile");
// 设置分段下载时的最大并发数
        request.setTaskNum(5);
// 设置分段大小为10MB
        request.setPartSize(10 * 1024 * 1024);
// 开启断点续传模式
        request.setEnableCheckpoint(true);

        request.setProgressListener(new ProgressListener() {

            @Override
            public void progressChanged(ProgressStatus status) {
                // 获取下载平均速率
                System.out.println("AverageSpeed:" + status.getAverageSpeed());
                // 获取下载进度百分比
                System.out.println("TransferPercentage:" + status.getTransferPercentage());
            }
        });
// 每下载1MB数据反馈下载进度
        request.setProgressInterval(20*1024 * 1024L);
        try{
            // 进行断点续传下载
            DownloadFileResult result = obsClient.downloadFile(request);
        }catch (ObsException e) {
            // 发生异常时可再次调用断点续传下载接口进行重新下载
        }
    }
}

controller

package test;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@Api(value = "HuaWeiCloud", tags = "华为云测试")
@RestController
@RequestMapping("/test")
public class TestConller {
    @Value("${hw.uploadpath}")
   private String uploadpath;

    @Autowired
    private HuaweiServer huaweiServer;

    @ApiOperation(value = "华为云上传", notes = "")
    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public void upload (@RequestParam String path){
        File file = new File(path);
        String name= file.getName();
        huaweiServer.upload(name,file.getParentFile().getAbsolutePath());
    }

    @ApiOperation(value = "华为云下载", notes = "")
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download (@RequestParam String name){
        huaweiServer.download(name);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值