AliyunOSSUtils.java
5.73 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.jflyfox.system.file.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import org.apache.log4j.Logger;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;
import com.jflyfox.system.config.ConfigCache;
/**
* 阿里云存储OSS工具类
*
* 2015年11月16日 上午9:55:27 flyfox 330627517@qq.com
*/
public class AliyunOSSUtils {
private static final Logger logger = Logger.getLogger(AliyunOSSUtils.class);
public AliyunOSS oss = new AliyunOSS();
/**
* 获取OSS Client
*
* 2015年11月16日 上午9:06:11 flyfox 330627517@qq.com
*
* @return
* @throws OSSException
* @throws ClientException
*/
public OSSClient getClient() {
return oss.getClient();
}
/**
* 上传文件
*
* 2015年11月16日 上午9:06:40 flyfox 330627517@qq.com
*
* @param key
* @param filename
*/
public boolean uploadFile(String key, String filename) {
try {
oss.uploadFile(getClient(), getBucketName(), key, filename);
return true;
} catch (Exception e) {
logger.error(e);
}
return false;
}
/**
* 下载文件
*
* 2015年11月16日 上午9:06:50 flyfox 330627517@qq.com
*
* @param key
* @param filename
*/
public boolean downloadFile(String key, String filename) {
try {
oss.downloadFile(getClient(), getBucketName(), key, filename);
return true;
} catch (Exception e) {
logger.error(e);
}
return false;
}
/**
* 删除一个Bucket和其中的Objects
*
* 2015年11月16日 上午9:06:57 flyfox 330627517@qq.com
*
* @param key
*/
public boolean deleteObject(String key) {
try {
oss.deleteObject(getClient(), getBucketName(), key);
return true;
} catch (Exception e) {
logger.error(e);
}
return false;
}
public String getBucketName() {
String BUCKET_NAME = ConfigCache.getValue("backup.oss.bucketname");
return BUCKET_NAME;
}
}
class AliyunOSS {
private String ENDPOINT = ConfigCache.getValue("backup.oss.endpoint");
private String ACCESS_ID = ConfigCache.getValue("backup.oss.id");
private String ACCESS_KEY = ConfigCache.getValue("backup.oss.key");
private String BUCKET_NAME = ConfigCache.getValue("backup.oss.bucketname");
/**
* 获取OSS Client
*
* 2015年11月16日 上午9:06:11 flyfox 330627517@qq.com
*
* @return
* @throws OSSException
* @throws ClientException
*/
public OSSClient getClient() throws OSSException, ClientException {
// 这里使用的北京OSS服务器
OSSClient client = new OSSClient(ENDPOINT, ACCESS_ID, ACCESS_KEY);
client.getBucketLocation(BUCKET_NAME);
return client;
}
/**
* 删除一个Bucket和其中的Objects
*
* 2015年11月16日 上午9:06:33 flyfox 330627517@qq.com
*
* @param client
* @param bucketName
* @throws OSSException
* @throws ClientException
*/
public void deleteBucket(OSSClient client, String bucketName) throws OSSException, ClientException {
ObjectListing ObjectListing = getClient().listObjects(bucketName);
List<OSSObjectSummary> listDeletes = ObjectListing.getObjectSummaries();
for (int i = 0; i < listDeletes.size(); i++) {
String objectName = listDeletes.get(i).getKey();
// 如果不为空,先删除bucket下的文件
getClient().deleteObject(bucketName, objectName);
}
getClient().deleteBucket(bucketName);
}
/**
* 上传文件
*
* 2015年11月16日 上午9:06:40 flyfox 330627517@qq.com
*
* @param client
* @param bucketName
* @param key
* @param filename
* @throws OSSException
* @throws ClientException
* @throws FileNotFoundException
*/
public void uploadFile(OSSClient client, String bucketName, String key, String filename) throws OSSException,
ClientException, FileNotFoundException {
File file = new File(filename);
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentLength(file.length());
// 可以在metadata中标记文件类型
String suf = filename.substring(filename.lastIndexOf("."));
if (".css".equals(suf)) {
objectMeta.setContentType("text/css");
} else if (".js".equals(suf)) {
objectMeta.setContentType("application/x-javascript");
} else if (".htm".equals(suf) || ".html".equals(suf)) {
objectMeta.setContentType("text/html");
} else if (".json".equals(suf) || ".jsonp".equals(suf)) {
objectMeta.setContentType("text/plain");
} else if (".bmp".equals(suf) || ".png".equals(suf) //
|| ".jpg".equals(suf) || ".jpeg".equals(suf) || ".jfif".equals(suf)) {
objectMeta.setContentType("image/jpeg");
} else {
objectMeta.setContentType("text/plain");
}
InputStream input = new FileInputStream(file);
client.putObject(bucketName, key, input, objectMeta);
}
/**
* 下载文件
*
* 2015年11月16日 上午9:06:50 flyfox 330627517@qq.com
*
* @param client
* @param bucketName
* @param key
* @param filename
* @throws OSSException
* @throws ClientException
*/
public void downloadFile(OSSClient client, String bucketName, String key, String filename) throws OSSException,
ClientException {
if (client.doesObjectExist(bucketName, key)) {
client.getObject(new GetObjectRequest(bucketName, key), new File(filename));
}
}
/**
* 删除一个Bucket和其中的Objects
*
* 2015年11月16日 上午9:06:57 flyfox 330627517@qq.com
*
* @param client
* @param bucketName
* @param key
* @throws OSSException
* @throws ClientException
*/
public void deleteObject(OSSClient client, String bucketName, String key) throws OSSException, ClientException {
client.deleteObject(bucketName, key);
}
}