k94314517
2024-07-11 a52f8c508d08014d1a153cfc02e12b5fc185a3a4
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.doumee.core.utils.tyyun;
 
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import com.doumee.core.utils.FileDigest;
 
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
 
@Slf4j
public class TyyZosUtil {
    private AmazonS3 client;
 
    // public static final String ENDPOINT = "oss-cn-shanghai.aliyuncs.com";
 
    public TyyZosUtil(String END_POINT, String ACCESS_KEY, String SECRET_KEY) {
        try {
            AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY,SECRET_KEY);
            ClientConfiguration awsClientConfig = new ClientConfiguration();
            awsClientConfig.setSignerOverride("AWSS3V4SignerType");
            awsClientConfig.setProtocol(Protocol.HTTP);
 
            client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new
                            AWSStaticCredentialsProvider(credentials))
                    .withClientConfiguration(awsClientConfig)
                    .withEndpointConfiguration(new
                            AwsClientBuilder.EndpointConfiguration(END_POINT, ""))
                    .disableChunkedEncoding()
                    .enablePathStyleAccess()
                    .build();
        }catch (Exception e){
                log.error("对象存储====================连接天翼云ZOS失败"+e.getMessage());
        }
    }
 
    /**
     * 上传文件
     *
     * @param bucketName 云端存放bucket名称
     * @param key        重新命名的文件名
     * @param filepath   将要上传的文件名称
     * @throws FileNotFoundException
     */
 
    public void uploadObject(String bucketName, String key, String filepath,
                             String mime){
        try {
            File file= new File(filepath);
            PutObjectRequest request = new PutObjectRequest(bucketName, key, file);
            PutObjectResult result = client.putObject(request);
        }catch (Exception e){
            log.error("对象存储===================="+filepath+"文件上传失败"+e.getMessage());
        }finally {
            shutDown();
        }
    }
 
    /**
     * 讲key文件存储在本地filename目标文件中
     * @param bucketName
     * @param key
     * @param filename
     */
    public void getObject(String bucketName, String key, String filename ){
        try {
            GetObjectRequest request = new GetObjectRequest(bucketName, key);
            S3Object result = client.getObject(request);
            System.out.print("=====request success=====\n");
            try {
                InputStream in = result.getObjectContent();
                File outputFile = new File(filename);
                FileOutputStream outputStream = new
                        FileOutputStream(outputFile);
                byte[] read_buf = new byte[1024 * 1024];
                int read_len = 0;
                while ((read_len = in.read(read_buf)) > 0) {
                    outputStream.write(read_buf, 0, read_len);
                }
                in.close();
                outputStream.close();
            } catch (IOException e){
                e.printStackTrace();
            }
        }catch (Exception e){
            log.error("对象存储===================="+filename+"文件读取失败"+e.getMessage());
        }finally {
            shutDown();
        }
    }
    public void deleteObject(String bucketName, String key  ){
        try {
            DeleteObjectRequest request = new DeleteObjectRequest(bucketName, key);
            client.deleteObject(request);
            System.out.print("=====request deleteObject success=====");
        }catch (Exception e){
            log.error("对象存储===================="+key+"文件删除失败"+e.getMessage());
        }finally {
            shutDown();
        }
    }
 
 
    /**
     * 关闭
     *
     * @throws FileNotFoundException
     */
 
    public void shutDown() {
        if (client != null) {
            // 关闭client
            client.shutdown();
        }
    }
 
    public static void main(String[] args) {
        TyyZosUtil aLiYunUtil = new TyyZosUtil("", "uc4nnpsqep1i9fijqr37nokh",
                "/rp41xCx/XdGEVCptdH6v7xpc9w=");
        // aLiYunUtil.uploadObject("pongto", "work/li2.txt", "D://哈.txt",
        // ".html,.html text/html");D://装机软件/办公学习
        // aLiYunUtil.partUploadObject("pongto", "work/ps.exe",
        // "D://装机软件/办公学习/Adobe_Illustrator_CS6_XiaZaiBa.exe",
        // ".html,.html text/html");
        // aLiYunUtil.deleteBucket("pongto");
    }
 
    /**
     * 上传网络文件
     *
     * @param bucketName 云端存放bucket名称
     * @param key        重新命名的文件名
     *                   将要上传的文件名称
     * @throws IOException
     */
 
    public boolean uploadOnlineObject(String url, String bucketName, String key )     {
        try {
 
            InputStream inputStream = new URL(url).openStream();
            if (inputStream != null) {
                ObjectMetadata metadata = new ObjectMetadata();
                PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream,metadata);
                PutObjectResult result = client.putObject(request);
            }else {
                log.error("对象存储===================="+url+"网络文件上传失败,网络文件读取失败");
            }
        }catch (Exception e){
            log.error("对象存储===================="+url+"网络文件上传失败"+e.getMessage());
        }finally {
            shutDown();
        }
        return false;
 
    }
    public boolean uploadInputstreamObject(InputStream inputStream, String bucketName, String key )     {
        try {
 
            if (inputStream != null) {
                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setContentLength(inputStream.available());
 
                PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream,metadata);
                request.setCannedAcl(CannedAccessControlList.PublicRead);
                PutObjectResult result = client.putObject(request);
                return true;
            }
        }catch (Exception e){
            log.error("对象存储==================== 文件上传失败"+e.getMessage());
        }finally {
            shutDown();
        }
        return false;
 
    }
 
}