|
@ -12,6 +12,7 @@ import com.aliyun.oss.OSSClient; |
|
|
import com.aliyun.oss.model.DownloadFileRequest; |
|
|
import com.aliyun.oss.model.DownloadFileRequest; |
|
|
import com.aliyun.oss.model.DownloadFileResult; |
|
|
import com.aliyun.oss.model.DownloadFileResult; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
|
|
import com.epmet.constants.PrivacyType; |
|
|
import com.epmet.exception.ModuleErrorCode; |
|
|
import com.epmet.exception.ModuleErrorCode; |
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
import java.io.ByteArrayInputStream; |
|
@ -29,39 +30,57 @@ public class AliyunCloudStorageService extends AbstractCloudStorageService { |
|
|
this.config = config; |
|
|
this.config = config; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @Description 根据隐私类型获取属性。传递internal的时候会返回内部的,否则返回外部的,即公开的 |
|
|
|
|
|
* @return |
|
|
|
|
|
* @author wxz |
|
|
|
|
|
* @date 2021.01.04 13:49 |
|
|
|
|
|
*/ |
|
|
|
|
|
private CloudStorageConfig.AliyunCloudStorageConfigProps getPropsByprivacyType(String privacyType) { |
|
|
|
|
|
if (PrivacyType.INTERNAL.equalsIgnoreCase(privacyType)) { |
|
|
|
|
|
return config.getAliyun().getInternal(); |
|
|
|
|
|
} else { |
|
|
|
|
|
return config.getAliyun().getExternal(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public String upload(byte[] data, String path) { |
|
|
public String upload(byte[] data, String path, String privacyType) { |
|
|
return upload(new ByteArrayInputStream(data), path); |
|
|
return upload(new ByteArrayInputStream(data), path, privacyType); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public String upload(InputStream inputStream, String path) { |
|
|
public String upload(InputStream inputStream, String path, String privacyType) { |
|
|
OSSClient client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(), |
|
|
CloudStorageConfig.AliyunCloudStorageConfigProps props = getPropsByprivacyType(privacyType); |
|
|
config.getAliyunAccessKeySecret()); |
|
|
OSSClient client = new OSSClient(props.getAliyunEndPoint(), props.getAliyunAccessKeyId(), |
|
|
|
|
|
props.getAliyunAccessKeySecret()); |
|
|
try { |
|
|
try { |
|
|
client.putObject(config.getAliyunBucketName(), path, inputStream); |
|
|
client.putObject(props.getAliyunBucketName(), path, inputStream); |
|
|
client.shutdown(); |
|
|
client.shutdown(); |
|
|
} catch (Exception e){ |
|
|
} catch (Exception e){ |
|
|
throw new RenException(ModuleErrorCode.OSS_UPLOAD_FILE_ERROR, e, ""); |
|
|
throw new RenException(ModuleErrorCode.OSS_UPLOAD_FILE_ERROR, e, ""); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return config.getAliyunDomain() + "/" + path; |
|
|
return props.getAliyunDomain() + "/" + path; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public String uploadSuffix(byte[] data, String suffix) { |
|
|
public String uploadSuffix(byte[] data, String suffix, String privacyType) { |
|
|
return upload(data, getPath(config.getAliyunPrefix(), suffix)); |
|
|
CloudStorageConfig.AliyunCloudStorageConfigProps props = getPropsByprivacyType(privacyType); |
|
|
|
|
|
return upload(data, getPath(props.getAliyunPrefix(), suffix), privacyType); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public String uploadSuffix(InputStream inputStream, String suffix) { |
|
|
public String uploadSuffix(InputStream inputStream, String suffix, String privacyType) { |
|
|
return upload(inputStream, getPath(config.getAliyunPrefix(), suffix)); |
|
|
CloudStorageConfig.AliyunCloudStorageConfigProps props = getPropsByprivacyType(privacyType); |
|
|
|
|
|
return upload(inputStream, getPath(props.getAliyunPrefix(), suffix), privacyType); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void down() throws IOException { |
|
|
public void down(String privacyType) throws IOException { |
|
|
OSSClient client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(), |
|
|
CloudStorageConfig.AliyunCloudStorageConfigProps props = getPropsByprivacyType(privacyType); |
|
|
config.getAliyunAccessKeySecret()); |
|
|
OSSClient client = new OSSClient(props.getAliyunEndPoint(), props.getAliyunAccessKeyId(), |
|
|
|
|
|
props.getAliyunAccessKeySecret()); |
|
|
try { |
|
|
try { |
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
@ -88,7 +107,7 @@ public class AliyunCloudStorageService extends AbstractCloudStorageService { |
|
|
}*/ |
|
|
}*/ |
|
|
|
|
|
|
|
|
//方式三
|
|
|
//方式三
|
|
|
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(config.getAliyunBucketName(), "epmet/test/20201103/198f3c11490a44eb964c5c8e9989a507.jpg"); |
|
|
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(props.getAliyunBucketName(), "epmet/test/20201103/198f3c11490a44eb964c5c8e9989a507.jpg"); |
|
|
downloadFileRequest.setDownloadFile("C:/Users/Administrator/Desktop/dd/4.jpg");// 本地下载文件名称
|
|
|
downloadFileRequest.setDownloadFile("C:/Users/Administrator/Desktop/dd/4.jpg");// 本地下载文件名称
|
|
|
downloadFileRequest.setPartSize(1 * 1024 * 1024);// 分片大小,取值范围为1B~5GB。
|
|
|
downloadFileRequest.setPartSize(1 * 1024 * 1024);// 分片大小,取值范围为1B~5GB。
|
|
|
downloadFileRequest.setTaskNum(10);//10个任务并发下载,默认值为1
|
|
|
downloadFileRequest.setTaskNum(10);//10个任务并发下载,默认值为1
|
|
|