华为云
对象存储服务 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);
}
}