add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.dobbinsoft.fw.support.storage;
 
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.dobbinsoft.fw.support.properties.FwObjectStorageProperties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
 
/**
 * ClassName: AliStorageClient
 * Description: 阿里云对象存储实现
 *
 * @author: e-weichaozheng
 * @date: 2021-03-17
 */
public class AliStorageClient implements StorageClient, InitializingBean {
 
    @Autowired
    private FwObjectStorageProperties properties;
 
    private OSSClient ossClient;
 
    @Override
    public void afterPropertiesSet() throws Exception {
         ossClient = new OSSClient(properties.getAliEndpoint(), properties.getAliAccessKeyId(), properties.getAliAccessKeySecret());
    }
 
    @Override
    public StorageResult save(StorageRequest request) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(request.getSize());
        objectMetadata.setContentType(request.getContentType());
        PutObjectRequest putObjectRequest =
                new PutObjectRequest(
                        properties.getAliBucket(),
                        request.getPath() + "/" + request.getFilename(), request.getIs(), objectMetadata);
        putObjectRequest.setProcess("");
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        ResponseMessage response = putObjectResult.getResponse();
        StorageResult result = new StorageResult();
        int statusCode = response.getStatusCode();
        result.setSuc(statusCode == 200);
        if (result.isSuc()) {
            result.setUrl(properties.getAliBaseUrl() + request.getPath() + "/" + request.getFilename());
        }
        return result;
    }
 
    @Override
    public StoragePrivateResult savePrivate(StorageRequest request) {
        throw new RuntimeException("不支持私有保存");
    }
 
    @Override
    public boolean delete(String url) {
        int index = url.indexOf("/", 5);
        String key = url.substring(index);
        ossClient.deleteObject(properties.getAliBucket(), key);
        return true;
    }
 
    @Override
    public boolean deletePrivate(String key) {
        throw new RuntimeException("不支持私有保存");
    }
 
    @Override
    public String getPrivateUrl(String key, Integer expireSec) {
        throw new RuntimeException("不支持私有保存");
    }
 
    @Override
    public String getKeyFormUrl(String url) {
        throw new RuntimeException("不支持私有保存");
    }
}