package com.zyt.back.utils;
//import com.aliyun.oss.OSSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* Created by sumeng on 2018/7/25.
*/
public class AliOssUtil {
private static final Logger logger = LoggerFactory.getLogger(AliOssUtil.class);
public static String accessKeyId = "LTAIcSNsNoJqQvUr";
public static String accessKeySecret = "zceKh1ZYYwN10RPjPe6WiLKEEr9ipd";
public static OSSClient ossClient = null;
public static String bucketName = "cube-yunze";
public static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
public static String key = "<downloadKey>";
//初始化
// {
//
// // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
//
// // 创建ClientConfiguration实例
// ClientConfiguration conf = new ClientConfiguration();
// // 设置OSSClient使用的最大连接数,默认1024
// conf.setMaxConnections(200);
// // 设置请求超时时间,默认50秒
// conf.setSocketTimeout(10000);
// // 设置失败请求重试次数,默认3次
// conf.setMaxErrorRetry(3);
//
// // 创建OSSClient实例
// ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//
// }
public static OSSClient getOssClient() {
return new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
public static String getUniqueFilePre() {
LocalDateTime localDateTime = LocalDateTime.now();
String time = localDateTime.format(DateTimeFormatter.ofPattern("yyMMddHHmmssSSS"));
return time;
}
public static boolean upload(MultipartFile file, String path) {
System.out.println("-----------upload start time = " + System.currentTimeMillis());
OSSClient ossClient = null;
try{
// 创建OSSClient实例
ossClient = AliOssUtil.getOssClient();
// 上传文件流
// ossClient.putObject(AliOssUtil.bucketName, path, file.getInputStream());
String fileName = file.getOriginalFilename();
if(fileName.trim() != "") {
File newFile = new File(fileName);
FileOutputStream outStream = new FileOutputStream(newFile);
outStream.write(file.getBytes());
outStream.close();
file.transferTo(newFile);
InputStream inputStream = new FileInputStream(newFile);
ossClient.putObject(AliOssUtil.bucketName, path, inputStream);
newFile.delete();
}
System.out.println("-----------upload end time = " + System.currentTimeMillis());
return true;
}catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
logger.info("[oss-upload] error:" + sw.toString());
}finally {
// 关闭client
ossClient.shutdown();
}
return false;
}
public static boolean delete(String path) {
OSSClient ossClient = null;
try{
// 创建OSSClient实例
ossClient = AliOssUtil.getOssClient();
ossClient.deleteObject(AliOssUtil.bucketName, path);
return true;
}catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
logger.info("[oss-delete] error:" + sw.toString());
}finally {
// 关闭client
ossClient.shutdown();
}
return false;
}
public static void main(String[] args) {
try{
//上传
// 创建OSSClient实例
OSSClient ossClient = AliOssUtil.getOssClient();
// // 上传文件流
File file = new File("F:/1.jpg");
InputStream inputStream = new FileInputStream(file);
ossClient.putObject(AliOssUtil.bucketName, "goods01"+"/" + getUniqueFilePre()+"-"+file.getName(), inputStream);
// //删除
// ossClient.deleteObject(AliOssUtil.bucketName, "commodity/171025161438789-file");
// 关闭client
ossClient.shutdown();
// Object是否存在
// boolean found = ossClient.doesObjectExist("<bucketName>", "<key>");
}catch(Exception e) {
e.printStackTrace();
}
}
}