正好这个项目快结束了 ,被阿里云下载折腾了好久,网上相关支持又比较少,所有我结合自己的项目,简单的讲一下。
1.OSSObjectUtils基于OSS官方文档写的工具类
public class OSSObjectUtils {
private static Logger logger = LoggerFactory.getLogger(OSSObjectUtils.class);
private static OSSClient ossClient = new OSSClient(OSSConfig.ENDPOINT_SHANGHAI, OSSConfig.ACCESSKEY_ID, OSSConfig.ACCESSKEY_SECRET);
//private static OSSClient ossClient = new OSSClient("http://oss-cn-shanghai.aliyuncs.com", "0aHuVNGwxXsHDRIg", "6dvOMka7ON0DogefcWm09MbR4ofymB");
private static String ENDPOINTINFO = ossClient.getEndpoint().toString();
private OSSObjectUtils() {
}
/** * 创建Bucket * *@param bucketName *@throws OSSException *@throws ClientException */
@SuppressWarnings("unused")
@Deprecated
private static void createBucket(String bucketName) throws OSSException, ClientException {
if (!ossClient.doesBucketExist(bucketName)) {
logger.info(ENDPOINTINFO + "创建bucketName:" + bucketName);
ossClient.createBucket(bucketName);
}
}
public static boolean isExistObject(String bucketName, String key) {
boolean result = false;
try {
result = ossClient.doesObjectExist(bucketName, key);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** * 上传object * *@param bucketName *@param key *@param inputStream *@throws OSSException *@throws ClientException */
public static void putObject(String bucketName, String key, InputStream inputStream)
throws OSSException, ClientException {
logger.info(ENDPOINTINFO + "上传Object,bucketName=" + bucketName + ",key=" + key);
ossClient.putObject(bucketName, key, inputStream);
}
/** * 上传object * *@param bucketName *@param key *@param inputStream *@throws OSSException *@throws ClientException */
public static void putObject(String bucketName, String key, File file) throws OSSException, ClientException {
logger.info(ENDPOINTINFO + "上传Object,bucketName=" + bucketName + ",key=" + key);
ossClient.putObject(bucketName, key, file);
}
/** * 上传object,带元数据 * *@param bucketName *@param key *@param inputStream *@param meta *@throws OSSException *@throws ClientException */
public static void putObject(String buck