|
|
@@ -1,22 +1,21 @@
|
|
|
package cn.reghao.devops.mgr.aliyun.service;
|
|
|
|
|
|
-import cn.reghao.devops.mgr.aliyun.db.repository.OssConfigRepository;
|
|
|
-import cn.reghao.devops.mgr.aliyun.model.po.OssConfig;
|
|
|
+import cn.reghao.devops.mgr.aliyun.db.repository.AliyunAccountRepository;
|
|
|
+import cn.reghao.devops.mgr.aliyun.model.po.AliyunAccount;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
import com.aliyun.oss.model.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.io.*;
|
|
|
-import java.net.URL;
|
|
|
import java.nio.file.FileVisitResult;
|
|
|
import java.nio.file.FileVisitor;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
@@ -24,27 +23,22 @@ import java.util.List;
|
|
|
* @date 2024-08-30 16:13:38
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-@Service
|
|
|
+@Component
|
|
|
public class AliyunOss {
|
|
|
private OSS ossClient;
|
|
|
- private String bucketName;
|
|
|
- private final OssConfigRepository ossConfigRepository;
|
|
|
+ private final AliyunAccountRepository aliyunAccountRepository;
|
|
|
|
|
|
- public AliyunOss(OssConfigRepository ossConfigRepository) {
|
|
|
- this.ossConfigRepository = ossConfigRepository;
|
|
|
+ public AliyunOss(AliyunAccountRepository aliyunAccountRepository) {
|
|
|
+ this.aliyunAccountRepository = aliyunAccountRepository;
|
|
|
}
|
|
|
|
|
|
- private OSS getOssClient() {
|
|
|
- if (ossClient == null) {
|
|
|
- OssConfig ossConfig = ossConfigRepository.findAll().get(0);
|
|
|
- String endpoint = ossConfig.getEndpoint();
|
|
|
- String accessKeyId = ossConfig.getAccessKeyId();
|
|
|
- String accessKeySecret = ossConfig.getAccessKeySecret();
|
|
|
- this.bucketName = ossConfig.getBucketName();
|
|
|
- this.ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
- }
|
|
|
-
|
|
|
- return ossClient;
|
|
|
+ @PostConstruct
|
|
|
+ public void initOssClient() {
|
|
|
+ AliyunAccount aliyunAccount = aliyunAccountRepository.findByType("oss");
|
|
|
+ String endpoint = aliyunAccount.getEndpoint();
|
|
|
+ String accessKeyId = aliyunAccount.getAccessKeyId();
|
|
|
+ String accessKeySecret = aliyunAccount.getAccessKeySecret();
|
|
|
+ this.ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
}
|
|
|
|
|
|
public void uploadObject(String bucketName, String objectName, File file) throws IOException {
|
|
|
@@ -52,15 +46,7 @@ public class AliyunOss {
|
|
|
ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
objectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
|
|
|
putObjectRequest.setMetadata(objectMetadata);
|
|
|
- getOssClient().putObject(putObjectRequest);
|
|
|
- }
|
|
|
-
|
|
|
- public void uploadObject(String objectName, File file) throws IOException {
|
|
|
- PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);
|
|
|
- ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
- objectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
|
|
|
- putObjectRequest.setMetadata(objectMetadata);
|
|
|
- getOssClient().putObject(putObjectRequest);
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -81,7 +67,7 @@ public class AliyunOss {
|
|
|
return failedList;
|
|
|
}
|
|
|
|
|
|
- void walkDir(String bucketName, Path path, List<String> failedList) throws IOException {
|
|
|
+ private void walkDir(String bucketName, Path path, List<String> failedList) throws IOException {
|
|
|
final String BLANK = "";
|
|
|
if (path.toString().equals(BLANK)) {
|
|
|
throw new IOException("CAN NOT specify a BLANK path");
|
|
|
@@ -111,7 +97,7 @@ public class AliyunOss {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- void uploadObject(String bucketName, File file, String baseDir, List<String> failedList) {
|
|
|
+ private void uploadObject(String bucketName, File file, String baseDir, List<String> failedList) {
|
|
|
String absolutePath = file.getAbsolutePath();
|
|
|
String objectName = absolutePath.replace(baseDir + "/", "");
|
|
|
try {
|
|
|
@@ -122,46 +108,11 @@ public class AliyunOss {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void downloadObject(String objectName) throws IOException {
|
|
|
- OSSObject object = getOssClient().getObject(bucketName, objectName);
|
|
|
- log.info("Content-Type: {}", object.getObjectMetadata().getContentType());
|
|
|
- displayTextInputStream(object.getObjectContent());
|
|
|
- }
|
|
|
-
|
|
|
- private void displayTextInputStream(InputStream input) throws IOException {
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
|
|
- while (true) {
|
|
|
- String line = reader.readLine();
|
|
|
- if (line == null) break;
|
|
|
- }
|
|
|
- reader.close();
|
|
|
- }
|
|
|
-
|
|
|
- public void deleteObject(String objectName) {
|
|
|
- getOssClient().deleteObject(bucketName, objectName);
|
|
|
- }
|
|
|
-
|
|
|
- public void listObjects(String prefix) {
|
|
|
- ObjectListing objectListing = getOssClient().listObjects(bucketName, prefix);
|
|
|
- for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
|
|
- String objectName = objectSummary.getKey();
|
|
|
- long size = objectSummary.getSize();
|
|
|
- if (size != 0) {
|
|
|
- //deleteObject(objectName);
|
|
|
- }
|
|
|
- log.info(" - {} (size={})", objectSummary.getKey(), objectSummary.getSize());
|
|
|
- }
|
|
|
+ public void deleteObject(String bucketName, String objectName) {
|
|
|
+ ossClient.deleteObject(bucketName, objectName);
|
|
|
}
|
|
|
|
|
|
public void close() {
|
|
|
- getOssClient().shutdown();
|
|
|
- }
|
|
|
-
|
|
|
- public String getSignedUrl(String objectName) {
|
|
|
- int expireSecond = 3600;
|
|
|
- long timestamp = System.currentTimeMillis() + expireSecond*1000L;
|
|
|
- Date expiration = new Date(timestamp);
|
|
|
- URL url = getOssClient().generatePresignedUrl(bucketName, objectName, expiration);
|
|
|
- return url.toString();
|
|
|
+ ossClient.shutdown();
|
|
|
}
|
|
|
}
|