|
@@ -2,102 +2,115 @@ package com.gree.mall.manager.utils.oss;
|
|
|
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
-import com.aliyun.oss.common.comm.ResponseMessage;
|
|
|
-import com.aliyun.oss.model.*;
|
|
|
-import com.gree.mall.manager.constant.Constant;
|
|
|
+import com.aliyun.oss.model.GeneratePresignedUrlRequest;
|
|
|
+import com.aliyun.oss.model.PutObjectRequest;
|
|
|
+import com.aliyun.oss.model.PutObjectResult;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.MediaType;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.net.URL;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
|
|
|
-
|
|
|
+@Service
|
|
|
@Slf4j
|
|
|
public class OSSUtil {
|
|
|
|
|
|
+ @Value("${ali.access.key.id}")
|
|
|
+ private String ACCESS_KEY_ID;
|
|
|
+ @Value("${ali.access.key.secert}")
|
|
|
+ private String ACCESS_KEY_SECERT;
|
|
|
+
|
|
|
+ @Value("${ali.oss.bucket.name}")
|
|
|
+ private String OSS_BUCKET_NAME;
|
|
|
+ @Value("${ali.oss.endpoint.ww}")
|
|
|
+ private String OSS_ENDPOINT_WW;
|
|
|
+ @Value("${ali.oss.endpoint}")
|
|
|
+ private String OSS_ENDPOINT;
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
+ private String active;
|
|
|
+ @Value("${sys.url}")
|
|
|
+ private String sysUrl;
|
|
|
+ @Value("${server.servlet.context-path}")
|
|
|
+ private String servletPath;
|
|
|
+
|
|
|
+ public String getAccessUrl(){
|
|
|
+ return "https://" + OSS_BUCKET_NAME +"."+ OSS_ENDPOINT_WW;
|
|
|
+ }
|
|
|
|
|
|
- // 用户相关文件夹
|
|
|
- public static final String ALIOSS_BUCKETNAME= "zfire-train";
|
|
|
- public static final String OSS_TYPE_PIC = "train/pic/";
|
|
|
- public static final String AliOSSEndPoint = "http://oss-cn-guangzhou.aliyuncs.com";
|
|
|
- public static final String AliOSSEndPointNohttp = "oss-cn-guangzhou.aliyuncs.com";
|
|
|
- public static final String accessUrl = "https://zfire-train.oss-cn-guangzhou.aliyuncs.com/";
|
|
|
-// public static final String OSSAccessKeyID = "LTAI4G3XTLh8G8DiTpXHpWgt";
|
|
|
-// public static final String OSSAccessKeySecret = "LYe8uf7Lg9WxJ1xejk45qnPFqxXLzN";
|
|
|
+ public String getAccessUrl2(){
|
|
|
+ return sysUrl + servletPath + "/img/get?key=";
|
|
|
+ }
|
|
|
|
|
|
- private static OSS ossClientPri = new OSSClientBuilder().build(AliOSSEndPoint,
|
|
|
- Constant.Ali.ACCESS_KEY_ID, Constant.Ali.ACCESS_KEY_SECERT);
|
|
|
+ public String getFilePath() {
|
|
|
+ String month = new SimpleDateFormat("yyyy-MM").format(new Date());
|
|
|
+ return month;
|
|
|
+ }
|
|
|
|
|
|
- public static boolean uploadFile(String ossObjectKey, byte[] file) throws IOException {
|
|
|
+ public OSS buildClient(){
|
|
|
+ return new OSSClientBuilder().build(!active.equals("prd") ? OSS_ENDPOINT_WW : OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECERT);
|
|
|
+ }
|
|
|
|
|
|
- PutObjectResult putObjectResult = null;
|
|
|
- putObjectResult = ossClientPri.putObject(new PutObjectRequest(ALIOSS_BUCKETNAME, ossObjectKey, new ByteArrayInputStream(file)));
|
|
|
+ /**
|
|
|
+ * 上传到oss
|
|
|
+ *
|
|
|
+ * @param ossObjectKey 附件的唯一路径名称
|
|
|
+ * @param file 附件
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean uploadFile(String ossObjectKey, File file) {
|
|
|
+ PutObjectResult putObjectResult = buildClient().putObject(new PutObjectRequest(OSS_BUCKET_NAME, ossObjectKey, file));
|
|
|
log.info("putObjectResult" + putObjectResult);
|
|
|
- if (putObjectResult != null) {
|
|
|
- ResponseMessage responseMessage = putObjectResult.getResponse();
|
|
|
- if (responseMessage != null) {
|
|
|
- log.error(responseMessage.getErrorResponseAsString());
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- public static boolean uploadFile(String ossObjectKey,File file) throws IOException {
|
|
|
- if (file == null) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ public boolean uploadFile(String ossObjectKey, byte[] file) {
|
|
|
+ PutObjectResult putObjectResult = buildClient().putObject(new PutObjectRequest(OSS_BUCKET_NAME, ossObjectKey, new ByteArrayInputStream(file)));
|
|
|
+ log.info("putObjectResult" + putObjectResult);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
- PutObjectResult putObjectResult = ossClientPri.putObject(new PutObjectRequest(ALIOSS_BUCKETNAME, ossObjectKey, new FileInputStream(file)));
|
|
|
+ public boolean uploadFile(String ossObjectKey, ByteArrayInputStream byteArrayInputStream) {
|
|
|
+ PutObjectResult putObjectResult = buildClient().putObject(new PutObjectRequest(OSS_BUCKET_NAME, ossObjectKey, byteArrayInputStream));
|
|
|
log.info("putObjectResult" + putObjectResult);
|
|
|
- if (putObjectResult != null) {
|
|
|
- ResponseMessage responseMessage = putObjectResult.getResponse();
|
|
|
- if (responseMessage != null) {
|
|
|
- log.error(responseMessage.getErrorResponseAsString());
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
+ return true;
|
|
|
}
|
|
|
- public static String getUrlWw(String key){
|
|
|
- OSS clientWw = new OSSClientBuilder().build(AliOSSEndPoint,Constant.Ali.ACCESS_KEY_ID, Constant.Ali.ACCESS_KEY_SECERT);
|
|
|
- // 设置URL过期时间为1小时
|
|
|
- Date expiration = new Date(new Date().getTime() + 3600 * 1000);
|
|
|
- GeneratePresignedUrlRequest generatePresignedUrlRequest ;
|
|
|
- generatePresignedUrlRequest =new GeneratePresignedUrlRequest(ALIOSS_BUCKETNAME, key);
|
|
|
- generatePresignedUrlRequest.setExpiration(expiration);
|
|
|
- URL url = clientWw.generatePresignedUrl(generatePresignedUrlRequest);
|
|
|
- return url.toString();
|
|
|
+
|
|
|
+ public boolean uploadFile(String ossObjectKey, InputStream inputStream) {
|
|
|
+ PutObjectResult putObjectResult = buildClient().putObject(new PutObjectRequest(OSS_BUCKET_NAME, ossObjectKey, inputStream));
|
|
|
+ log.info("putObjectResult" + putObjectResult);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
+ public String getUrlWw(String key) {
|
|
|
+ return getUrlWw(key, null);
|
|
|
+ }
|
|
|
|
|
|
- public static String getUrlWw(String key, MediaType mediaType) {
|
|
|
+ public String getUrlWw(String key, MediaType mediaType) {
|
|
|
if (StringUtils.isEmpty(key)) {
|
|
|
return "";
|
|
|
}
|
|
|
// 设置URL过期时间为1小时
|
|
|
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
|
|
|
- GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(ALIOSS_BUCKETNAME, key);
|
|
|
+ GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(OSS_BUCKET_NAME, key);
|
|
|
request.setExpiration(expiration);
|
|
|
if (mediaType != null) {
|
|
|
request.setContentType(mediaType.toString());
|
|
|
}
|
|
|
|
|
|
URL url = new OSSClientBuilder()
|
|
|
- .build("https://" + AliOSSEndPointNohttp, Constant.Ali.ACCESS_KEY_ID, Constant.Ali.ACCESS_KEY_SECERT)
|
|
|
+ .build("https://" + OSS_ENDPOINT_WW, ACCESS_KEY_ID, ACCESS_KEY_SECERT)
|
|
|
.generatePresignedUrl(request);
|
|
|
|
|
|
String resultUrl = url.toString();
|
|
|
|
|
|
return resultUrl;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|