import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
public class OssUtil {
public static String endpoint;
public static String accessKeyId;
public static String accessKeySecret;
public static String bucketName;
public static String ossPrefix;
public static String bindUrl;
static {
InputStream is = OssUtil.class.getResourceAsStream("/application.properties");
Properties prop = new Properties();
try {
prop.load(is);
} catch (IOException e1) {
e1.printStackTrace();
}
endpoint = prop.getProperty("ali.oss.endpoint");
accessKeyId = prop.getProperty("ali.oss.access-keyid");
accessKeySecret = prop.getProperty("ali.oss.access-secret");
bucketName = prop.getProperty("ali.oss.bucket-name");
ossPrefix = prop.getProperty("ali.oss.ossPrefix");
bindUrl = prop.getProperty("ali.oss.bind-url");
}
/**
* 取得阿里Oss对象Key
*
* @param sampleNo 渠道ID
* @return
*/
public static String getKey(String numStr) {
// checkup_<目录名>_<文件名>
return StringUtils.join("_", "checkup", numStr, UUID.randomUUID().toString());
}
/**
* 取得阿里Oss对象Key
*
* @param fileName
* @return
*/
private static String getKey(String fileName, String url) {
// 删除绑定URL
String key = StringUtils.removeStart(fileName, url);
return StringUtils.removeStart(key, "/");
}
/**
* oss上传方法方法,失败返回null,成功返回路径
*
* @param file
* @param key 文件名
* @return
*/
public static String putObject(File file, String key) {
System.out.println("AliUtil::putObject:{}" + key);
// //创建OSSConfig对象
// OSSConfig config = SpringBeanFactoryUtils.getBean(OSSConfig.class);
// 取得阿里Oss对象Key
key = getKey(key, ossPrefix);
//创建OssClient对象
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//将文件放入桶中
PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, key, file));
//释放资源
ossClient.shutdown();
// 上传失败,返回null
if (StringUtils.isEmpty(result.getETag())) {
System.out.println("阿里云上传失败:{}" + result);
return null;
}
//上传成功返回路径
return StringUtils.join(ossPrefix, "/", key);
}
/**
* 批量提交对象流到阿里OSS
*
* @param fileMap
* @return
*/
public static boolean putObjects(Map<String, byte[]> fileMap) {
System.out.println("AliUtil::putObjects:{}" + fileMap.keySet());
// //创建OSSConfig对象
// OSSConfig config = SpringBeanFactoryUtils.getBean(OSSConfig.class);
//创建OssClient对象
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
for (Map.Entry<String, byte[]> entry : fileMap.entrySet()) {
// 取得阿里Oss对象Key
String key = getKey(entry.getKey(), ossPrefix);
//将文件放入你Bucket中
PutObjectResult result = ossClient.putObject(
new PutObjectRequest(bucketName, key, new ByteArrayInputStream(entry.getValue())));
// 上传失败,返回false
if (StringUtils.isEmpty(result.getETag())) {
System.out.println("阿里云批量上传失败:{}" + result);
return false;
}
}
return true;
}
/**
* 根据key下载,获得输入流
*
* @param key
* @return
*/
public static byte[] getObject(String key) throws IOException {
System.out.println("AliUtil::getObject:{}" + key);
// //创建OSSConfig对象
// OSSConfig config = SpringBeanFactoryUtils.getBean(OSSConfig.class);
// 取得阿里Oss对象Key
key = getKey(key, ossPrefix);
//创建OssClient对象
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
OSSObject object = ossClient.getObject(new GetObjectRequest(bucketName, key));
byte[] obj = IOUtils.toByteArray(object.getObjectContent());
//释放资源
ossClient.shutdown();
return obj;
}
public static void main(String[] args) {
String wordFileName = "aaa";
File file = new File("/tmp/"+aaa+".docx");
String uploadPathSuffix = "/word_data/" + wordFileName + ".docx";
long begin = System.currentTimeMillis();
String uploadPath = putObject(file, uploadPathSuffix);
long end = System.currentTimeMillis();
System.out.println("用时:" + (end - begin)/1000 + " s");
System.out.println(uploadPath);
}
}