guohongjin
2024-05-15 5b7639f0bd9e056738ec15100ed0532e965c6cd5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cn.stylefeng.guns.utils;
 
import cn.hutool.core.lang.UUID;
import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectResult;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
/**
 * @author guohongjin
 * @create 2023/5/8 17:37
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "obs")
public class ObsUtil {
 
    //endpoint
    private  String endPoint;
 
    private  String ak;
 
    private  String sk;
    //桶名
    private String bucketName;
 
    private  static ObsClient obsClient;
 
    //生成obsclient
    private ObsClient createObsClient(){
        if (obsClient == null){
            synchronized (ObsClient.class){
                if (obsClient == null){
                    // 创建ObsClient实例
                    obsClient = new ObsClient(ak, sk, endPoint);
                }
            }
        }
 
       return obsClient;
    }
 
    /**
     * 文件上传
     * @param filePath 文件地址
     * @param fileName 文件名称
     */
    public String upload(String filePath,String fileName){
        // 待上传的本地文件路径,需要指定到具体的文件名
        FileInputStream fis = null;
        try {
 
            fis = new FileInputStream(new File(filePath));
            String obsName = fileName.substring(fileName.lastIndexOf("/")+1,fileName.length());
            String obsFilePath = UUID.randomUUID().toString().replace("-","")+"/"+fileName.substring(0,fileName.lastIndexOf("/")+1);
//            createObsClient().putObject(this.bucketName, obsFilePath, new ByteArrayInputStream(new byte[0]));
 
            PutObjectResult putObjectResult = createObsClient().putObject(this.bucketName, "//teest//tet//"+obsName, fis);
            return putObjectResult.getObjectUrl();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 文件上传
     * @param file 文件对象
     * @param fileName 文件名称
     */
    public  String upload(File file,String fileName){
        // 待上传的本地文件路径,需要指定到具体的文件名
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            PutObjectResult putObjectResult = createObsClient().putObject(this.bucketName, UUID.randomUUID().toString().replace("-","")+"/"+fileName, fis);
            return putObjectResult.getObjectUrl();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * 文件上传
     * @param file 文件对象
     */
    public String upload(MultipartFile file){
 
        // 待上传的本地文件路径,需要指定到具体的文件名
        try {
            PutObjectResult putObjectResult = createObsClient().putObject(this.bucketName, UUID.randomUUID().toString().replace("-","")+"/"+file.getOriginalFilename(), file.getInputStream());
            return putObjectResult.getObjectUrl();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch(Exception ex){
            ex.printStackTrace();
            log.error("上传文件失败!",ex);
        }
        return null;
    }
 
 
//    public static void main(String[] args) {
//        // Endpoint以北京四为例,其他地区请按实际情况填写。
//        String endPoint = "https://obs.cn-east-3.myhuaweicloud.com";
//        String ak = "AYQCHVTCQUTHVDVLQZMJ";
//        String sk = "QSC0iTbxmugIzYoTH13lptrOkix6e15DGK1LOoky";
//        // 创建ObsClient实例
//        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
//
//        // 待上传的本地文件路径,需要指定到具体的文件名
//        FileInputStream fis = null;
//        try {
//            fis = new FileInputStream(new File("C:\\Users\\hyb\\Desktop\\CI.xlsx"));
//            PutObjectResult putObjectResult = obsClient.putObject("fbwl", "CI.xlsx", fis);
//            System.out.println(JSONObject.toJSON(putObjectResult));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//
//
////        // 待上传的本地文件路径,需要指定到具体的文件名
////        FileInputStream fis2 = new FileInputStream(new File("localfile2"));
////        PutObjectRequest request = new PutObjectRequest();
////        request.setBucketName("bucketname");
////        request.setObjectKey("objectname2");
////        request.setInput(fis2);
////        obsClient.putObject(request);
//    }
 
 
//    public static String uploadFile(String fileUrl){
//
//    }
 
 
}