tj
2025-04-11 f71719bf3e2b433b790cfaa83265611faf1f1a1c
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
package com.mzl.flower.service;
 
 
import com.mzl.flower.config.TosProperties;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.utils.UUIDGenerator;
import com.volcengine.tos.TOSV2;
 
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.PutObjectFromFileInput;
import com.volcengine.tos.model.object.PutObjectFromFileOutput;
import com.volcengine.tos.model.object.PutObjectInput;
import com.volcengine.tos.model.object.PutObjectOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.io.InputStream;
 
@Service
public class TosService {
 
    @Autowired
    private TOSV2 tosClient;
 
    @Autowired
    private TosProperties tosProperties;
 
    private String upladPre="https://";
 
 
 
    public TosService(TOSV2 tosClient) {
        this.tosClient = tosClient;
    }
 
 
    public String uploadFile(String objectKey, String filePath) throws IOException {
        try {
            PutObjectFromFileInput input = new PutObjectFromFileInput()
                    .setBucket(tosProperties.getBucketname()).setKey(objectKey).setFilePath(filePath);
 
            PutObjectFromFileOutput output = tosClient.putObjectFromFile(input);
            return "Upload success! ETag: " + output.getEtag();
        } catch (TosClientException | TosServerException e) {
            throw new IOException("Upload failed: " + e.getMessage(), e);
        }
    }
 
    public String uploadFile(InputStream inputStream, String fileName) throws IOException {
        try {
            //获取文件名称
            String filename = fileName;
            //使文件名称唯一
            String uuid = UUIDGenerator.getUUID();
            filename = uuid + filename;
            String dir = uuid.substring(0, 2);
            filename = dir + "/" + filename;
            PutObjectInput putObjectInput = new PutObjectInput().setBucket(tosProperties.getBucketname()).setKey(filename).setContent(inputStream);
            PutObjectOutput output = tosClient.putObject(putObjectInput);
//            System.out.println("putObject succeed, object's etag is " + output.getEtag());
//            System.out.println("putObject succeed, object's crc64 is " + output.getHashCrc64ecma());
            // https://edu-mys.oss-cn-chengdu.aliyuncs.com/yy.JPG
            String url = upladPre+tosProperties.getBucketname() + "." + tosProperties.getEndpoint() + "/" + filename;
            return url;
        } catch (TosClientException | TosServerException e) {
            throw new ValidationException("上传到云服务器发生异常");
        }
    }
}