|
@@ -0,0 +1,199 @@
|
|
|
+package cn.com.qmth.examcloud.web.aliyun;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.web.config.SystemProperties;
|
|
|
+import com.aliyun.oss.ClientBuilderConfiguration;
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
+import com.aliyun.oss.model.GetObjectRequest;
|
|
|
+import com.aliyun.oss.model.OSSObject;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesRequest;
|
|
|
+import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesResponse;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class OssClient {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(OssClient.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SystemProperties properties;
|
|
|
+
|
|
|
+ public void upload(File file, String ossFilePath) {
|
|
|
+ this.upload(file, ossFilePath, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void upload(File file, String ossFilePath, boolean refreshCDNCache) {
|
|
|
+ OSS ossClient = this.getClient();
|
|
|
+ ossFilePath = this.fixOssFilePath(ossFilePath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // ObjectMetadata meta = new ObjectMetadata();
|
|
|
+ // meta.setContentMD5(md5);
|
|
|
+ ossClient.putObject(properties.getAliyunBucketName(), ossFilePath, file);
|
|
|
+
|
|
|
+ if (refreshCDNCache) {
|
|
|
+ this.refreshCDNCache(properties.getAliyunDomain() + "/" + ossFilePath);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("OSS文件上传异常!{}", e.getMessage());
|
|
|
+ throw new StatusException("OSS文件上传异常!");
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ ossClient.shutdown();
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void upload(byte[] fileBytes, String ossFilePath) {
|
|
|
+ this.upload(fileBytes, ossFilePath, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void upload(byte[] fileBytes, String ossFilePath, boolean refreshCDNCache) {
|
|
|
+ OSS ossClient = this.getClient();
|
|
|
+ ossFilePath = this.fixOssFilePath(ossFilePath);
|
|
|
+
|
|
|
+ try (ByteArrayInputStream input = new ByteArrayInputStream(fileBytes);) {
|
|
|
+ ossClient.putObject(properties.getAliyunBucketName(), ossFilePath, input);
|
|
|
+
|
|
|
+ if (refreshCDNCache) {
|
|
|
+ this.refreshCDNCache(properties.getAliyunDomain() + "/" + ossFilePath);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("OSS文件上传异常!{}", e.getMessage());
|
|
|
+ throw new StatusException("OSS文件上传异常!");
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ ossClient.shutdown();
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void download(String ossFilePath, File toFile) {
|
|
|
+ OSS ossClient = this.getClient();
|
|
|
+ ossFilePath = this.fixOssFilePath(ossFilePath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ GetObjectRequest request = new GetObjectRequest(properties.getAliyunBucketName(), ossFilePath);
|
|
|
+ ossClient.getObject(request, toFile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("OSS文件下载异常!{}", e.getMessage());
|
|
|
+ throw new StatusException("OSS文件下载异常!");
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ ossClient.shutdown();
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public byte[] download(String ossFilePath) {
|
|
|
+ OSS ossClient = this.getClient();
|
|
|
+ ossFilePath = this.fixOssFilePath(ossFilePath);
|
|
|
+
|
|
|
+ InputStream is = null;
|
|
|
+ ByteArrayOutputStream bos = null;
|
|
|
+ try {
|
|
|
+ GetObjectRequest request = new GetObjectRequest(properties.getAliyunBucketName(), ossFilePath);
|
|
|
+ OSSObject ossObject = ossClient.getObject(request);
|
|
|
+
|
|
|
+ is = ossObject.getObjectContent();
|
|
|
+ bos = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ int len;
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ while ((len = is.read(bytes)) != -1) {
|
|
|
+ bos.write(bytes, 0, len);
|
|
|
+ }
|
|
|
+ bos.flush();
|
|
|
+ ossObject.close();
|
|
|
+
|
|
|
+ return bos.toByteArray();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("OSS文件下载异常!{}", e.getMessage());
|
|
|
+ throw new StatusException("OSS文件下载异常!");
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ ossClient.shutdown();
|
|
|
+ if (is != null) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ if (bos != null) {
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新文件的CDN缓存:
|
|
|
+ * 1、同一个账号 每天最多可提交2000条URL刷新和100个目录刷新
|
|
|
+ * 2、每次请求最多只能提交1000条URL刷新,多个URL之间需要用换行符 \n 分割
|
|
|
+ * 3、每秒最多50次请求
|
|
|
+ */
|
|
|
+ public void refreshCDNCache(String ossFileUrls) {
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile("oss-cn-shenzhen",
|
|
|
+ properties.getAccessKeyId(), properties.getAccessKeySecret());
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+
|
|
|
+ RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
|
|
|
+ request.setObjectPath(ossFileUrls);
|
|
|
+ request.setObjectType("file");
|
|
|
+
|
|
|
+ try {
|
|
|
+ RefreshObjectCachesResponse resp = client.getAcsResponse(request);
|
|
|
+
|
|
|
+ log.info("refreshCDNCache:{} RequestId:{}", ossFileUrls, resp != null ? resp.getRequestId() : "none");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("refreshCDNCache:{} err:{}", ossFileUrls, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String fixOssFilePath(String ossFilePath) {
|
|
|
+ // OSS file path must be not start with '/'
|
|
|
+ if (ossFilePath.startsWith("/")) {
|
|
|
+ return ossFilePath.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return ossFilePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OSS getClient() {
|
|
|
+ ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
|
|
|
+
|
|
|
+ OSS client = new OSSClientBuilder().build(
|
|
|
+ properties.getAliyunOssEndpoint(),
|
|
|
+ properties.getAccessKeyId(),
|
|
|
+ properties.getAccessKeySecret(),
|
|
|
+ configuration
|
|
|
+ );
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+
|
|
|
+ public SystemProperties getProperties() {
|
|
|
+ return properties;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setProperties(SystemProperties properties) {
|
|
|
+ this.properties = properties;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|