import com.aliyun.oss.ClientException;import com.aliyun.oss.HttpMethod;import com.aliyun.oss.OSSClient;import com.aliyun.oss.OSSException;import com.aliyun.oss.common.utils.DateUtil;import com.aliyun.oss.model.GeneratePresignedUrlRequest;import com.aliyun.oss.model.PutObjectRequest;import org.apache.log4j.Logger;import java.io.ByteArrayInputStream;import java.io.File;import java.net.URL;import java.text.ParseException;import java.util.Date;/**
* @author cc
*/publicclassOssUtils{privatestatic Logger log = Logger.getLogger(OssUtils.class);privatestatic String endpoint = ProperUtils.getString("endpoint");privatestatic String accessKeyId = ProperUtils.getString("accessKeyId");privatestatic String accessKeySecret = ProperUtils.getString("accessKeySecret");privatestatic String bucketName = ProperUtils.getString("bucketName");//private static String key = "ADFSEHSJ";//private static String localFile = "D://temp/abc.jpg";/**
* @param args
*/publicstaticvoidmain(String[] args){//fileDelete("audio/2");//fileUpload(localFile, "a/cover/abc.jpg");}publicstatic String fileUpload(String filePath, String fileRelaPath){
String fileUrl ="";
OSSClient ossClient =newOSSClient(endpoint, accessKeyId, accessKeySecret);try{/*
* 上传一个对象到存储空间
*/
Date expiration = DateUtil.parseRfc822Date("Wed, 18 Mar 2118 16:18:00 GMT");
GeneratePresignedUrlRequest request =newGeneratePresignedUrlRequest(
bucketName, fileRelaPath, HttpMethod.GET);// 设置过期时间
request.setExpiration(expiration);// 生成URL签名(HTTP GET请求)
URL signedUrl = ossClient.generatePresignedUrl(request);
fileUrl = signedUrl.toString();
File file =newFile(filePath);
ossClient.putObject(newPutObjectRequest(bucketName, fileRelaPath, file));//上传到OSS后删除本地文件
file.delete();return fileUrl;}catch(OSSException oe){
log.info("Caught an OSSException, which means your request made it to OSS, "+"but was rejected with an error response for some reason.");
log.info("Error Message: "+ oe.getErrorCode());
log.info("Error Code: "+ oe.getErrorCode());
log.info("Request ID: "+ oe.getRequestId());
log.info("Host ID: "+ oe.getHostId());}catch(ClientException ce){
log.info("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.");
log.info("Error Message: "+ ce.getMessage());}catch(ParseException e){
e.printStackTrace();}finally{
ossClient.shutdown();}return fileUrl;}publicstaticvoidfileDelete(String fileRelaPath){
OSSClient ossClient =newOSSClient(endpoint, accessKeyId, accessKeySecret);try{/*
* 从存储空间删除一个对象
*///判断文件是否存在
ossClient.deleteObject(bucketName, fileRelaPath);}catch(OSSException oe){
log.info("Caught an OSSException, which means your request made it to OSS, "+"but was rejected with an error response for some reason.");
log.info("Error Message: "+ oe.getErrorCode());
log.info("Error Code: "+ oe.getErrorCode());
log.info("Request ID: "+ oe.getRequestId());
log.info("Host ID: "+ oe.getHostId());}catch(ClientException ce){
log.info("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.");
log.info("Error Message: "+ ce.getMessage());}finally{
ossClient.shutdown();}}publicstatic String fileUploadBytes(byte[] bytes, String fileRelaPath){
String fileUrl ="";
OSSClient ossClient =newOSSClient(endpoint, accessKeyId, accessKeySecret);try{/*
* 上传一个对象到存储空间
*/
Date expiration = DateUtil.parseRfc822Date("Wed, 18 Mar 2118 16:18:00 GMT");
GeneratePresignedUrlRequest request =newGeneratePresignedUrlRequest(
bucketName, fileRelaPath, HttpMethod.GET);// 设置过期时间
request.setExpiration(expiration);// 生成URL签名(HTTP GET请求)
URL signedUrl = ossClient.generatePresignedUrl(request);
fileUrl = signedUrl.toString();
ossClient.putObject(bucketName, fileRelaPath,newByteArrayInputStream(bytes));//上传到OSS后删除本地文件return fileUrl;}catch(OSSException oe){
log.info("Caught an OSSException, which means your request made it to OSS, "+"but was rejected with an error response for some reason.");
log.info("Error Message: "+ oe.getErrorCode());
log.info("Error Code: "+ oe.getErrorCode());
log.info("Request ID: "+ oe.getRequestId());
log.info("Host ID: "+ oe.getHostId());}catch(ClientException ce){
log.info("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.");
log.info("Error Message: "+ ce.getMessage());}catch(ParseException e){
e.printStackTrace();}finally{
ossClient.shutdown();}return fileUrl;}publicstatic String getUploadFileUrl(String filePathName){
String fileUrl ="";
OSSClient ossClient =newOSSClient(endpoint, accessKeyId, accessKeySecret);try{/*
* 上传一个对象到存储空间
*/
Date expiration = DateUtil.parseRfc822Date("Wed, 18 Mar 2118 16:18:00 GMT");
GeneratePresignedUrlRequest request =newGeneratePresignedUrlRequest(
bucketName, filePathName, HttpMethod.GET);// 设置过期时间
request.setExpiration(expiration);// 生成URL签名(HTTP GET请求)
URL signedUrl = ossClient.generatePresignedUrl(request);return signedUrl.toString();}catch(OSSException oe){
log.info("Caught an OSSException, which means your request made it to OSS, "+"but was rejected with an error response for some reason.");
log.info("Error Message: "+ oe.getErrorCode());
log.info("Error Code: "+ oe.getErrorCode());
log.info("Request ID: "+ oe.getRequestId());
log.info("Host ID: "+ oe.getHostId());}catch(ClientException ce){
log.info("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.");
log.info("Error Message: "+ ce.getMessage());}catch(ParseException e){
e.printStackTrace();}finally{
ossClient.shutdown();}return fileUrl;}}