Ver código fonte

添加上传文件指定header的逻辑

tianboguang 3 anos atrás
pai
commit
3e496a4212

+ 13 - 3
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -42,10 +42,15 @@ public class OssFileService extends AbstractFYunFileService {
 
     @Override
     public void uploadFile(String bucket, String filePath, String remoteFilePath) throws Exception {
-        uploadFile(bucket, filePath, remoteFilePath, true);
+        uploadFile(bucket, filePath, remoteFilePath, true, null);
     }
 
-    private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown) throws Exception {
+    @Override
+    public void uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) throws Exception {
+        uploadFile(bucket, filePath, remoteFilePath, true, headers);
+    }
+
+    private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map<String, String> headers) throws Exception {
         try {
             File file = new File(filePath);
             if (!file.exists()) {
@@ -62,6 +67,11 @@ public class OssFileService extends AbstractFYunFileService {
             if (filePath.contains(".mp3")) {
                 metadata.setContentType("audio/mp3");
             }
+            if(org.apache.commons.lang3.ObjectUtils.isNotEmpty(headers)){
+                for (Map.Entry<String, String> header : headers.entrySet()) {
+                    metadata.setHeader(header.getKey(),header.getValue());
+                }
+            }
             ossClient.putObject(bucket, remoteFilePath, new File(filePath), metadata);
         } catch (Exception e) {
             log.error("oss上传文件失败", e);
@@ -124,7 +134,7 @@ public class OssFileService extends AbstractFYunFileService {
     public void uploadMulFiles(String bucket, Map<String, String> filepaths) throws Exception {
         try {
             for (Map.Entry<String, String> entry : filepaths.entrySet()) {
-                uploadFile(bucket, entry.getKey(), entry.getValue(), false);
+                uploadFile(bucket, entry.getKey(), entry.getValue(), false,null);
             }
         } catch (Exception e) {
             log.error("OSS批量上传文件失败!");

+ 4 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/AbstractFYunFileService.java

@@ -24,6 +24,10 @@ public abstract class AbstractFYunFileService implements FYunFileServiceInterfac
         uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
     }
 
+    @Override
+    public void uploadFile(String filePath, String remoteFilePath,Map<String,String> headers) throws Exception{
+        uploadFile(fYunFileConfig.getBucket(),filePath,remoteFilePath,headers);
+    }
 
     @Override
     public void uploadFileByCommand(String filePath, String remoteFilePath) throws Exception {

+ 17 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/FYunFileServiceInterface.java

@@ -44,6 +44,23 @@ public interface FYunFileServiceInterface {
 	void uploadFile(String filePath, String remoteFilePath) throws Exception;
 
 	/**
+	 * 上传本地文件
+	 * @param bucket 目标bucket
+	 * @param filePath 本地路径
+	 * @param remoteFilePath 上传后的文件路径
+	 * @throws Exception
+	 */
+	void uploadFile(String bucket, String filePath, String remoteFilePath,Map<String,String> headers) throws Exception;
+
+	/**
+	 * 上传本地文件至系统默认bucket
+	 * @param filePath 本地路径
+	 * @param remoteFilePath 上传后的文件路径
+	 * @throws Exception
+	 */
+	void uploadFile(String filePath, String remoteFilePath,Map<String,String> headers) throws Exception;
+
+	/**
 	 * 通过本地脚本上传
 	 *
 	 * @param filePath

+ 13 - 3
4dkankan-utils-fyun-s3/src/main/java/com/fdkankan/fyun/s3/S3FileService.java

@@ -45,10 +45,15 @@ public class S3FileService extends AbstractFYunFileService {
 
     @Override
     public void uploadFile(String bucket, String filePath, String remoteFilePath) throws Exception {
-        uploadFile(bucket, filePath, remoteFilePath,true);
+        uploadFile(bucket, filePath, remoteFilePath,true,null);
     }
 
-    private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown) throws Exception {
+    @Override
+    public void uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) throws Exception {
+        uploadFile(bucket, filePath, remoteFilePath,true,headers);
+    }
+
+    private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map<String, String> headers) throws Exception {
         try {
             File file = new File(filePath);
             if (!file.exists()) {
@@ -64,6 +69,11 @@ public class S3FileService extends AbstractFYunFileService {
             if (filePath.contains(".png")) {
                 metadata.setContentType("image/png");
             }
+            if(org.apache.commons.lang3.ObjectUtils.isNotEmpty(headers)){
+                for (Map.Entry<String, String> header : headers.entrySet()) {
+                    metadata.setHeader(header.getKey(),header.getValue());
+                }
+            }
             PutObjectRequest request = new PutObjectRequest(bucket, remoteFilePath, file);
             request.withCannedAcl(CannedAccessControlList.PublicRead);
             request.withMetadata(metadata);
@@ -149,7 +159,7 @@ public class S3FileService extends AbstractFYunFileService {
     public void uploadMulFiles(String bucket, Map<String, String> filepaths) throws Exception {
         try {
             for (Map.Entry<String, String> entry : filepaths.entrySet()) {
-                uploadFile(bucket, entry.getKey(), entry.getValue(), false);
+                uploadFile(bucket, entry.getKey(), entry.getValue(), false,null);
             }
         } catch (Exception e) {
             log.error("OSS批量上传文件失败!");