dengsixing 8 hónapja
szülő
commit
9b0e37b166

+ 18 - 20
4dkankan-utils-fyun-obs/src/main/java/com/fdkankan/fyun/oss/ObsFileService.java

@@ -1,10 +1,12 @@
 package com.fdkankan.fyun.oss;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.StrUtil;
 import com.fdkankan.fyun.constant.FYunTypeEnum;
 import com.fdkankan.fyun.face.AbstractFYunFileService;
 import com.obs.services.ObsClient;
 import com.obs.services.model.*;
+import org.apache.http.HttpHeaders;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -52,7 +54,7 @@ public class ObsFileService extends AbstractFYunFileService {
         try {
             PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, remoteFilePath, inputStream);
             putObjectRequest.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
-            obsClient.putObject(bucket, remoteFilePath, inputStream);
+            obsClient.putObject(putObjectRequest);
             log.info("文件流上传成功,目标路径:remoteFilePath:{}", remoteFilePath);
         } catch (Exception e) {
             log.error("oss上传文件失败,remoteFilePath:"+remoteFilePath, e);
@@ -69,22 +71,11 @@ public class ObsFileService extends AbstractFYunFileService {
                 return null;
             }
             ObjectMetadata metadata = new ObjectMetadata();
-            if (filePath.contains(".jpg")) {
-                metadata.setContentType("image/jpeg");
-            }
-            if (filePath.contains(".mp4")) {
-                metadata.setContentType("video/mp4");
-            }
-            if (filePath.contains(".mp3")) {
-                metadata.setContentType("audio/mp3");
-            }
-            if(CollUtil.isNotEmpty(headers)){
-                String contentEncoding = headers.get("Content-Encoding");
-                metadata.setContentEncoding(contentEncoding);
-            }
             if (CollUtil.isNotEmpty(headers)) {
-                Map<String, Object> collect = headers.keySet().stream().collect(Collectors.toMap(v -> v, v -> headers.get(v)));
-                metadata.setUserMetadata(collect);
+                String contentType = headers.get(HttpHeaders.CONTENT_TYPE);
+                if(StrUtil.isNotEmpty(contentType)){
+                    metadata.setContentType(contentType);
+                }
             }
             PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, remoteFilePath, file);
             putObjectRequest.setMetadata(metadata);
@@ -101,7 +92,7 @@ public class ObsFileService extends AbstractFYunFileService {
     public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
         try {
             String optType = new File(filePath).isDirectory() ? "folder" : "file";
-            String command = String.format(fYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.OSS.code(), optType);
+            String command = String.format(fYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.OBS.code(), optType);
             log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             callshell(command);
             log.info("上传文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
@@ -115,7 +106,7 @@ public class ObsFileService extends AbstractFYunFileService {
     public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
         try {
             String optType = remoteFilePath.contains(".") ? "file" : "folder";
-            String command = String.format(fYunConstants.DOWNLOAD_SH, bucket, remoteFilePath, filePath, FYunTypeEnum.OSS.code(), optType);
+            String command = String.format(fYunConstants.DOWNLOAD_SH, bucket, remoteFilePath, filePath, FYunTypeEnum.OBS.code(), optType);
             log.info("开始下载文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             callshell(command);
             log.info("下载文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
@@ -214,7 +205,9 @@ public class ObsFileService extends AbstractFYunFileService {
                 return;
             }
             files.stream().forEach(file -> {
-                obsClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
+                CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
+                copyObjectRequest.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
+                obsClient.copyObject(copyObjectRequest);
             });
         } catch (Exception e) {
             log.error("列举文件目录失败,key:" + sourcePath, e);
@@ -237,7 +230,12 @@ public class ObsFileService extends AbstractFYunFileService {
 
     @Override
     public String getFileContent(String bucketName, String remoteFilePath) {
-        ObsObject ossObject = obsClient.getObject(bucketName, remoteFilePath);
+        ObsObject ossObject = null;
+        try {
+            ossObject = obsClient.getObject(bucketName, remoteFilePath);
+        }catch (Exception e){
+            return null;
+        }
         try (InputStream objectContent = ossObject.getObjectContent()){
             StringBuilder contentJson = new StringBuilder();
             try (BufferedReader reader = new BufferedReader(new InputStreamReader(objectContent))) {

+ 3 - 1
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/constant/FYunTypeEnum.java

@@ -5,7 +5,9 @@ public enum FYunTypeEnum {
     AWS("aws", "亚马逊"),
     COS("cos", "腾讯云"),
     LOCAL("local", "本地"),
-    HTTPS("https", "通过https请求文件");
+    HTTPS("https", "通过https请求文件"),
+
+    OBS("obs", "华为云");
 
     private String code;
     private String message;