123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- package com.fdkankan.fyun.oss;
- import com.aliyun.oss.OSSClient;
- import com.aliyun.oss.model.OSSObjectSummary;
- import com.aliyun.oss.model.ObjectListing;
- import com.aliyun.oss.model.ObjectMetadata;
- import com.aliyun.oss.model.PutObjectResult;
- import com.amazonaws.HttpMethod;
- import com.amazonaws.auth.AWSStaticCredentialsProvider;
- import com.amazonaws.auth.BasicAWSCredentials;
- import com.amazonaws.regions.Regions;
- import com.amazonaws.services.s3.AmazonS3;
- import com.amazonaws.services.s3.AmazonS3ClientBuilder;
- import com.amazonaws.services.s3.model.CannedAccessControlList;
- import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
- import com.amazonaws.services.s3.model.GetObjectRequest;
- import com.amazonaws.services.s3.model.PutObjectRequest;
- import com.qiniu.common.Zone;
- import com.qiniu.storage.Configuration;
- import com.qiniu.storage.UploadManager;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.fileupload.FileItem;
- import org.apache.commons.fileupload.FileItemFactory;
- import org.apache.commons.fileupload.disk.DiskFileItemFactory;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import org.springframework.web.multipart.MultipartFile;
- import org.springframework.web.multipart.commons.CommonsMultipartFile;
- import java.io.*;
- import java.net.FileNameMap;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Slf4j
- @Component
- public class UploadToOssUtil {
- Zone zone = Zone.autoZone();
- Configuration config = new Configuration(zone);
- UploadManager uploadManager = new UploadManager(config);
- @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;
- @Value("${oss.sdk:4dscene}")
- private String bucketSdk;
- @Value("${oss.type:oss}")
- private String type;
- @Value("${oss.s3key:AKIAWCV5QFZ3ZNELKYUY}")
- private String s3key;
- @Value("${oss.s3secrey:epS5ghyR4LJ7rxk/qJO9ZYh6m9Oz6g5haKDu4yws}")
- private String s3secrey;
- @Value("${oss.s3bucket:4dkankan}")
- private String s3bucket;
- public void delete(String key1) throws IOException{
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try {
- // bucketMgr.delete(bucketname, key);
- // 2019-2-28 启动aliyun oss 空间
- ossClient.deleteObject(bucket, key1);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //上传的数据是byte[],key是上传后的文件名
- public void upload(byte[] data,String key1) throws IOException{
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try
- {
- //bucketMgr.delete(bucketname, key);
- //Response res = uploadManager.put(data, key, getUpToken(key));
- // 2019-2-28 启动aliyun oss 空间
- ossClient.putObject(bucket, key1, new ByteArrayInputStream(data));
- //log.info(res.bodyString());
- } catch (Exception e) {
- log.error(e.toString()+key1);
- }
- }
- public void upload(String filePath, String key1) {
- log.info("开始上传文件 源路径:{},目标路径:{},point:{}" , filePath,key1,point);
- if("oss".equals(type)){
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try {
- File file = new File(filePath);
- if (!file.exists()) {
- log.error("要上传的文件不存在:" + filePath);
- }
- ObjectMetadata metadata = new ObjectMetadata();
- if(filePath.contains(".jpg")){
- metadata.setContentType("image/jpeg");
- }
- ossClient.putObject(bucket, key1, new File(filePath), metadata);
- } catch (Exception e) {
- log.error(e.toString() + filePath);
- }
- }
- if("s3".equals(type)){
- try{
- uploadS3File(filePath, key1);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- public void uploadSdk(String filePath, String key1) {
- if("oss".equals(type)){
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try {
- File file = new File(filePath);
- if (!file.exists()) {
- log.error("要上传的文件不存在:" + filePath);
- }
- // 调用put方法上传
- // Response res = uploadManager.put(FilePath, key, getUpToken(key));
- // 打印返回的信息
- // log.info(res.bodyString());
- // 2019-2-28 启动aliyun oss 空间
- // ObjectMetadata meta = new ObjectMetadata();
- // meta.setCacheControl("no-cache");
- // ossClient.putObject(BUCKET_NAME, key, new File(filePath), meta);
- ObjectMetadata metadata = new ObjectMetadata();
- if(filePath.contains(".jpg")){
- metadata.setContentType("image/jpeg");
- }
- ossClient.putObject(bucketSdk, key1, new File(filePath), metadata);
- } catch (Exception e) {
- log.error(e.toString() + filePath);
- }
- }
- if("s3".equals(type)){
- try{
- uploadS3File(filePath, key1);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- public void upload2(String filePath, String key1) {
- log.info("开始上传文件 源路径:{},目标路径:{},point:{}" , filePath,key1,point);
- if("oss".equals(type)){
- OSSClient ossClient = new OSSClient(point, key, secrey);
- try {
- // 调用put方法上传
- // Response res = uploadManager.put(FilePath, key, getUpToken(key));
- // 打印返回的信息
- // log.info(res.bodyString());
- // 2019-2-28 启动aliyun oss 空间
- ObjectMetadata metadata = new ObjectMetadata();
- if(filePath.contains(".jpg")){
- metadata.setContentType("image/jpeg");
- }
- if(filePath.contains(".mp4")){
- metadata.setContentType("video/mp4");
- }
- if(filePath.contains(".mp3")){
- metadata.setContentType("audio/mp3");
- }
- ossClient.putObject(bucket, key1, new File(filePath), metadata);
- } catch (Exception e) {
- log.error(e.toString() + filePath);
- }
- }
- if("s3".equals(type)){
- try{
- uploadS3File(filePath, key1);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- //上传的数据是文件夹,参数是文件夹路径,key是上传后的文件名
- public void uploadMulFiles(Map<String, String> filepaths) {
- if (filepaths == null) {
- return;
- }
- Long start = System.currentTimeMillis();
- log.info("开始批量上传文件:");
- if (filepaths.size() > 50) {
- filepaths.entrySet().parallelStream().forEach(entry->{
- upload2(entry.getKey(), entry.getValue());
- });
- } else {
- filepaths.entrySet().parallelStream().forEach(entry->{
- upload(entry.getKey(), entry.getValue());
- });
- }
- log.info("批量上传文件结束,用时:{}" ,(System.currentTimeMillis() - start));
- }
- public int deleteFile(String prefix){
- if("oss".equals(type)){
- OSSClient ossClient = new OSSClient(point, key, secrey);
- ObjectListing objectListing = ossClient.listObjects(bucket, prefix);
- List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
- try {
- for (OSSObjectSummary s : sums) {
- delete(s.getKey());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if("s3".equals(type)){
- deleteS3Object(prefix);
- }
- return 1;
- }
- public Map<String, String> getUploadS3Url(List<String> urls){
- if(urls == null || urls.size() <= 0){
- return null;
- }
- BasicAWSCredentials awsCred = new BasicAWSCredentials(s3key, s3secrey);
- AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
- .withCredentials(new AWSStaticCredentialsProvider(awsCred))
- .withRegion(Regions.EU_WEST_2)
- .build();
- // Set the pre-signed URL to expire after one hour.
- java.util.Date expiration = new java.util.Date();
- long expTimeMillis = expiration.getTime();
- expTimeMillis += 1000 * 60 * 60 * 8;
- expiration.setTime(expTimeMillis);
- //生成预签名URL
- log.info("生成预签名URL");
- GeneratePresignedUrlRequest generatePresignedUrlRequest = null;
- URL url = null;
- Map<String, String> map = new HashMap();
- for(String path : urls){
- // if(path.contains(".jpg") || path.contains("png")){
- // generatePresignedUrlRequest = new GeneratePresignedUrlRequest(s3bucket, path)
- // .withMethod(HttpMethod.PUT)
- // .withExpiration(expiration)
- // .withContentType("image/jpeg");
- // }else {
- generatePresignedUrlRequest = new GeneratePresignedUrlRequest(s3bucket, path)
- .withMethod(HttpMethod.PUT)
- .withExpiration(expiration);
- // }
- url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
- map.put(path, url.toString());
- }
- return map;
- }
- public String upload5(String filePath, String key1) {
- OSSClient ossClient = new OSSClient(point, key, secrey);
- PutObjectResult result = null;
- try {
- File file = new File(filePath);
- if (!file.exists()) {
- log.error("要上传的文件不存在:" + filePath);
- }
- result = ossClient.putObject(bucket, key1, new File(filePath));
- } catch (Exception e) {
- log.error(e.toString() + filePath);
- }
- log.info(" getETag : " + result.getETag());
- log.info("1 : " + result.toString());
- log.info("2 : " + result.getRequestId());
- log.info("3 : " + result.getClientCRC());
- log.info("4 : " + result.getResponse());
- log.info("5 : " + result.getServerCRC());
- return result.getETag();
- }
- //海外亚马逊s3
- /**
- * s3上传文件流
- *
- * @param file 文件
- * @param updatePath 上传路径[ eg: xxx/xxx ]
- */
- public String updateS3LoadFile(MultipartFile file, String updatePath) {
- if (isEmpty(file)) {
- return null;
- }
- /**
- * 创建s3对象
- */
- BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
- AmazonS3 s3 = AmazonS3ClientBuilder.standard()
- .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
- .withRegion(Regions.EU_WEST_2)
- .build();
- try {
- // 创建临时文件,程序运行结束,会自动删除
- File localFile = File.createTempFile("temp", null);
- // 把文件写入内存中
- file.transferTo(localFile);
- // 指定要上传到服务器上的路径
- String key = updatePath;
- // 设置文件并设置公读
- PutObjectRequest request = new PutObjectRequest(s3bucket, key, localFile);
- request.withCannedAcl(CannedAccessControlList.PublicRead);
- // 上传文件
- com.amazonaws.services.s3.model.PutObjectResult putObjectResult = s3.putObject(request);
- if (StringUtils.isNotEmpty(putObjectResult.getETag())) {
- System.out.println("success");
- return key;
- }
- return null;
- } catch (IOException e) {
- }
- return null;
- }
- /**
- * s3上传文件
- * @param filePath
- * @param key1
- * @throws IOException
- */
- private void uploadS3File(String filePath, String key1) throws IOException {
- File file = new File(filePath);
- if(!file.exists()){
- log.info("要上传s3的文件不存在");
- return;
- }
- /**
- * 创建s3对象
- */
- BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
- AmazonS3 s3 = AmazonS3ClientBuilder.standard()
- .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
- .withRegion(Regions.EU_WEST_2)
- .build();
- // 设置文件并设置公读
- com.amazonaws.services.s3.model.ObjectMetadata metadata = new com.amazonaws.services.s3.model.ObjectMetadata();
- if(filePath.contains(".jpg")){
- metadata.setContentType("image/jpeg");
- }
- if(filePath.contains(".png")){
- metadata.setContentType("image/png");
- }
- PutObjectRequest request = new PutObjectRequest(s3bucket, key1, file);
- request.withCannedAcl(CannedAccessControlList.PublicRead);
- request.withMetadata(metadata);
- // 上传文件
- com.amazonaws.services.s3.model.PutObjectResult putObjectResult = s3.putObject(request);
- if (StringUtils.isNotEmpty(putObjectResult.getETag())) {
- log.info("s3上传文件成功:" + key1);
- }
- }
- /**
- * 删除单个文件
- *
- * @param filePath 文件路径[ eg: /head/xxxx.jpg ]
- * @return
- */
- public void deleteS3Object(String filePath) {
- /**
- * 创建s3对象
- */
- BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
- AmazonS3 s3 = AmazonS3ClientBuilder.standard()
- .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
- .withRegion(Regions.EU_WEST_2)
- .build();
- if (filePath.startsWith("/")) {
- filePath = filePath.substring(1);
- }
- try {
- s3.deleteObject(s3bucket, filePath);
- } catch (Exception e) {
- }
- }
- /**
- * 获取文件类型
- */
- public static String getContentType(String filePath){
- FileNameMap fileNameMap = URLConnection.getFileNameMap();
- String contentType = fileNameMap.getContentTypeFor(filePath);
- System.out.println(contentType);
- return contentType;
- }
- /**
- * 检查文件是否为空
- *
- * @param imageFile
- * @return
- */
- private static boolean isEmpty(MultipartFile imageFile) {
- if (imageFile == null || imageFile.getSize() <= 0) {
- return true;
- }
- return false;
- }
- private static MultipartFile getMulFileByPath(String picPath) {
- FileItem fileItem = createFileItem(picPath);
- MultipartFile mfile = new CommonsMultipartFile(fileItem);
- return mfile;
- }
- private static FileItem createFileItem(String filePath)
- {
- FileItemFactory factory = new DiskFileItemFactory(16, null);
- String textFieldName = "textField";
- int num = filePath.lastIndexOf(".");
- String extFile = filePath.substring(num);
- FileItem item = factory.createItem(textFieldName, "text/plain", true,
- "MyFileName" + extFile);
- File newfile = new File(filePath);
- int bytesRead = 0;
- byte[] buffer = new byte[8192];
- try
- {
- FileInputStream fis = new FileInputStream(newfile);
- OutputStream os = item.getOutputStream();
- while ((bytesRead = fis.read(buffer, 0, 8192))
- != -1)
- {
- os.write(buffer, 0, bytesRead);
- }
- os.close();
- fis.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- return item;
- }
- /**
- * 下载文件
- * @param bucketName 桶名
- * @param remoteFileName 文件名
- * @param path 下载路径
- */
- public boolean downFromS3(String bucketName, String remoteFileName, String path) {
- try {
- /**
- * 创建s3对象
- */
- BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
- AmazonS3 s3 = AmazonS3ClientBuilder.standard()
- .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
- .withRegion(Regions.EU_WEST_2)
- .build();
- GetObjectRequest request = new GetObjectRequest(bucketName,remoteFileName);
- s3.getObject(request,new File(path));
- return true;
- } catch (Exception ase) {
- log.error("amazonS3下载文件异常 " + ase.getMessage(), ase);
- }
- return false;
- }
- }
|