AliyunOSS整合Spring的一些簡單操作。
2016-03-20 分類:插件工具 閱讀(2190) 評論(0)
阿裡雲出品的對象存儲服務——OSS
對象存儲(Object Storage Service,簡稱OSS),是阿裡雲提供的海量、安全和高可靠的雲存儲服務。存儲容量和處理能力的彈性擴充,按量付費真正使您專注于核心業務。您還可以友善的同其他雲産品搭配使用,廣泛的應用于海量資料存儲與備份,資料加工與處理,内容加速分發,業務資料挖掘分析等多種業務場景
OSS工具部落格已經更新,現在不需要整合到Spring内就可以很輕松的使用。
推薦大家請通路:阿裡雲 OSS 工具
aliyun_java_sdk_20160301.zip
OSSToolsJava
package util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.IOUtils;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
/**
* Aliyun OSS
* 2016-1-20 15:10:21
* @author Jecced
*/
public class OSSTools {
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String ossEndpoint;
private int timeout;
private OSSClient ossClient;
public OSSTools() {}
/**
* OSS初始化
*/
public void init() {
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(timeout);
conf.setMaxErrorRetry();
ossClient = new OSSClient("http://" + ossEndpoint, accessKeyId, accessKeySecret, conf);
System.out.println("OSS初始化");
}
public void destroy(){
ossClient.shutdown();
}
/**
* 指定的key是否存在
*/
public boolean isExist(String key){
key = genKey(key);
return ossClient.doesObjectExist(bucketName, key);
}
/**
* 從OSS中擷取檔案輸入流
*/
public InputStream getObjeInputStream(String key){
key = genKey(key);
OSSObject obj = ossClient.getObject(bucketName, key);
return obj.getObjectContent();
}
/**
* 将輸入流下載下傳存到指定的File檔案中
*/
public void saveIsToFile(InputStream is ,File file){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
byte[] buffer = new byte[ * ];
int len = -;
while ((len = is.read(buffer)) != -) {
fos.write(buffer, , len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.safeClose(fos);
}
}
/**
* 檔案下載下傳,以流的形式
*/
public void downObj(String key,File file){
key = genKey(key);
InputStream is = getObjeInputStream(key);
saveIsToFile(is, file);
}
/**
* 簡單上傳OSS檔案
* @param name 檔案名
* @param file File對象
* @param path 存儲路徑
* @param contentType 手動設定檔案類型:image/png
* @return OSS檔案Key的路徑
*/
public String uploadObj(String path, String name, File file,String contentType) {
String key = path + "/" + name;
key = genKey(key);
ObjectMetadata meta = null;
if(contentType != null){
meta = new ObjectMetadata();
meta.setContentType(contentType);
}
ossClient.putObject(bucketName, key, file, meta);
return "http://" + bucketName + "." + ossEndpoint + "/" + key;
}
public String uploadObj(String path, String name, File file) {
return uploadObj(path,name,file,null);
}
/**
* 删除指定key
*/
public void delObj(String key){
ossClient.deleteObject(bucketName, key);
}
/**
* 處理key開頭是/,傳回開頭沒有/的key
*/
private String genKey(String key){
if (key != null) {
key = key.replaceAll("\\\\", "/");
}
while (key != null && key.startsWith("/")) {
key = key.substring(, key.length());
}
return key;
}
/**
* 簽名url
*
* @author kira
* @param bucketName
* @param key
* @param expiration
* 有效期
* @return
*/
public static URL generatePresignedUrl(String bucketName, String key, Date expiration) {
logger.info(ENDPOINTINFO + "擷取URL,bucketName=" + bucketName + ",key=" + key);
return ossClient.generatePresignedUrl(bucketName, key, expiration);
}
public OSSClient getOSSClient() {
return ossClient;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
public void setOssBucket(String ossBucket) {
this.bucketName = ossBucket;
}
public void setOssEndpoint(String ossEndpoint) {
this.ossEndpoint = ossEndpoint;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
}
AppliactionContext.xmlXHTML
<!--配置OSS-->
<bean id="aliyun" class="Util.Aliyun" lazy-init="false" init-method="init" destroy-method="destroy">
<property name="accessKeyId" value="****AccessKey****"/>
<property name="accessKeySecret" value="****AccessKeySecret****"/>
<property name="bucketName" value="****BucketName****"/>
<property name="timeout" value="5000"/>
<property name="ossEndpoint" value="oss-cn-hangzhou.aliyuncs.com"/>
</bean>