|
@@ -11,6 +11,8 @@ import com.aliyun.oss.model.*;
|
|
|
import com.amazonaws.HttpMethod;
|
|
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
|
|
import com.amazonaws.auth.BasicAWSCredentials;
|
|
|
+import com.amazonaws.client.builder.AwsClientBuilder;
|
|
|
+import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
|
|
|
import com.amazonaws.regions.Regions;
|
|
|
import com.amazonaws.services.s3.AmazonS3;
|
|
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
|
@@ -97,30 +99,30 @@ public class UploadToOssUtil {
|
|
|
private static final String UPLOAD_SH = "bash /opt/ossutil/upload.sh %s %s";
|
|
|
|
|
|
//上传的数据是byte[],key是上传后的文件名
|
|
|
- public void upload(byte[] data,String key1) throws IOException{
|
|
|
+ public void upload(String bucket, byte[] data,String key1) throws IOException{
|
|
|
log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , new String(data, "UTF-8"),key1,type);
|
|
|
StorageType storageType = StorageType.get(type);
|
|
|
switch (storageType){
|
|
|
case OSS:
|
|
|
- uploadOss(data,key1);
|
|
|
+ uploadOss(bucket, data,key1);
|
|
|
break;
|
|
|
case AWS:
|
|
|
- uploadAws(data,key1);
|
|
|
+ uploadAws(bucket, data,key1);
|
|
|
break;
|
|
|
case LOCAL:
|
|
|
uploadLocal(data,key1);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- public void upload(String filePath, String key1) {
|
|
|
+ public void upload(String bucket, String filePath, String key1) {
|
|
|
log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
|
|
|
StorageType storageType = StorageType.get(type);
|
|
|
switch (storageType){
|
|
|
case OSS:
|
|
|
- uploadOss(filePath,key1);
|
|
|
+ uploadOss(bucket, filePath,key1);
|
|
|
break;
|
|
|
case AWS:
|
|
|
- uploadAws(filePath,key1);
|
|
|
+ uploadAws(bucket, filePath,key1);
|
|
|
break;
|
|
|
case LOCAL:
|
|
|
uploadLocal(filePath,key1);
|
|
@@ -133,8 +135,8 @@ public class UploadToOssUtil {
|
|
|
* @param filePath
|
|
|
* @param key
|
|
|
*/
|
|
|
- public void uploadBySh(String filePath, String key) {
|
|
|
- String ossPath = this.bucket + "/" + key;
|
|
|
+ public void uploadBySh(String bucket, String filePath, String key) {
|
|
|
+ String ossPath = bucket + "/" + key;
|
|
|
try {
|
|
|
String command = String.format(UploadToOssUtil.UPLOAD_SH, ossPath, filePath);
|
|
|
log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
|
|
@@ -146,26 +148,26 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void uploadSdk(String filePath, String key1) {
|
|
|
+ public void uploadSdk(String bucket, String filePath, String key1) {
|
|
|
log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
|
|
|
switch (type){
|
|
|
- case "oss":uploadSdkOss(filePath,key1); break;
|
|
|
- case "aws": uploadAws(filePath,key1); break;
|
|
|
+ case "oss":uploadSdkOss(bucket, filePath, key1); break;
|
|
|
+ case "aws": uploadAws(bucket, filePath, key1); break;
|
|
|
case "local":uploadLocal(filePath,key1); break;
|
|
|
}
|
|
|
}
|
|
|
- public void upload2(String filePath, String key1) {
|
|
|
+ public void upload2(String bucket, String filePath, String key1) {
|
|
|
log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
|
|
|
switch (type){
|
|
|
- case "oss":upload2Oss(filePath,key1); break;
|
|
|
- case "aws": uploadAws(filePath,key1); break;
|
|
|
+ case "oss":upload2Oss(bucket, filePath,key1); break;
|
|
|
+ case "aws": uploadAws(bucket, filePath,key1); break;
|
|
|
case "local":uploadLocal(filePath,key1); break;
|
|
|
}
|
|
|
}
|
|
|
- public void delete(String key1) throws IOException{
|
|
|
+ public void delete(String bucket, String key1) throws IOException{
|
|
|
switch (type){
|
|
|
- case "oss":deleteOss(key1); break;
|
|
|
- case "aws": deleteS3Object(key1); break;
|
|
|
+ case "oss":deleteOss(bucket, key1); break;
|
|
|
+ case "aws": deleteS3Object(bucket, key1); break;
|
|
|
case "local":FileUtil.del(key1); break;
|
|
|
}
|
|
|
}
|
|
@@ -175,16 +177,16 @@ public class UploadToOssUtil {
|
|
|
* @param prefix
|
|
|
* @return
|
|
|
*/
|
|
|
- public int deleteFile(String prefix){
|
|
|
+ public int deleteFile(String bucket, String prefix){
|
|
|
switch (type){
|
|
|
- case "oss":deleteOssFile(prefix); break;
|
|
|
- case "aws": deleteAwsFile(prefix); break;
|
|
|
+ case "oss":deleteOssFile(bucket, prefix); break;
|
|
|
+ case "aws": deleteAwsFile(bucket, prefix); break;
|
|
|
case "local":FileUtil.del(prefix); break;
|
|
|
}
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
- public void deleteOss(String objectName){
|
|
|
+ public void deleteOss(String bucket, String objectName){
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
ossClient.deleteObject(bucket, objectName);
|
|
@@ -197,7 +199,7 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void deleteOssFile(String prefix){
|
|
|
+ public void deleteOssFile(String bucket, String prefix){
|
|
|
int maxKeys = 200;
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
@@ -206,7 +208,7 @@ public class UploadToOssUtil {
|
|
|
ObjectListing objectListing;
|
|
|
|
|
|
do {
|
|
|
- objectListing = ossClient.listObjects(new ListObjectsRequest(this.bucket).withPrefix(prefix).withMarker(nextMarker).withMaxKeys(maxKeys));
|
|
|
+ objectListing = ossClient.listObjects(new ListObjectsRequest(bucket).withPrefix(prefix).withMarker(nextMarker).withMaxKeys(maxKeys));
|
|
|
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
|
if (CollUtil.isEmpty(sums)) {
|
|
|
return;
|
|
@@ -228,23 +230,6 @@ public class UploadToOssUtil {
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
-
|
|
|
-// ListUtil.page(keys, 100, subKeys -> {
|
|
|
-// DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket)
|
|
|
-// .withKeys(
|
|
|
-// subKeys).withEncodingType("url");
|
|
|
-// DeleteObjectsResult deleteObjectsResult = ossClient
|
|
|
-// .deleteObjects(deleteObjectsRequest);
|
|
|
-// List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
|
|
|
-// try {
|
|
|
-// for (String deletedObject : deletedObjects) {
|
|
|
-// String decode = URLDecoder.decode(deletedObject, "UTF-8");
|
|
|
-// log.info("删除oss文件:{}", decode);
|
|
|
-// }
|
|
|
-// } catch (UnsupportedEncodingException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-// });
|
|
|
}while (objectListing.isTruncated());
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
@@ -255,7 +240,7 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void deleteAwsFile(String prefix){
|
|
|
+ private void deleteAwsFile(String bucket, String prefix){
|
|
|
|
|
|
int maxKeys = 200;
|
|
|
String nextMaker = null;
|
|
@@ -266,7 +251,7 @@ public class UploadToOssUtil {
|
|
|
.build();
|
|
|
try {
|
|
|
com.amazonaws.services.s3.model.ListObjectsRequest listObjectsRequest = new com.amazonaws.services.s3.model.ListObjectsRequest();
|
|
|
- listObjectsRequest.setBucketName(this.bucket);
|
|
|
+ listObjectsRequest.setBucketName(bucket);
|
|
|
listObjectsRequest.setPrefix(prefix);
|
|
|
listObjectsRequest.setMaxKeys(maxKeys);
|
|
|
|
|
@@ -278,7 +263,7 @@ public class UploadToOssUtil {
|
|
|
List<KeyVersion> keys =objectSummaries.stream().map(summary->new KeyVersion(summary.getKey())).collect(Collectors.toList());
|
|
|
|
|
|
com.amazonaws.services.s3.model.DeleteObjectsRequest multiObjectDeleteRequest =
|
|
|
- new com.amazonaws.services.s3.model.DeleteObjectsRequest(this.bucket)
|
|
|
+ new com.amazonaws.services.s3.model.DeleteObjectsRequest(bucket)
|
|
|
.withKeys(keys)
|
|
|
.withQuiet(false);
|
|
|
com.amazonaws.services.s3.model.DeleteObjectsResult delObjRes = s3.deleteObjects(multiObjectDeleteRequest);
|
|
@@ -295,7 +280,7 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void uploadOss(byte[] data,String objectName){
|
|
|
+ public void uploadOss(String bucket, byte[] data,String objectName){
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
ossClient.putObject(bucket, objectName, new ByteArrayInputStream(data));
|
|
@@ -307,7 +292,7 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- public void uploadAws(byte[] data,String objectName){
|
|
|
+ public void uploadAws(String bucket, byte[] data,String objectName){
|
|
|
BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
|
|
|
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
|
|
@@ -315,7 +300,9 @@ public class UploadToOssUtil {
|
|
|
.build();
|
|
|
try {
|
|
|
com.amazonaws.services.s3.model.ObjectMetadata metadata = new com.amazonaws.services.s3.model.ObjectMetadata();
|
|
|
- s3.putObject(this.bucket, objectName, new ByteArrayInputStream(data), metadata);
|
|
|
+ PutObjectRequest request = new PutObjectRequest(bucket, objectName, new ByteArrayInputStream(data), metadata);
|
|
|
+ request.withCannedAcl(CannedAccessControlList.PublicRead);
|
|
|
+ s3.putObject(request);
|
|
|
}catch (Exception e){
|
|
|
log.error("s3上传文件失败", e);
|
|
|
}finally {
|
|
@@ -354,7 +341,7 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void uploadOss(String filePath, String key1){
|
|
|
+ public void uploadOss(String bucket, String filePath, String key1){
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
File file = new File(filePath);
|
|
@@ -375,9 +362,9 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void uploadAws(String filePath, String key1){
|
|
|
+ public void uploadAws(String bucket, String filePath, String key1){
|
|
|
try{
|
|
|
- uploadS3File(filePath, key1);
|
|
|
+ uploadS3File(bucket, filePath, key1);
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -393,7 +380,7 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void uploadSdkOss(String filePath, String key1){
|
|
|
+ public void uploadSdkOss(String bucket, String filePath, String key1){
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
File file = new File(filePath);
|
|
@@ -405,14 +392,14 @@ public class UploadToOssUtil {
|
|
|
if(filePath.contains(".jpg")){
|
|
|
metadata.setContentType("image/jpeg");
|
|
|
}
|
|
|
- ossClient.putObject(bucketSdk, key1, new File(filePath), metadata);
|
|
|
+ ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
log.error(e.toString() + filePath);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void upload2Oss(String filePath, String key1){
|
|
|
+ public void upload2Oss(String bucket, String filePath, String key1){
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
@@ -432,7 +419,7 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
|
|
|
//上传的数据是文件夹,参数是文件夹路径,key是上传后的文件名
|
|
|
- public void uploadMulFiles(Map<String, String> filepaths) {
|
|
|
+ public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
|
|
|
if (filepaths == null) {
|
|
|
return;
|
|
|
}
|
|
@@ -440,11 +427,11 @@ public class UploadToOssUtil {
|
|
|
log.info("开始批量上传文件:");
|
|
|
if (filepaths.size() > 50) {
|
|
|
filepaths.entrySet().parallelStream().forEach(entry->{
|
|
|
- upload2(entry.getKey(), entry.getValue());
|
|
|
+ upload2(bucket, entry.getKey(), entry.getValue());
|
|
|
});
|
|
|
} else {
|
|
|
filepaths.entrySet().parallelStream().forEach(entry->{
|
|
|
- upload(entry.getKey(), entry.getValue());
|
|
|
+ upload(bucket, entry.getKey(), entry.getValue());
|
|
|
});
|
|
|
}
|
|
|
log.info("批量上传文件结束,用时:{}" ,(System.currentTimeMillis() - start));
|
|
@@ -527,7 +514,7 @@ public class UploadToOssUtil {
|
|
|
* @param file 文件
|
|
|
* @param updatePath 上传路径[ eg: xxx/xxx ]
|
|
|
*/
|
|
|
- public String updateS3LoadFile(MultipartFile file, String updatePath) {
|
|
|
+ public String updateS3LoadFile(String bucket, MultipartFile file, String updatePath) {
|
|
|
|
|
|
if (isEmpty(file)) {
|
|
|
return null;
|
|
@@ -552,7 +539,7 @@ public class UploadToOssUtil {
|
|
|
String key = updatePath;
|
|
|
|
|
|
// 设置文件并设置公读
|
|
|
- PutObjectRequest request = new PutObjectRequest(s3bucket, key, localFile);
|
|
|
+ PutObjectRequest request = new PutObjectRequest(bucket, key, localFile);
|
|
|
request.withCannedAcl(CannedAccessControlList.PublicRead);
|
|
|
|
|
|
// 上传文件
|
|
@@ -574,7 +561,7 @@ public class UploadToOssUtil {
|
|
|
* @param key1
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- private void uploadS3File(String filePath, String key1) throws Exception {
|
|
|
+ private void uploadS3File(String bucket, String filePath, String key1) throws Exception {
|
|
|
/**
|
|
|
* 创建s3对象
|
|
|
*/
|
|
@@ -598,7 +585,7 @@ public class UploadToOssUtil {
|
|
|
if(filePath.contains(".png")){
|
|
|
metadata.setContentType("image/png");
|
|
|
}
|
|
|
- PutObjectRequest request = new PutObjectRequest(s3bucket, key1, file);
|
|
|
+ PutObjectRequest request = new PutObjectRequest(bucket, key1, file);
|
|
|
request.withCannedAcl(CannedAccessControlList.PublicRead);
|
|
|
request.withMetadata(metadata);
|
|
|
|
|
@@ -621,7 +608,7 @@ public class UploadToOssUtil {
|
|
|
* @param objectName 文件路径[ eg: /head/xxxx.jpg ]
|
|
|
* @return
|
|
|
*/
|
|
|
- public void deleteS3Object(String objectName) {
|
|
|
+ public void deleteS3Object(String bucket, String objectName) {
|
|
|
|
|
|
/**
|
|
|
* 创建s3对象
|
|
@@ -637,7 +624,7 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- s3.deleteObject(s3bucket, objectName);
|
|
|
+ s3.deleteObject(bucket, objectName);
|
|
|
} catch (Exception e) {
|
|
|
log.error("s3删除文件失败,key="+objectName, e);
|
|
|
}finally {
|
|
@@ -708,13 +695,13 @@ public class UploadToOssUtil {
|
|
|
return item;
|
|
|
}
|
|
|
|
|
|
- public List<String> listKeys(String sourcePath){
|
|
|
+ public List<String> listKeys(String bucket, String sourcePath){
|
|
|
StorageType storageType = StorageType.get(type);
|
|
|
switch (storageType){
|
|
|
case OSS:
|
|
|
- return this.listKeysFromAli(sourcePath);
|
|
|
+ return this.listKeysFromAli(bucket, sourcePath);
|
|
|
case AWS:
|
|
|
- return this.listKeysFromAws(sourcePath);
|
|
|
+ return this.listKeysFromAws(bucket, sourcePath);
|
|
|
case LOCAL:
|
|
|
return this.listKeysFromLocal(sourcePath);
|
|
|
}
|
|
@@ -725,13 +712,13 @@ public class UploadToOssUtil {
|
|
|
* 获得文件列表-阿里云
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<String> listKeysFromAli(String sourcePath) {
|
|
|
+ public List<String> listKeysFromAli(String bucket, String sourcePath) {
|
|
|
List<String> keyList = new ArrayList<>();
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
boolean flag = true;
|
|
|
String nextMaker = null;
|
|
|
- ListObjectsRequest listObjectsRequest = new ListObjectsRequest(this.bucket);
|
|
|
+ ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
|
|
|
//指定下一级文件
|
|
|
listObjectsRequest.setPrefix(sourcePath);
|
|
|
//设置分页的页容量
|
|
@@ -770,7 +757,7 @@ public class UploadToOssUtil {
|
|
|
* 获得文件列表-亚马逊
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<String> listKeysFromAws(String sourcePath) {
|
|
|
+ public List<String> listKeysFromAws(String bucket, String sourcePath) {
|
|
|
List<String> keyList = new ArrayList<>();
|
|
|
|
|
|
BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
@@ -783,7 +770,7 @@ public class UploadToOssUtil {
|
|
|
boolean flag = true;
|
|
|
String nextMaker = null;
|
|
|
com.amazonaws.services.s3.model.ListObjectsRequest listObjectsRequest = new com.amazonaws.services.s3.model.ListObjectsRequest();
|
|
|
- listObjectsRequest.setBucketName(this.bucket);
|
|
|
+ listObjectsRequest.setBucketName(bucket);
|
|
|
listObjectsRequest.setPrefix(sourcePath);
|
|
|
listObjectsRequest.setMaxKeys(200);
|
|
|
|
|
@@ -828,14 +815,14 @@ public class UploadToOssUtil {
|
|
|
* @param sourcePath
|
|
|
* @param targetPath
|
|
|
**/
|
|
|
- public void copyFiles(String sourcePath, String targetPath) throws IOException {
|
|
|
+ public void copyFiles(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) throws IOException {
|
|
|
StorageType storageType = StorageType.get(type);
|
|
|
switch (storageType){
|
|
|
case OSS:
|
|
|
- this.copyFilesFromAli(sourcePath, targetPath);
|
|
|
+ this.copyFilesFromAli(sourceBucketName, sourcePath, targetBucketName, targetPath);
|
|
|
break;
|
|
|
case AWS:
|
|
|
- this.copyFilesFromAws(sourcePath, targetPath);
|
|
|
+ this.copyFilesFromAws(sourceBucketName, sourcePath, targetBucketName, targetPath);
|
|
|
break;
|
|
|
case LOCAL: this.copyFilesFromLocal(sourcePath, targetPath);
|
|
|
}
|
|
@@ -850,14 +837,14 @@ public class UploadToOssUtil {
|
|
|
* @param sourceKey
|
|
|
* @param targetKey
|
|
|
**/
|
|
|
- public void copyObject(String sourceKey, String targetKey) throws IOException {
|
|
|
+ public void copyObject(String sourceBucketName, String sourceKey, String targetBucketName, String targetKey) throws IOException {
|
|
|
StorageType storageType = StorageType.get(type);
|
|
|
switch (storageType){
|
|
|
case OSS:
|
|
|
- this.copyObjectFromAli(sourceKey, targetKey);
|
|
|
+ this.copyObjectFromAli(sourceBucketName, sourceKey, targetBucketName, targetKey);
|
|
|
break;
|
|
|
case AWS:
|
|
|
- this.copyObjectFromAws(sourceKey, targetKey);
|
|
|
+ this.copyObjectFromAws(sourceBucketName, sourceKey, targetBucketName, targetKey);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -871,13 +858,13 @@ public class UploadToOssUtil {
|
|
|
* @param sourcePath
|
|
|
* @param targetPath
|
|
|
**/
|
|
|
- public void copyObjectFromAli(String sourcePath, String targetPath) throws IOException {
|
|
|
+ public void copyObjectFromAli(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) throws IOException {
|
|
|
|
|
|
// 创建OSSClient实例。
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
// 复制文件
|
|
|
log.info("开始复制:" + sourcePath);
|
|
|
- ossClient.copyObject(this.bucket, sourcePath, this.bucket, targetPath);
|
|
|
+ ossClient.copyObject(sourceBucketName, sourcePath, targetBucketName, targetPath);
|
|
|
log.info("复制成功:" + sourcePath);
|
|
|
ossClient.shutdown();
|
|
|
}
|
|
@@ -893,10 +880,10 @@ public class UploadToOssUtil {
|
|
|
* @param sourcePath
|
|
|
* @param targetPath
|
|
|
**/
|
|
|
- public void copyFilesFromAli(String sourcePath, String targetPath) throws IOException {
|
|
|
+ public void copyFilesFromAli(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) throws IOException {
|
|
|
|
|
|
//获取源文件列表
|
|
|
- List<String> sourceKeyList = this.listKeysFromAli(sourcePath);
|
|
|
+ List<String> sourceKeyList = this.listKeysFromAli(sourceBucketName, sourcePath);
|
|
|
if(CollUtil.isEmpty(sourceKeyList)){
|
|
|
return;
|
|
|
}
|
|
@@ -905,7 +892,7 @@ public class UploadToOssUtil {
|
|
|
// 复制文件
|
|
|
sourceKeyList.parallelStream().forEach(key -> {
|
|
|
log.info("开始复制:" + key);
|
|
|
- ossClient.copyObject(this.bucket, key, this.bucket, key.replace(sourcePath, targetPath));
|
|
|
+ ossClient.copyObject(sourceBucketName, key, targetBucketName, key.replace(sourcePath, targetPath));
|
|
|
log.info("复制成功:" + key);
|
|
|
});
|
|
|
|
|
@@ -922,10 +909,10 @@ public class UploadToOssUtil {
|
|
|
* @param sourcePath
|
|
|
* @param targetPath
|
|
|
**/
|
|
|
- public void copyFilesFromAws(String sourcePath, String targetPath){
|
|
|
+ public void copyFilesFromAws(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath){
|
|
|
|
|
|
try {
|
|
|
- List<String> sourceKeyList = this.listKeysFromAws(sourcePath);
|
|
|
+ List<String> sourceKeyList = this.listKeysFromAws(sourceBucketName, sourcePath);
|
|
|
/**
|
|
|
* 创建s3对象
|
|
|
*/
|
|
@@ -938,7 +925,10 @@ public class UploadToOssUtil {
|
|
|
// 复制文件
|
|
|
sourceKeyList.parallelStream().forEach(key -> {
|
|
|
log.info("开始复制:" + key);
|
|
|
- s3.copyObject(this.bucket, key, this.bucket, key.replace(sourcePath, targetPath));
|
|
|
+ com.amazonaws.services.s3.model.CopyObjectRequest request =
|
|
|
+ new com.amazonaws.services.s3.model.CopyObjectRequest(sourceBucketName, key, targetBucketName, key.replace(sourcePath, targetPath));
|
|
|
+ request.withCannedAccessControlList(CannedAccessControlList.PublicRead);
|
|
|
+ s3.copyObject(request);
|
|
|
log.info("复制成功:" + key);
|
|
|
});
|
|
|
s3.shutdown();
|
|
@@ -956,7 +946,7 @@ public class UploadToOssUtil {
|
|
|
* @param sourceKey
|
|
|
* @param targetKey
|
|
|
**/
|
|
|
- public void copyObjectFromAws(String sourceKey, String targetKey){
|
|
|
+ public void copyObjectFromAws(String sourceBucketName, String sourceKey, String targetBucketName, String targetKey){
|
|
|
BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
|
|
|
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
|
|
@@ -964,7 +954,7 @@ public class UploadToOssUtil {
|
|
|
.build();
|
|
|
// 复制文件
|
|
|
log.info("开始复制:" + sourceKey);
|
|
|
- s3.copyObject(this.bucket, sourceKey, this.bucket, targetKey);
|
|
|
+ s3.copyObject(sourceBucketName, sourceKey, targetBucketName, targetKey);
|
|
|
log.info("复制成功:" + sourceKey);
|
|
|
s3.shutdown();
|
|
|
}
|
|
@@ -1042,7 +1032,7 @@ public class UploadToOssUtil {
|
|
|
* @param objectName
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean existOnAli(String objectName){
|
|
|
+ public boolean existOnAli(String bucket, String objectName){
|
|
|
//创建oss客户端
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
@@ -1103,7 +1093,7 @@ public class UploadToOssUtil {
|
|
|
* @param objectName
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean existOnAws(String objectName){
|
|
|
+ public boolean existOnAws(String bucket, String objectName){
|
|
|
|
|
|
BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
|
|
@@ -1128,13 +1118,13 @@ public class UploadToOssUtil {
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean existKey(String key){
|
|
|
+ public boolean existKey(String bucket, String key){
|
|
|
StorageType storageType = StorageType.get(type);
|
|
|
switch (storageType){
|
|
|
case OSS:
|
|
|
- return this.existOnAli(key);
|
|
|
+ return this.existOnAli(bucket, key);
|
|
|
case AWS:
|
|
|
- return this.existOnAws(key);
|
|
|
+ return this.existOnAws(bucket, key);
|
|
|
default:
|
|
|
return false;
|
|
|
}
|
|
@@ -1155,13 +1145,13 @@ public class UploadToOssUtil {
|
|
|
* @param objectName
|
|
|
* @param localPath
|
|
|
*/
|
|
|
- public boolean download(String objectName, String localPath){
|
|
|
+ public boolean download(String bucket, String objectName, String localPath){
|
|
|
StorageType storageType = StorageType.get(this.type);
|
|
|
switch (storageType){
|
|
|
case OSS:
|
|
|
- return this.downFormAli(objectName, localPath);
|
|
|
+ return this.downFormAli(bucket, objectName, localPath);
|
|
|
case AWS:
|
|
|
- return this.downFromS3(objectName, localPath);
|
|
|
+ return this.downFromS3(bucket, objectName, localPath);
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
@@ -1172,7 +1162,7 @@ public class UploadToOssUtil {
|
|
|
* @param localPath 本地文件地址
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean downFormAli(String objectName, String localPath){
|
|
|
+ public boolean downFormAli(String bucket, String objectName, String localPath){
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
com.aliyun.oss.model.GetObjectRequest request = new com.aliyun.oss.model.GetObjectRequest(bucket,objectName);
|
|
@@ -1194,14 +1184,14 @@ public class UploadToOssUtil {
|
|
|
* @param localPath 本地文件地址
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean downFromS3(String objectName, String localPath) {
|
|
|
+ public boolean downFromS3(String bucket, String objectName, String localPath) {
|
|
|
BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
|
|
|
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
|
|
|
.withRegion(Regions.EU_WEST_2)
|
|
|
.build();
|
|
|
try {
|
|
|
- GetObjectRequest request = new GetObjectRequest(this.bucket,objectName);
|
|
|
+ GetObjectRequest request = new GetObjectRequest(bucket,objectName);
|
|
|
s3.getObject(request,new File(localPath));
|
|
|
return true;
|
|
|
} catch (Exception e) {
|