| | |
| | | package com.doumee.core.utils.aliyun; |
| | | |
| | | import com.aliyun.auth.credentials.Credential; |
| | | import com.aliyun.auth.credentials.provider.StaticCredentialProvider; |
| | | import com.aliyun.oss.ClientException; |
| | | import com.aliyun.oss.OSSClient; |
| | | import com.aliyun.oss.OSSException; |
| | | import com.aliyun.oss.event.ProgressEvent; |
| | | import com.aliyun.oss.event.ProgressEventType; |
| | | import com.aliyun.oss.event.ProgressListener; |
| | | import com.aliyun.oss.model.*; |
| | | import com.aliyun.sdk.service.cloudauth20190307.AsyncClient; |
| | | import com.aliyun.sdk.service.cloudauth20190307.models.Id2MetaVerifyRequest; |
| | | import com.aliyun.sdk.service.cloudauth20190307.models.Id2MetaVerifyResponse; |
| | | import com.doumee.core.utils.FileDigest; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.CompletableFuture; |
| | | |
| | | import com.google.gson.Gson; |
| | | import darabonba.core.client.ClientOverrideConfiguration; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | |
| | | return filename; |
| | | } |
| | | |
| | | public static void main(String[] args) throws OSSException, |
| | | ClientException, IOException { |
| | | ALiYunUtil aLiYunUtil = new ALiYunUtil("", "uc4nnpsqep1i9fijqr37nokh", |
| | | "/rp41xCx/XdGEVCptdH6v7xpc9w="); |
| | | // aLiYunUtil.uploadObject("pongto", "work/li2.txt", "D://哈.txt", |
| | | // ".html,.html text/html");D://装机软件/办公学习 |
| | | // aLiYunUtil.partUploadObject("pongto", "work/ps.exe", |
| | | // "D://装机软件/办公学习/Adobe_Illustrator_CS6_XiaZaiBa.exe", |
| | | // ".html,.html text/html"); |
| | | // aLiYunUtil.deleteBucket("pongto"); |
| | | } |
| | | // public static void main(String[] args) throws OSSException, |
| | | // ClientException, IOException { |
| | | // ALiYunUtil aLiYunUtil = new ALiYunUtil("", "uc4nnpsqep1i9fijqr37nokh", |
| | | // "/rp41xCx/XdGEVCptdH6v7xpc9w="); |
| | | // // aLiYunUtil.uploadObject("pongto", "work/li2.txt", "D://哈.txt", |
| | | // // ".html,.html text/html");D://装机软件/办公学习 |
| | | // // aLiYunUtil.partUploadObject("pongto", "work/ps.exe", |
| | | // // "D://装机软件/办公学习/Adobe_Illustrator_CS6_XiaZaiBa.exe", |
| | | // // ".html,.html text/html"); |
| | | // // aLiYunUtil.deleteBucket("pongto"); |
| | | // } |
| | | |
| | | /** |
| | | * 上传网络文件 |
| | |
| | | |
| | | } |
| | | |
| | | public boolean uploadOnlineObject(InputStream inputStream, String bucketName, String key, |
| | | public boolean uploadOnlineObjectNew(InputStream inputStream, String bucketName, String key, |
| | | String mime) throws OSSException, ClientException, IOException { |
| | | if (inputStream != null) { |
| | | // 判断是否存在,不存在创建 |
| | |
| | | return false; |
| | | |
| | | } |
| | | public static ProgressModel queryProgressModel(String id){ |
| | | return PutObjectProgressListenerBiz.queryProgressModel(id); |
| | | } |
| | | public boolean uploadOnlineObject(InputStream inputStream, String bucketName, String key,String tempId, |
| | | String mime) throws OSSException, ClientException, IOException { |
| | | try { |
| | | isExistBucket(bucketName); |
| | | ObjectMetadata objectMeta = new ObjectMetadata(); |
| | | objectMeta.setContentLength(inputStream.available()); |
| | | // 可以在metadata中标记文件类型 |
| | | if (StringUtils.isNotBlank(mime)) { |
| | | objectMeta.setContentType(mime); |
| | | } |
| | | PutObjectRequest putObjectRequest = null; |
| | | if(StringUtils.isNotBlank(tempId)){ |
| | | ProgressModel pmodel = new ProgressModel(); |
| | | pmodel.setKey(key); |
| | | pmodel.setId(tempId); |
| | | pmodel.setStartDate(new Date()); |
| | | putObjectRequest = new PutObjectRequest(bucketName, key, |
| | | inputStream).withProgressListener(new PutObjectProgressListenerBiz(pmodel)); |
| | | }else{ |
| | | putObjectRequest = new PutObjectRequest(bucketName, key, |
| | | inputStream); |
| | | } |
| | | PutObjectResult putObjectResult = client.putObject(putObjectRequest); |
| | | |
| | | // 下载文件的同时指定进度条参数。此处GetObjectProgressListenerDemo为调用类的类名,请在实际使用时替换为相应的类名。 |
| | | // ossClient.getObject(new GetObjectRequest(bucketName,objectName). |
| | | // <GetObjectRequest>withProgressListener(new GetObjectProgressListenerDemo())); |
| | | |
| | | } catch (OSSException oe) { |
| | | System.out.println("Caught an OSSException, which means your request made it to OSS, " |
| | | + "but was rejected with an error response for some reason."); |
| | | System.out.println("Error Message:" + oe.getErrorMessage()); |
| | | System.out.println("Error Code:" + oe.getErrorCode()); |
| | | System.out.println("Request ID:" + oe.getRequestId()); |
| | | System.out.println("Host ID:" + oe.getHostId()); |
| | | } catch (ClientException ce) { |
| | | System.out.println("Caught an ClientException, which means the client encountered " |
| | | + "a serious internal problem while trying to communicate with OSS, " |
| | | + "such as not being able to access the network."); |
| | | System.out.println("Error Message:" + ce.getMessage()); |
| | | } finally { |
| | | if (inputStream != null) { |
| | | // 判断是否存在,不存在创建 |
| | | inputStream.close(); |
| | | } |
| | | if (client != null) { |
| | | client.shutdown(); |
| | | } |
| | | } |
| | | |
| | | |
| | | return false; |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |