|
@ -9,10 +9,13 @@ |
|
|
package com.epmet.cloud; |
|
|
package com.epmet.cloud; |
|
|
|
|
|
|
|
|
import com.aliyun.oss.OSSClient; |
|
|
import com.aliyun.oss.OSSClient; |
|
|
|
|
|
import com.aliyun.oss.model.DownloadFileRequest; |
|
|
|
|
|
import com.aliyun.oss.model.DownloadFileResult; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import com.epmet.exception.ModuleErrorCode; |
|
|
import com.epmet.exception.ModuleErrorCode; |
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
import java.io.ByteArrayInputStream; |
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.InputStream; |
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -54,4 +57,55 @@ public class AliyunCloudStorageService extends AbstractCloudStorageService { |
|
|
public String uploadSuffix(InputStream inputStream, String suffix) { |
|
|
public String uploadSuffix(InputStream inputStream, String suffix) { |
|
|
return upload(inputStream, getPath(config.getAliyunPrefix(), suffix)); |
|
|
return upload(inputStream, getPath(config.getAliyunPrefix(), suffix)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void down() throws IOException { |
|
|
|
|
|
OSSClient client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(), |
|
|
|
|
|
config.getAliyunAccessKeySecret()); |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
//方式一
|
|
|
|
|
|
File file = new File("C:/Users/Administrator/Desktop/dd/df.jpg"); |
|
|
|
|
|
client.getObject(new GetObjectRequest(config.getAliyunBucketName(), "epmet/test/20201103/198f3c11490a44eb964c5c8e9989a507.jpg"), file);*/ |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
//方式二
|
|
|
|
|
|
OSSObject oo = client.getObject(config.getAliyunBucketName(), path); |
|
|
|
|
|
InputStream is = oo.getObjectContent(); |
|
|
|
|
|
OutputStream os = null; |
|
|
|
|
|
File file = new File("C:/Users/Administrator/Desktop/dd/aa.jpg"); |
|
|
|
|
|
try { |
|
|
|
|
|
os = new FileOutputStream(file); |
|
|
|
|
|
int len = 0; |
|
|
|
|
|
byte[] buffer = new byte[8192]; |
|
|
|
|
|
while ((len = is.read(buffer)) != -1) { |
|
|
|
|
|
os.write(buffer, 0, len); |
|
|
|
|
|
} |
|
|
|
|
|
} finally { |
|
|
|
|
|
os.close(); |
|
|
|
|
|
is.close(); |
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
|
|
//方式三
|
|
|
|
|
|
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(config.getAliyunBucketName(), "epmet/test/20201103/198f3c11490a44eb964c5c8e9989a507.jpg"); |
|
|
|
|
|
downloadFileRequest.setDownloadFile("C:/Users/Administrator/Desktop/dd/4.jpg");// 本地下载文件名称
|
|
|
|
|
|
downloadFileRequest.setPartSize(1 * 1024 * 1024);// 分片大小,取值范围为1B~5GB。
|
|
|
|
|
|
downloadFileRequest.setTaskNum(10);//10个任务并发下载,默认值为1
|
|
|
|
|
|
downloadFileRequest.setEnableCheckpoint(true);// 启动断点续传
|
|
|
|
|
|
// 下载文件
|
|
|
|
|
|
DownloadFileResult downloadRes = client.downloadFile(downloadFileRequest); |
|
|
|
|
|
// 下载成功时,会返回文件元信息。
|
|
|
|
|
|
downloadRes.getObjectMetadata(); |
|
|
|
|
|
|
|
|
|
|
|
// 关闭OSSClient。
|
|
|
|
|
|
client.shutdown(); |
|
|
|
|
|
} catch (Exception e){ |
|
|
|
|
|
throw new RenException(ModuleErrorCode.OSS_UPLOAD_FILE_ERROR, e, ""); |
|
|
|
|
|
} catch (Throwable throwable) { |
|
|
|
|
|
throwable.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|