commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.core.file.modular.aliyun; |
|
26 |
|
|
27 |
import cn.hutool.core.io.IoUtil; |
|
28 |
import com.aliyun.oss.ClientException; |
|
29 |
import com.aliyun.oss.OSS; |
|
30 |
import com.aliyun.oss.OSSClientBuilder; |
|
31 |
import com.aliyun.oss.OSSException; |
|
32 |
import com.aliyun.oss.model.CannedAccessControlList; |
|
33 |
import com.aliyun.oss.model.OSSObject; |
|
34 |
import com.aliyun.oss.model.PutObjectRequest; |
|
35 |
import vip.xiaonuo.core.file.FileOperator; |
|
36 |
import vip.xiaonuo.core.file.common.enums.BucketAuthEnum; |
|
37 |
import vip.xiaonuo.core.file.modular.aliyun.exp.AliyunFileServiceException; |
|
38 |
import vip.xiaonuo.core.file.modular.aliyun.prop.AliyunOssProperties; |
|
39 |
|
|
40 |
import java.io.ByteArrayInputStream; |
|
41 |
import java.io.InputStream; |
|
42 |
import java.net.URL; |
|
43 |
import java.util.Date; |
|
44 |
|
|
45 |
/** |
|
46 |
* 阿里云文件操作 |
|
47 |
* |
|
48 |
* @author xuyuxiang |
|
49 |
* @date 2020/5/25 2:33 下午 |
|
50 |
*/ |
|
51 |
public class AliyunFileOperator implements FileOperator { |
|
52 |
|
|
53 |
/** |
|
54 |
* 阿里云文件操作客户端 |
|
55 |
*/ |
|
56 |
private OSS ossClient; |
|
57 |
|
|
58 |
/** |
|
59 |
* 阿里云oss的配置 |
|
60 |
*/ |
|
61 |
private final AliyunOssProperties aliyunOssProperties; |
|
62 |
|
|
63 |
public AliyunFileOperator(AliyunOssProperties aliyunOssProperties) { |
|
64 |
this.aliyunOssProperties = aliyunOssProperties; |
|
65 |
this.initClient(); |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public void initClient() { |
|
70 |
String endpoint = aliyunOssProperties.getEndPoint(); |
|
71 |
String accessKeyId = aliyunOssProperties.getAccessKeyId(); |
|
72 |
String accessKeySecret = aliyunOssProperties.getAccessKeySecret(); |
|
73 |
|
|
74 |
// 创建OSSClient实例。 |
|
75 |
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
public void destroyClient() { |
|
80 |
ossClient.shutdown(); |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public Object getClient() { |
|
85 |
return ossClient; |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public boolean doesBucketExist(String bucketName) { |
|
90 |
try { |
|
91 |
return ossClient.doesBucketExist(bucketName); |
|
92 |
} catch (OSSException e) { |
|
93 |
throw new AliyunFileServiceException(e); |
|
94 |
} catch (ClientException e) { |
|
95 |
throw new AliyunFileServiceException(e); |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public void setBucketAcl(String bucketName, BucketAuthEnum bucketAuthEnum) { |
|
101 |
try { |
|
102 |
if (bucketAuthEnum.equals(BucketAuthEnum.PRIVATE)) { |
|
103 |
ossClient.setBucketAcl(bucketName, CannedAccessControlList.Private); |
|
104 |
} else if (bucketAuthEnum.equals(BucketAuthEnum.PUBLIC_READ)) { |
|
105 |
ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead); |
|
106 |
} else if (bucketAuthEnum.equals(BucketAuthEnum.PUBLIC_READ_WRITE)) { |
|
107 |
ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicReadWrite); |
|
108 |
} |
|
109 |
} catch (OSSException e) { |
|
110 |
throw new AliyunFileServiceException(e); |
|
111 |
} catch (ClientException e) { |
|
112 |
throw new AliyunFileServiceException(e); |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public boolean isExistingFile(String bucketName, String key) { |
|
118 |
try { |
|
119 |
return ossClient.doesObjectExist(bucketName, key); |
|
120 |
} catch (OSSException e) { |
|
121 |
throw new AliyunFileServiceException(e); |
|
122 |
} catch (ClientException e) { |
|
123 |
throw new AliyunFileServiceException(e); |
|
124 |
} |
|
125 |
} |
|
126 |
|
|
127 |
@Override |
|
128 |
public void storageFile(String bucketName, String key, byte[] bytes) { |
|
129 |
try { |
|
130 |
ossClient.putObject(bucketName, key, new ByteArrayInputStream(bytes)); |
|
131 |
} catch (OSSException e) { |
|
132 |
throw new AliyunFileServiceException(e); |
|
133 |
} catch (ClientException e) { |
|
134 |
throw new AliyunFileServiceException(e); |
|
135 |
} |
|
136 |
} |
|
137 |
|
|
138 |
@Override |
|
139 |
public void storageFile(String bucketName, String key, InputStream inputStream) { |
|
140 |
try { |
|
141 |
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream); |
|
142 |
ossClient.putObject(putObjectRequest); |
|
143 |
} catch (OSSException e) { |
|
144 |
throw new AliyunFileServiceException(e); |
|
145 |
} catch (ClientException e) { |
|
146 |
throw new AliyunFileServiceException(e); |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
@Override |
|
151 |
public byte[] getFileBytes(String bucketName, String key) { |
|
152 |
InputStream objectContent = null; |
|
153 |
try { |
|
154 |
OSSObject ossObject = ossClient.getObject(bucketName, key); |
|
155 |
objectContent = ossObject.getObjectContent(); |
|
156 |
return IoUtil.readBytes(objectContent); |
|
157 |
} catch (OSSException e) { |
|
158 |
throw new AliyunFileServiceException(e); |
|
159 |
} catch (ClientException e) { |
|
160 |
throw new AliyunFileServiceException(e); |
|
161 |
} finally { |
|
162 |
IoUtil.close(objectContent); |
|
163 |
} |
|
164 |
|
|
165 |
} |
|
166 |
|
|
167 |
@Override |
|
168 |
public void setFileAcl(String bucketName, String key, BucketAuthEnum bucketAuthEnum) { |
|
169 |
try { |
|
170 |
if (bucketAuthEnum.equals(BucketAuthEnum.PRIVATE)) { |
|
171 |
ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.Private); |
|
172 |
} else if (bucketAuthEnum.equals(BucketAuthEnum.PUBLIC_READ)) { |
|
173 |
ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead); |
|
174 |
} else if (bucketAuthEnum.equals(BucketAuthEnum.PUBLIC_READ_WRITE)) { |
|
175 |
ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicReadWrite); |
|
176 |
} |
|
177 |
} catch (OSSException e) { |
|
178 |
throw new AliyunFileServiceException(e); |
|
179 |
} catch (ClientException e) { |
|
180 |
throw new AliyunFileServiceException(e); |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
@Override |
|
185 |
public void copyFile(String originBucketName, String originFileKey, String newBucketName, String newFileKey) { |
|
186 |
try { |
|
187 |
ossClient.copyObject(originBucketName, originFileKey, newBucketName, newFileKey); |
|
188 |
} catch (OSSException e) { |
|
189 |
throw new AliyunFileServiceException(e); |
|
190 |
} catch (ClientException e) { |
|
191 |
throw new AliyunFileServiceException(e); |
|
192 |
} |
|
193 |
} |
|
194 |
|
|
195 |
@Override |
|
196 |
public String getFileAuthUrl(String bucketName, String key, Long timeoutMillis) { |
|
197 |
try { |
|
198 |
Date expiration = new Date(new Date().getTime() + timeoutMillis); |
|
199 |
URL url = ossClient.generatePresignedUrl(bucketName, key, expiration); |
|
200 |
return url.toString(); |
|
201 |
} catch (OSSException e) { |
|
202 |
throw new AliyunFileServiceException(e); |
|
203 |
} catch (ClientException e) { |
|
204 |
throw new AliyunFileServiceException(e); |
|
205 |
} |
|
206 |
} |
|
207 |
|
|
208 |
@Override |
|
209 |
public void deleteFile(String bucketName, String key) { |
|
210 |
ossClient.deleteObject(bucketName, key); |
|
211 |
} |
|
212 |
|
|
213 |
} |