|
|
@@ -1,5 +1,6 @@
|
|
|
package com.ytpm.middle.util;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.aliyun.oss.ClientBuilderConfiguration;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
@@ -8,36 +9,20 @@ import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
|
|
import com.aliyun.oss.common.auth.DefaultCredentials;
|
|
|
import com.aliyun.oss.common.comm.Protocol;
|
|
|
import com.aliyun.oss.common.comm.SignVersion;
|
|
|
-import com.aliyun.oss.model.Bucket;
|
|
|
-import com.aliyun.oss.model.DeleteObjectsRequest;
|
|
|
-import com.aliyun.oss.model.DeleteObjectsResult;
|
|
|
-import com.aliyun.oss.model.GetObjectRequest;
|
|
|
-import com.aliyun.oss.model.ListObjectsRequest;
|
|
|
-import com.aliyun.oss.model.OSSObject;
|
|
|
-import com.aliyun.oss.model.OSSObjectSummary;
|
|
|
-import com.aliyun.oss.model.ObjectListing;
|
|
|
-import com.aliyun.oss.model.ObjectMetadata;
|
|
|
import com.aliyun.oss.model.PutObjectRequest;
|
|
|
-import com.aliyun.oss.model.PutObjectResult;
|
|
|
-import com.aliyun.oss.model.UploadFileRequest;
|
|
|
+import com.aliyun.sls20201230.models.GetLogsResponse;
|
|
|
+import com.aliyun.tea.TeaException;
|
|
|
+import com.ytpm.util.DateUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
-import java.net.URL;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Base64;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
+@Slf4j(topic = "AliOSS")
|
|
|
@Component
|
|
|
@RefreshScope
|
|
|
public class AliOSSUtil {
|
|
|
@@ -70,18 +55,30 @@ public class AliOSSUtil {
|
|
|
bucketName = bucketNameCon;
|
|
|
}
|
|
|
|
|
|
+ private static String projectName;
|
|
|
+ @Value("${oss.aliyun.bucketName}")
|
|
|
+ public static void setProjectName(String projectName) {
|
|
|
+ AliOSSUtil.projectName = projectName;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 针对MultipartFile类型
|
|
|
- *
|
|
|
- * @param multipartFile 前端传回的MultipartFile类型的数据
|
|
|
- * @return java.lang.String 返回该文件的访问链接
|
|
|
- */
|
|
|
- public static String upload(MultipartFile multipartFile, String fileName) throws IOException {
|
|
|
- // 获取上传的文件的输入流
|
|
|
- InputStream inputStream = multipartFile.getInputStream();
|
|
|
- return upload(inputStream, fileName);
|
|
|
+ private static String logstoreName;
|
|
|
+ @Value("${oss.aliyun.logstoreName}")
|
|
|
+ public static void setLogstoreName(String logstoreName) {
|
|
|
+ AliOSSUtil.logstoreName = logstoreName;
|
|
|
}
|
|
|
+
|
|
|
+ private static String logUrl;
|
|
|
+ @Value("${oss.aliyun.logUrl}")
|
|
|
+ public static void setLogUrl(String logUrl) {
|
|
|
+ AliOSSUtil.logUrl = logUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String querySql;
|
|
|
+ @Value("${oss.aliyun.querySql}")
|
|
|
+ public static void setQuerySql(String querySql) {
|
|
|
+ AliOSSUtil.querySql = querySql;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 上传广告平台文件
|
|
|
*/
|
|
|
@@ -116,260 +113,81 @@ public class AliOSSUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 针对Base64类型
|
|
|
- *
|
|
|
- * @param base64 Base64字符串,标准的Base64字符串,例如data:image/png;base64,iVBORw0...
|
|
|
- * @return java.lang.String 返回该文件的访问链接
|
|
|
- */
|
|
|
- public static String upload(String base64) {
|
|
|
- // 获取上传的文件的输入流
|
|
|
- byte[] bytes = Base64.getDecoder().decode(base64.split(",")[1]);
|
|
|
- InputStream inputStream = new ByteArrayInputStream(bytes);
|
|
|
- // 获取文件名称
|
|
|
- String fileName = base64.substring(base64.indexOf(":") + 1, base64.indexOf(";")).replace("/", ".");
|
|
|
-
|
|
|
- return upload(inputStream, fileName);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询所有文件名称
|
|
|
- *
|
|
|
- * @param path 相对于阿里云bucket存储空间的文件路径
|
|
|
- * @return java.util.List<java.lang.String> 该路径下所有文件的名称
|
|
|
- * <p>
|
|
|
- * 注:首先会获取根文件的名称
|
|
|
- */
|
|
|
- public static List<String> listFileName(String path) {
|
|
|
- List<String> res = new ArrayList<>();
|
|
|
- // 构造ListObjectsRequest请求。
|
|
|
- ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
|
|
|
-
|
|
|
- // 设置prefix参数来获取fun目录下的所有文件。
|
|
|
- listObjectsRequest.setPrefix(path);
|
|
|
-
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
- // 列出文件。
|
|
|
- ObjectListing listing = ossClient.listObjects(listObjectsRequest);
|
|
|
- // 遍历所有文件
|
|
|
- for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
|
|
|
- System.out.println(objectSummary.getKey());
|
|
|
- }
|
|
|
- // 关闭OSSClient。
|
|
|
- ossClient.shutdown();
|
|
|
- return res;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询所有文件的url
|
|
|
- *
|
|
|
- * @param path 相对于阿里云bucket存储空间的文件路径
|
|
|
- * @return java.util.List<java.lang.String> 返回路径下所有文件的url
|
|
|
- * <p>
|
|
|
- * 注:首先会获取根文件夹的url,且根文件夹的url无法访问
|
|
|
- */
|
|
|
- public static List<String> listFileUrl(String path) {
|
|
|
- List<String> res = new ArrayList<>();
|
|
|
-
|
|
|
- // 构造ListObjectsRequest请求
|
|
|
- ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
|
|
|
-
|
|
|
- // 设置prefix参数来获取fun目录下的所有文件。
|
|
|
- listObjectsRequest.setPrefix(path);
|
|
|
-
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
- // 列出文件。
|
|
|
- ObjectListing listing = ossClient.listObjects(listObjectsRequest);
|
|
|
- // 遍历所有文件。
|
|
|
-
|
|
|
- for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
|
|
|
- //文件访问路径
|
|
|
- Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
|
|
|
- URL url = ossClient.generatePresignedUrl(bucketName, objectSummary.getKey(), expiration);
|
|
|
- res.add(url.toString());
|
|
|
- }
|
|
|
- // 关闭OSSClient。
|
|
|
- ossClient.shutdown();
|
|
|
- return res;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断文件是否存在
|
|
|
- *
|
|
|
- * @param objectName 相对于阿里云bucket存储空间的文件路径和文件名称
|
|
|
- * @return boolean 存在返回true,存在返回false
|
|
|
- */
|
|
|
- public static boolean isFileExist(String objectName) {
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
- boolean res = ossClient.doesObjectExist(bucketName, objectName);
|
|
|
- return res;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过文件名称(包括路径)下载文件
|
|
|
- *
|
|
|
- * @param objectName 相对于阿里云bucket存储空间的文件路径和文件名称
|
|
|
- * @param localFileName 下载的路径和文件名称
|
|
|
- */
|
|
|
- public static void downloadFile(String objectName, String localFileName) {
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
- // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
|
|
|
- ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(localFileName));
|
|
|
- // 关闭OSSClient。
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除文件或目录
|
|
|
- *
|
|
|
- * @param objectName 相对于阿里云bucket存储空间的文件路径和文件名称
|
|
|
+ * 初始化客户端
|
|
|
*/
|
|
|
- public static void deleteFile(String objectName) {
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
- ossClient.deleteObject(bucketName, objectName);
|
|
|
- ossClient.shutdown();
|
|
|
+ public static com.aliyun.sls20201230.Client createClient() throws Exception {
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
|
+ .setAccessKeyId("LTAI5tQQ57pTXeFHacavoJMq")
|
|
|
+ .setAccessKeySecret("TZX8QPD81w1magxBxVpnRJ3NFHEAoS");
|
|
|
+ config.endpoint = "cn-nanjing.log.aliyuncs.com";
|
|
|
+ return new com.aliyun.sls20201230.Client(config);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 批量删除文件或目录
|
|
|
- *
|
|
|
- * @param keys 相对于阿里云bucket存储空间的文件路径和文件名称
|
|
|
+ * 根据传入时间分析查询日志
|
|
|
*/
|
|
|
- public static void deleteFiles(List<String> keys) {
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
- // 删除文件。
|
|
|
- DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
|
|
|
- List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
|
|
|
-
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 创建文件夹
|
|
|
- *
|
|
|
- * @param folder 文件夹名称。注:文件夹名称后面需要添加“/”,否则创建的是文件,并非文件夹。
|
|
|
- * @return java.lang.String 相对于阿里云bucket存储空间的文件夹路径
|
|
|
- */
|
|
|
- public static String createFolder(String folder) {
|
|
|
- // 文件夹名
|
|
|
- final String keySuffixWithSlash = folder;
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
- // 判断文件夹是否存在,不存在则创建
|
|
|
- if (!ossClient.doesObjectExist(bucketName, keySuffixWithSlash)) {
|
|
|
- // 创建文件夹
|
|
|
- ossClient.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
|
|
|
- // 得到文件夹名
|
|
|
- OSSObject object = ossClient.getObject(bucketName, keySuffixWithSlash);
|
|
|
- String fileDir = object.getKey();
|
|
|
- ossClient.shutdown();
|
|
|
- return fileDir;
|
|
|
- }
|
|
|
-
|
|
|
- return keySuffixWithSlash;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 文件上传,文件上传后会将文件放置在当前日期的目录中,如:2022-07-08/1_1657251247153.png
|
|
|
- *
|
|
|
- * @param inputStream 上传文件的流
|
|
|
- * @param fileName 文件名称,命名规则是:文件名+当前时间戳+后缀
|
|
|
- * @return java.lang.String 返回该文件的访问链接
|
|
|
- */
|
|
|
- private static String upload(InputStream inputStream, String fileName) {
|
|
|
- // 避免文件被覆盖,为上传文件名称添加时间戳
|
|
|
- int i = fileName.lastIndexOf(".");
|
|
|
- String suffix = fileName.substring(i);
|
|
|
- fileName = fileName.substring(0, i) + "-" + System.currentTimeMillis() + suffix;
|
|
|
-
|
|
|
- // 如果需要上传时设置存储类型与访问权限
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- metadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));
|
|
|
-
|
|
|
- // 上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
|
|
|
- PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
|
|
|
- putObjectRequest.setMetadata(metadata);
|
|
|
-
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
- ossClient.putObject(putObjectRequest);
|
|
|
-
|
|
|
- // 关闭ossClient
|
|
|
- ossClient.shutdown();
|
|
|
- // 把上传到oss的路径返回
|
|
|
- return fileName;
|
|
|
- }
|
|
|
-
|
|
|
- public static String upload1(InputStream inputStream, String fileName) {
|
|
|
- // 避免文件被覆盖,为上传文件名称添加时间戳
|
|
|
- fileName = fileName + "-" + System.currentTimeMillis();
|
|
|
-
|
|
|
- // 2. 设置文件的 Content-Type
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- metadata.setContentType(getContentType("png"));
|
|
|
-
|
|
|
- // 3. 上传文件到OSS,指定文件路径(bucketName 是 OSS 存储桶名称)
|
|
|
- PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
|
|
|
- putObjectRequest.setMetadata(metadata);
|
|
|
-
|
|
|
- // 4. 创建 OSS 客户端
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
-
|
|
|
- // 5. 上传文件
|
|
|
- ossClient.putObject(putObjectRequest);
|
|
|
-
|
|
|
- // 6. 关闭 OSS 客户端
|
|
|
- ossClient.shutdown();
|
|
|
- // 把上传到oss的路径返回
|
|
|
- return fileName;
|
|
|
- }
|
|
|
-
|
|
|
- private static String getContentType(String FileNameExtension) {
|
|
|
- if (FileNameExtension.equalsIgnoreCase(".bmp")) {
|
|
|
- return "image/bmp";
|
|
|
- }
|
|
|
- if (FileNameExtension.equalsIgnoreCase(".gif")) {
|
|
|
- return "image/gif";
|
|
|
- }
|
|
|
- if (FileNameExtension.equalsIgnoreCase(".jpeg") ||
|
|
|
- FileNameExtension.equalsIgnoreCase(".jpg") ||
|
|
|
- FileNameExtension.equalsIgnoreCase(".png")
|
|
|
- ) {
|
|
|
- return "image/jpg";
|
|
|
+ public static Map<String, Integer> getCountDownload(int from , int to){
|
|
|
+ if(from > to)return new HashMap<>();
|
|
|
+ try {
|
|
|
+ com.aliyun.sls20201230.Client client = AliOSSUtil.createClient();
|
|
|
+ com.aliyun.sls20201230.models.GetLogsRequest getLogsRequest = new com.aliyun.sls20201230.models.GetLogsRequest()
|
|
|
+ .setFrom(from)
|
|
|
+ .setTo(to)
|
|
|
+ .setQuery("* and __topic__: oss_access_log and bucket: yt-advise|SELECT object, count(1) pv from log where object like '%.apk' group by object order by pv Desc");
|
|
|
+ com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
|
|
+ java.util.Map<String, String> headers = new java.util.HashMap<>();
|
|
|
+ // 复制代码运行请自行打印 API 的返回值
|
|
|
+ GetLogsResponse logsWithOptions = client.getLogsWithOptions("oss-log-1694777633719600-cn-nanjing", "oss-log-store", getLogsRequest, headers, runtime);
|
|
|
+ Map<String, Integer> map = new HashMap<>();
|
|
|
+ for (Map<String, ?> stringMap : logsWithOptions.getBody()) {
|
|
|
+ String key = stringMap.get("object").toString();
|
|
|
+ map.put(key.substring(key.lastIndexOf("/")+1).toLowerCase(Locale.ROOT), Integer.parseInt(stringMap.get("pv").toString()));
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ } catch (TeaException error) {
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
+ // 错误 message
|
|
|
+ log.error(error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ log.error(error.getData().get("Recommend").toString());
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
+ } catch (Exception _error) {
|
|
|
+ TeaException error = new TeaException(_error.getMessage(), _error);
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
+ // 错误 message
|
|
|
+ log.error(error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ log.error(error.getData().get("Recommend").toString());
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
}
|
|
|
- return "image/jpg";
|
|
|
+ return new HashMap<>();
|
|
|
}
|
|
|
|
|
|
- public static String uploadToOSS(BufferedImage qrImage, String fileName) {
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+// getCountDownload(Math.toIntExact(DateUtil.getTodayStart().getTime() / 1000), (int) (System.currentTimeMillis()/1000));
|
|
|
+ com.aliyun.sls20201230.Client client = AliOSSUtil.createClient();
|
|
|
+ com.aliyun.sls20201230.models.GetLogsRequest getLogsRequest = new com.aliyun.sls20201230.models.GetLogsRequest();
|
|
|
+ com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
|
|
+ java.util.Map<String, String> headers = new java.util.HashMap<>();
|
|
|
try {
|
|
|
- // 设置文件的 MIME 类型
|
|
|
- String fileExtension = fileName.substring(fileName.lastIndexOf("."));
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- metadata.setContentType(getContentType(fileExtension)); // 设置上传文件的 MIME 类型
|
|
|
-
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(qrImage, "PNG", baos);
|
|
|
-
|
|
|
- ByteArrayInputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
|
|
|
-
|
|
|
- // 使用 OSSClient 上传文件
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
- PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
|
|
|
- putObjectRequest.setMetadata(metadata); // 设置文件的元数据
|
|
|
- ossClient.putObject(putObjectRequest);
|
|
|
-
|
|
|
- inputStream.close();
|
|
|
- ossClient.shutdown();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ // 复制代码运行请自行打印 API 的返回值
|
|
|
+ client.getLogsWithOptions("", "", getLogsRequest, headers, runtime);
|
|
|
+ } catch (TeaException error) {
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
+ // 错误 message
|
|
|
+ System.out.println(error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ System.out.println(error.getData().get("Recommend"));
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
+ } catch (Exception _error) {
|
|
|
+ TeaException error = new TeaException(_error.getMessage(), _error);
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
+ // 错误 message
|
|
|
+ System.out.println(error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ System.out.println(error.getData().get("Recommend"));
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
}
|
|
|
-
|
|
|
- // 返回上传到OSS的路径
|
|
|
- return "https://" + bucketName + "." + endpoint + "/" + fileName;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
+
|