华为云主要提供了流式下载、范围下载、断点续传下载三种下载途径
流式下载
public String streamDownload(GetObjectRequest request){
String s = new String();
try{
ObsObject obsObject = obsClient.getObject(request);
// 读取对象内容
System.out.println("Object content:");
InputStream input = obsObject.getObjectContent();
byte[] b = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
while ((len=input.read(b)) != -1){
bos.write(b, 0, len);
}
s = new String(bos.toByteArray());
System.out.println(s);
bos.close();
input.close();
}catch(IOException e){
return "IO exception!";
}catch (ObsException e){
return "OBS exception!";
}
return s;
}
通过流式下载可以范围一个字符串
范围下载
public String rangeDownload(GetObjectRequest request,String localFile,long begin,long end){
// 指定开始和结束范围
request.setRangeStart(begin);
request.setRangeEnd(end);
try{
ObsObject obsObject = obsClient.getObject(request);
OutputStream out = new FileOutputStream(localFile);
InputStream in = obsObject.getObjectContent();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
}catch(IOException e){
return "IO exception!";
}catch (ObsException e){
return "OBS exception!";
}
return localFile;
}
范围下载可以指定下载文件的一部分,如获取一段话中的某一句。
断点续传下载
/* 新建断点续传下载请求 */
public DownloadFileRequest newDownloadRequest(String bucketName,String objectKey){
DownloadFileRequest request = new DownloadFileRequest(bucketName, objectKey);
return request;
}
/* 断点续传下载 */
public String checkPointDownload(DownloadFileRequest request,String localfile,int partSize,int taskNum){
// 设置下载对象的本地文件路径
request.setDownloadFile(localfile);
// 设置分段下载时的最大并发数
request.setTaskNum(taskNum);
// 设置分段大小为10MB
request.setPartSize(partSize);
// 开启断点续传模式
request.setEnableCheckpoint(true);
try{
// 进行断点续传下载
DownloadFileResult result = obsClient.downloadFile(request);
}catch (ObsException e) {
// 发生异常时可再次调用断点续传下载接口进行重新下载
return "download failed";
}
return localfile;
}
断点续传其实是分段下载的特殊情况,可以在网络条件不佳的情况下,如果下载过程被终端,使用断点续传下载可以继续下载,而不是重新下载
其他功能
限定条件下载
//限定条件下载
public GetObjectRequest setIfModifiedSince(GetObjectRequest request, Date date){
request.setIfModifiedSince(date);
return request;
}
public DownloadFileRequest setIfModifiedSince(DownloadFileRequest request, Date date){
request.setIfModifiedSince(date);
return request;
}
public GetObjectRequest setIfUnModifiedSince(GetObjectRequest request, Date date){
request.setIfUnmodifiedSince(date);
return request;
}
public DownloadFileRequest setIfUnModifiedSince(DownloadFileRequest request, Date date){
request.setIfUnmodifiedSince(date);
return request;
}
public GetObjectRequest setIfMatchTag(GetObjectRequest request, String etag){
request.setIfMatchTag(etag);
return request;
}
public DownloadFileRequest setIfMatchTag(DownloadFileRequest request, String etag){
request.setIfMatchTag(etag);
return request;
}
public GetObjectRequest setIfNoneMatchTag(GetObjectRequest request, String etag){
request.setIfNoneMatchTag(etag);
return request;
}
public DownloadFileRequest setIfNoneMatchTag(DownloadFileRequest request, String etag){
request.setIfNoneMatchTag(etag);
return request;
}
这里有四种可以限定的条件,通过调用不同的request来修改request请求的下载条件
重写HTTP/HTTPS响应头信息
//重写HTTP/HTTPS响应头信息
public GetObjectRequest setContentType(GetObjectRequest request,String c){
ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata();
replaceMetadata.setContentType(c);
request.setReplaceMetadata(replaceMetadata);
return request;
}
public GetObjectRequest setContentLanguage(GetObjectRequest request,String c) {
ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata();
replaceMetadata.setContentLanguage(c);
request.setReplaceMetadata(replaceMetadata);
return request;
}
public GetObjectRequest setExpires(GetObjectRequest request,String c){
ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata();
replaceMetadata.setExpires(c);
request.setReplaceMetadata(replaceMetadata);
return request;
}
public GetObjectRequest setCacheControl(GetObjectRequest request,String c){
ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata();
replaceMetadata.setCacheControl(c);
request.setReplaceMetadata(replaceMetadata);
return request;
}
public GetObjectRequest setContentDisposition(GetObjectRequest request,String c){
ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata();
replaceMetadata.setContentDisposition(c);
request.setReplaceMetadata(replaceMetadata);
return request;
}
public GetObjectRequest setContentEncoding(GetObjectRequest request,String c){
ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata();
replaceMetadata.setContentEncoding(c);
request.setReplaceMetadata(replaceMetadata);
return request;
}
取回归档存储物品
归档存储的对象需要先取回,再进行下载
取回有两种方式,快速取回需要三到五分钟,普通取回则需要几个小时
/* 取回归档存储对象 */
public String getRestoreObject(String bucketName,String objectKey,int restoreTierEnum,int time){
try {
RestoreObjectRequest request = new RestoreObjectRequest();
request.setBucketName(bucketName);
request.setObjectKey(objectKey);
request.setDays(time);
if (restoreTierEnum == 0) {
request.setRestoreTier(RestoreTierEnum.EXPEDITED);
}
if (restoreTierEnum == 1) {
request.setRestoreTier(RestoreTierEnum.STANDARD);
}
obsClient.restoreObject(request);
}catch (ObsException e){
return "restore failed";
}
return "success";
}