123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package com.fdkankan.site.common.util;
- import cn.hutool.core.collection.CollUtil;
- import com.aliyun.oss.OSSClient;
- import com.aliyun.oss.model.*;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- @Slf4j
- @Component
- public class UploadToOssUtil {
- @Value("${oss.point:http://oss-cn-shenzhen-internal.aliyuncs.com}")
- private String point;
- @Value("${oss.key:LTAIUrvuHqj8pvry}")
- private String key;
- @Value("${oss.secrey:JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4}")
- private String secrey;
- @Value("${oss.bucket:4dkankan}")
- private String bucket;
- /**
- * 获取文件内容-阿里云
- * @param objectName
- * @return
- */
- public boolean existKey(String objectName){
- //创建oss客户端
- OSSClient ossClient = new OSSClient(point, key, secrey);
- // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
- try{
- boolean exist = ossClient.doesObjectExist(bucket, objectName);
- return exist;
- }catch (Exception e){
- log.error("s4判断是否存在key异常,key=" + objectName, e);
- }finally {
- if(ossClient != null){
- ossClient.shutdown();
- }
- }
- return false;
- }
- /**
- * 从阿里云oss下载文件到本地
- * @param objectName 云端文件k地址
- * @param localPath 本地文件地址
- * @return
- */
- public boolean downFormAli(String objectName, String localPath){
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try {
- GetObjectRequest request = new GetObjectRequest(bucket,objectName);
- File file = new File(localPath);
- if(!file.getParentFile().exists()){
- file.getParentFile().mkdirs();
- file = new File(localPath);
- }
- ossClient.getObject(request, file);
- return true;
- }catch (Exception e){
- log.error("阿里云oss文件下载失败,key=" + objectName, e);
- }finally {
- if(ossClient != null){
- ossClient.shutdown();
- }
- }
- return false;
- }
- public void uploadOss(String filePath, String key1){
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try {
- log.info("upload-to-oss:file-path:{},oss-path:{}",filePath,key1);
- File file = new File(filePath);
- if (!file.exists()) {
- log.info("upload-to-oss:file-path:{},oss-path:{},filePath不存在!",filePath,key1);
- return;
- }
- ObjectMetadata metadata = new ObjectMetadata();
- ossClient.putObject(bucket, key1, new File(filePath), metadata);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- ossClient.shutdown();
- }
- }
- public void delete(String objectName){
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try {
- ossClient.deleteObject(bucket, objectName);
- } catch (Exception e) {
- log.error("OSS删除文件失败,key=" + objectName);
- }finally {
- if(ossClient != null){
- ossClient.shutdown();
- }
- }
- }
- /**
- * 获得文件列表-阿里云
- * @return
- */
- public List<String> listKeysFromAli(String sourcePath) {
- List<String> keyList = new ArrayList<>();
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try {
- boolean flag = true;
- String nextMaker = null;
- ListObjectsRequest listObjectsRequest = new ListObjectsRequest(this.bucket);
- //指定下一级文件
- listObjectsRequest.setPrefix(sourcePath);
- //设置分页的页容量
- listObjectsRequest.setMaxKeys(200);
- do
- {
- //获取下一页的起始点,它的下一项
- listObjectsRequest.setMarker(nextMaker);
- ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
- List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
- List<String> collect = objectSummaries.stream().map(summary -> {
- return summary.getKey();
- }).collect(Collectors.toList());
- if(CollUtil.isNotEmpty(collect)){
- keyList.addAll(collect);
- }
- nextMaker = objectListing.getNextMarker();
- //全部执行完后,为false
- flag = objectListing.isTruncated();
- } while (flag);
- }catch (Exception e){
- log.error("获取文件列表失败,path="+sourcePath, e);
- }finally {
- if(ossClient != null){
- ossClient.shutdown();
- }
- }
- ossClient.shutdown();
- return keyList;
- }
- /**
- * 获取文件内容-阿里云
- * @param bucketName
- * @param objectName
- * @return
- */
- public String getObjectContent(String bucketName, String objectName){
- //创建oss客户端
- OSSClient ossClient = new OSSClient(point, key, secrey);
- InputStream objectContent = null;
- StringBuilder contentJson = new StringBuilder();
- try {
- // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
- OSSObject ossObject = ossClient.getObject(bucketName, objectName);
- objectContent = ossObject.getObjectContent();
- try(BufferedReader reader = new BufferedReader(new InputStreamReader(objectContent))){
- while (true) {
- String line = reader.readLine();
- if (line == null) break;
- contentJson.append(line);
- }
- } catch (IOException e) {
- log.error("读取scene.json文件流失败", e);
- }
- }catch (Exception e){
- log.error("s3获取文件内容失败,key="+objectName, e);
- }finally {
- if(ossClient != null){
- ossClient.shutdown();
- }
- }
- return contentJson.toString();
- }
- public void uploadFileOss(File file) {
- if(file.isFile()){
- String ossPath = file.getPath();
- ossPath = ossPath.replace("/mnt/","");
- ossPath = ossPath.replace("\\","/");
- this.uploadOss(file.getPath(),ossPath);
- }else {
- File[] files = file.listFiles();
- for (File file1 : files) {
- uploadFileOss(file1);
- }
- }
- }
- }
|