1. 程式人生 > >OSS物件儲存學習——JAVA篇

OSS物件儲存學習——JAVA篇

import java.io.File;
import java.net.URL;
import java.util.Date;


import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;

import com.aliyun.oss.model.PutObjectResult;

/**
 * OSS方法測試類
 * @author ZD
 *
 */

public class OssTest {

//建立OSS客戶端
private static OSSClient client;

//設定連線

private static String bucket_name = "  your bucketName "; 

private static String endpoint = " you endpoint ";

private static String accessKeyId = " you accessKeyId ";

private static String accessKeySecret =

 " you accessKeySecret ";

/**
*  配置
Conf連線屬性

* @return conf
*/

public ClientConfiguration conf() {
// 建立ClientConfiguration例項
ClientConfiguration conf = new ClientConfiguration();

// 設定OSSClient使用的最大連線數,預設1024
conf.setMaxConnections(200);

// 設定請求超時時間,預設50秒
conf.setSocketTimeout(10000);

// 設定失敗請求重試次數,預設3次
conf.setMaxErrorRetry(

3);

return conf;

}

/**
*  開啟OSS
* @return boolean
*/
public boolean open() {
if(client == null) {
client = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf());
}
return true;
}

/**
*  關閉OSS
* @return boolean
*/
public boolean close() {
if (client != null) {
client.shutdown();
}
return true;
}

/**
* 列
當前Bucket下的所有檔案
*/
public void listObjects() {
ObjectListing objectListing = client.listObjects(bucket_name);
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
}
}

/**
* 生成URL
*/
public void makeUrl() {

try {
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket_name,"UAT/upload_test_001.xml",
HttpMethod.GET);

// 設定URL過期時間為1小時

Date expiration = new Date(new Date().getTime() + 3600 * 1000);
request.setExpiration(expiration);

// 生成URL簽名(HTTP GET請求)
URL signedUrl = client.generatePresignedUrl(request);
System.out.println("signed url for getObject: " + signedUrl);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* 上傳檔案
*/
public void upload(){

try {

// 建立上傳Object的Metadata
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("application/octet-stream");

//上傳本地檔案
PutObjectResult rs = client.putObject(bucket_name, "UAT/upload_test_001.xml", new File("D:/test.xml"),meta);

System.out.println("上傳成功");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("上傳失敗");
}

}

/**
* 上傳檔案流
*/
public void OssUpload(InputStream inputStream,String fileName){
try {
// 建立上傳Object的Metadata
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("application/octet-stream");
client.putObject(bucket_name,fileName,inputStream,meta);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

/**
* 下載檔案到本地
*/
public void download(){
try{
client.getObject(new GetObjectRequest(bucket_name, "UAT/upload_test_001.xml"), new File("D:/upload_test_001.xml"));
System.out.println("下載成功");
}catch(Exception e) {
e.printStackTrace();
System.out.println("下載失敗");
}
}

/**
* 流式下載
*/
public InputStream  ossDownload(){


OSSObject ossObject = client.getObject(bucket_name, "UAT/upload_test_001.xml");
InputStream is = new DataInputStream(ossObject.getObjectContent());

return is;
}

/**
* Oss刪除
* @param name
*/
public void delete(String name) {
client.deleteObject(bucket_name, name);
}

}

更多OSS物件儲存學習,請參考阿里雲物件儲存OSS_SDK文件