tj
2025-04-17 15ea9064ecdfbb9c90a1e5123f2f60e0e94c5b9f
src/main/java/com/mzl/flower/service/oss/TosOssService.java
文件名从 src/main/java/com/mzl/flower/service/TosService.java 修改
@@ -1,7 +1,7 @@
package com.mzl.flower.service;
package com.mzl.flower.service.oss;
import com.mzl.flower.config.TosProperties;
import com.mzl.flower.config.oss.TosOssProperties;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.utils.UUIDGenerator;
import com.volcengine.tos.TOSV2;
@@ -13,26 +13,25 @@
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 {
public class TosOssService {
    @Autowired
    private TOSV2 tosClient;
    @Autowired
    private TosProperties tosProperties;
    private TosOssProperties tosOssProperties;
    private String upladPre="https://";
    public TosService(TOSV2 tosClient) {
    public TosOssService(TOSV2 tosClient) {
        this.tosClient = tosClient;
    }
@@ -40,7 +39,7 @@
    public String uploadFile(String objectKey, String filePath) throws IOException {
        try {
            PutObjectFromFileInput input = new PutObjectFromFileInput()
                    .setBucket(tosProperties.getBucketname()).setKey(objectKey).setFilePath(filePath);
                    .setBucket(tosOssProperties.getBucketname()).setKey(objectKey).setFilePath(filePath);
            PutObjectFromFileOutput output = tosClient.putObjectFromFile(input);
            return "Upload success! ETag: " + output.getEtag();
@@ -58,12 +57,12 @@
            filename = uuid + filename;
            String dir = uuid.substring(0, 2);
            filename = dir + "/" + filename;
            PutObjectInput putObjectInput = new PutObjectInput().setBucket(tosProperties.getBucketname()).setKey(filename).setContent(inputStream);
            PutObjectInput putObjectInput = new PutObjectInput().setBucket(tosOssProperties.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;
            String url = upladPre+ tosOssProperties.getBucketname() + "." + tosOssProperties.getEndpoint() + "/" + filename;
            return url;
        } catch (TosClientException | TosServerException e) {
            throw new ValidationException("上传到云服务器发生异常");