阿里雲的OSS雲端儲存 上傳檔案
阿新 • • 發佈:2018-11-10
第一次使用阿里雲的雲端儲存,mark一下
1.首先,你需要申請購買到AccessKey 和 AccessKeySecret。
因許可權問題,建議開通單獨的RAM帳號
在雲服務中開啟新建一個bucket空間,用於儲存上傳的檔案儲存位置
2.編碼工作開始:
1)maven中引入jar包
pom檔案配置
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version >
</dependency>
2)設定oss的相關引數在properties檔案中,我的檔案是oss.properties
Endpoint=your endPoint
AccessKey=your accessKey
AccessKeySecret=your accessKeySecret
BucketName=your bucketName
3)java中程式碼上傳及獲取可訪問的url地址
OSSClient ossClient = null;
String url = "";
String fileName = "your file name";
//上傳阿里雲
try {
// 建立上傳Object的Metadata
ObjectMetadata metadata = new ObjectMetadata();
// 指定該Object被下載時的網頁的快取行為
metadata.setCacheControl("no-cache");
// 指定該Object下設定Header
metadata.setHeader("Pragma", "no-cache");
// 指定該Object被下載時的內容編碼格式
metadata.setContentEncoding("utf-8");
// 檔案的MIME,定義檔案的型別及網頁編碼,決定瀏覽器將以什麼形式、什麼編碼讀取檔案。如果使用者沒有指定則根據Key或檔名的副檔名生成,
// 如果沒有副檔名則填預設值application/octet-stream,apk檔案的contentType
metadata.setContentType("application/octet-stream");
//讀取配置檔案
OSSConfigure ossCon = new OSSConfigure("/properties/oss.properties");
//建立ossClient
ossClient = new OSSClient(ossCon.getEndpoint(), ossCon.getAccessKeyId(), ossCon.getAccessKeySecret());
//判斷bucket是否存在
if(ossClient.doesBucketExist(ossCon.getBucketName())) {
//已存在,不用處理
}else {
// 建立儲存空間
ossClient.createBucket(ossCon.getBucketName());
}
// 檔案轉換為輸入流
CommonsMultipartFile cf = (CommonsMultipartFile) file;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
InputStream fileContent;
fileContent = fi.getInputStream();
//oss 檔案上傳
ossClient.putObject(ossCon.getBucketName(), fileName, fileContent, metadata);
//獲取其url
Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
url = ossClient.generatePresignedUrl(ossCon.getBucketName(), fileName, expiration).toString();
// 關閉client
ossClient.shutdown();
}catch(OSSException e) {
e.printStackTrace();
}catch(ClientException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} finally { //保證是否存在異常,ossClint都要關閉,不然會在console中持續提示ossClient未關閉
if(ossClient != null) {
ossClient.shutdown();
}
}
4)最後,附上自己寫的OSSConfigure類
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class OSSConfigure {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
public OSSConfigure() {
}
//建構函式-傳入properties檔案路徑
public OSSConfigure(String storageConfName) throws IOException {
Properties prop = new Properties();
InputStream is = super.getClass().getClassLoader().getResourceAsStream(storageConfName);
prop.load(is);
endpoint = prop.getProperty("Endpoint").trim();
accessKeyId = prop.getProperty("AccessKey").trim();
accessKeySecret = prop.getProperty("AccessKeySecret").trim();
bucketName = prop.getProperty("BucketName").trim();
}
//建構函式-傳入oss預設配置的值
public OSSConfigure(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
this.endpoint = endpoint;
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
this.bucketName = bucketName;
}
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getAccessKeySecret() {
return accessKeySecret;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
}
以上。