所需jar包:
aliyun-openservices-1.2.3.jar
jdom-1.1.jar
commons-codec-1.4.jar
commons-logging-1.1.1.jar
gson-2.2.4.jar
httpclient-4.1.3.jar
httpcore-4.1.4.jar
protobuf-java-2.4.1.jar
實體類:
package com.xcfh.aliyun;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import com.aliyun.openservices.ClientConfiguration;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ServiceException;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.OSSErrorCode;
import com.aliyun.openservices.oss.OSSException;
import com.aliyun.openservices.oss.model.CannedAccessControlList;
import com.aliyun.openservices.oss.model.GetObjectRequest;
import com.aliyun.openservices.oss.model.OSSObjectSummary;
import com.aliyun.openservices.oss.model.ObjectListing;
import com.aliyun.openservices.oss.model.ObjectMetadata;
/**
*
*@ClassName: TestOSS
*
*@Description: 該示例代碼展示了如果在OSS中建立和删除一個Bucket,以及如何上傳和下載下傳一個檔案。
*
* 該示例代碼的執行過程是:
* 1. 建立一個Bucket(如果已經存在,則忽略錯誤碼);
* 2. 上傳一個檔案到OSS;
* 3. 下載下傳這個檔案到本地;
* 4. 清理測試資源:删除Bucket及其中的所有Objects。
*
* 嘗試運作這段示例代碼時需要注意:
* 1. 為了展示在删除Bucket時除了需要删除其中的Objects,
* 示例代碼最後為删除掉指定的Bucket,因為不要使用您的已經有資源的Bucket進行測試!
* 2. 請使用您的API授權密鑰填充ACCESS_ID和ACCESS_KEY常量;
* 3. 需要準确上傳用的測試檔案,并修改常量uploadFilePath為測試檔案的路徑;
* 修改常量downloadFilePath為下載下傳檔案的路徑。
* 4. 該程式僅為示例代碼,僅供參考,并不能保證足夠健壯。
*OSS Java API手冊:http://aliyun_portal_storage.oss.aliyuncs.com/oss_api/oss_javahtml/index.html?spm=5176.7150518.1996836753.8.d5TjaG
*OSS Java SDK開發包:http://help.aliyun.com/view/13438814.html
*OSSClient:www.mvnrepository.com/artifact/cglib/cglib/2.2
*@author PineTree
*@date 2014年12月1日 下午3:23:32
*@version
*/
public class TestOSS {
/**
* 阿裡雲ACCESS_ID
*/
private static String ACCESS_ID = "你的ACCESS_ID";
/**
* 阿裡雲ACCESS_KEY
*/
private static String ACCESS_KEY = "你的ACCESS_KEY";
/**
* 阿裡雲OSS_ENDPOINT 青島Url
*/
private static String OSS_ENDPOINT = "http://oss-cn-qingdao.aliyuncs.com";
/**
* 阿裡雲BUCKET_NAME OSS
*/
private static String BUCKET_NAME = "demo10";
public static void main(String[] args) {
//String bucketName = "demo10";
String Objectkey = "photo1.jpg";
String uploadFilePath = "D:\\photo.jpg";
String downloadFilePath = "D:\\photo1.jpg";
// 使用預設的OSS伺服器位址建立OSSClient對象,不叫OSS_ENDPOINT代表使用杭州節點,青島節點要加上不然包異常
OSSClient client = new OSSClient(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY);
//如果你想配置OSSClient的一些細節的參數,可以在構造OSSClient的時候傳入ClientConfiguration對象。
//ClientConfiguration是OSS服務的配置類,可以為用戶端配置代理,最大連接配接數等參數。
//具體配置看http://aliyun_portal_storage.oss.aliyuncs.com/oss_api/oss_javahtml/OSSClient.html#id2
//ClientConfiguration conf = new ClientConfiguration();
//OSSClient client = new OSSClient(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY, conf);
try {
ensureBucket(client, BUCKET_NAME);
setBucketPublicReadable(client, BUCKET_NAME);
System.out.println("正在上傳...");
uploadFile(client, BUCKET_NAME, Objectkey, uploadFilePath);
System.out.println("正在下載下傳...");
downloadFile(client, BUCKET_NAME, Objectkey, downloadFilePath);
}catch(Exception e){
e.printStackTrace();
} finally {
deleteBucket(client, BUCKET_NAME);
}
}
/**
* 建立Bucket
*
* @param client OSSClient對象
* @param bucketName BUCKET名
* @throws OSSException
* @throws ClientException
*/
public static void ensureBucket(OSSClient client, String bucketName)throws OSSException, ClientException{
try{
client.createBucket(bucketName);
}catch(ServiceException e){
if(!OSSErrorCode.BUCKES_ALREADY_EXISTS.equals(e.getErrorCode())){
throw e;
}
}
}
/**
* 删除一個Bucket和其中的Objects
*
* @param client OSSClient對象
* @param bucketName Bucket名
* @throws OSSException
* @throws ClientException
*/
private static void deleteBucket(OSSClient client, String bucketName)throws OSSException, ClientException{
ObjectListing ObjectListing = client.listObjects(bucketName);
List<OSSObjectSummary> listDeletes = ObjectListing.getObjectSummaries();
for(int i = 0; i < listDeletes.size(); i++){
String objectName = listDeletes.get(i).getKey();
System.out.println("objectName = " + objectName);
//如果不為空,先删除bucket下的檔案
client.deleteObject(bucketName, objectName);
}
client.deleteBucket(bucketName);
}
/**
* 把Bucket設定成所有人可讀
*
* @param client OSSClient對象
* @param bucketName Bucket名
* @throws OSSException
* @throws ClientException
*/
private static void setBucketPublicReadable(OSSClient client, String bucketName)throws OSSException, ClientException{
//建立bucket
client.createBucket(bucketName);
//設定bucket的通路權限, public-read-write權限
client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
}
/**
* 上傳檔案
*
* @param client OSSClient對象
* @param bucketName Bucket名
* @param Objectkey 上傳到OSS起的名
* @param filename 本地檔案名
* @throws OSSException
* @throws ClientException
* @throws FileNotFoundException
*/
private static void uploadFile(OSSClient client, String bucketName, String Objectkey, String filename)
throws OSSException, ClientException, FileNotFoundException{
File file = new File(filename);
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentLength(file.length());
//判斷上傳類型,多的可根據自己需求來判定
if (filename.endsWith("xml")) {
objectMeta.setContentType("text/xml");
}
else if (filename.endsWith("jpg")) {
objectMeta.setContentType("image/jpeg");
}
else if (filename.endsWith("png")) {
objectMeta.setContentType("image/png");
}
InputStream input = new FileInputStream(file);
client.putObject(bucketName, Objectkey, input, objectMeta);
}
/**
* 下載下傳檔案
*
* @param client OSSClient對象
* @param bucketName Bucket名
* @param Objectkey 上傳到OSS起的名
* @param filename 檔案下載下傳到本地儲存的路徑
* @throws OSSException
* @throws ClientException
*/
private static void downloadFile(OSSClient client, String bucketName, String Objectkey, String filename)
throws OSSException, ClientException {
client.getObject(new GetObjectRequest(bucketName, Objectkey),
new File(filename));
}
}