用户创建的Bucket总共有三种权限:
/*私有
* 只允许自己读写操作,其他用户没有权限
*/
CannedAccessControlList acl_private =CannedAccessControlList.Private;
/*公共读写
* 允许自己和其他用户读写操作
*/
CannedAccessControlList acl_pub_readwrite =CannedAccessControlList.PublicReadWrite;
/*公共读
* 只允许自己进行写操作,但是允许自己及其他用户进行读操作
*/
CannedAccessControlList acl_pub_red =CannedAccessControlList.PublicRead;
测试demo
package com.ls;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Bucket;
importcom.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
importcom.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
importcom.aliyun.oss.model.PutObjectResult;
/**文件读写权限测试,需要两个账号
*/
public class OSSAccessExample {
/*公司账号*/
privatestatic final String ACCESS_ID1 = "*******************";
privatestatic final String ACCESS_KEY1 = "************************";
/*自己账号*/
privatestatic final String ACCESS_ID2 = "***************";
privatestatic final String ACCESS_KEY2 = "**********************";
privatestatic final String OSS_ENDPOINT = "http://oss.aliyuncs.com/";
publicstatic void main(String[] args) {
//初始化OSS客户端
OSSClientclient1 = new OSSClient(OSS_ENDPOINT, ACCESS_ID1, ACCESS_KEY1);
OSSClientclient2 = new OSSClient(OSS_ENDPOINT, ACCESS_ID2, ACCESS_KEY2);
//key1创建名为saas01的BUCKET,权限为私有
createBucket(client1,"saas01", CannedAccessControlList.Private);
//key2创建名为saas02的BUCKET,权限为公用读写
createBucket(client2,"saas02", CannedAccessControlList.PublicReadWrite);
//key1创建名为saas03的BUCKET,权限为公用读
createBucket(client1,"saas03", CannedAccessControlList.PublicRead);
/*上传文件*/
StringbucketName1 = "saas01";
Stringkey1 = "索隆Q版.jpg";
StringuploadFilePath1 = "G:/test/索隆Q版.jpg";
StringbucketName2 = "saas02";
Stringkey2 = "女帝.jpg";
StringuploadFilePath2 = "G:/test/女帝.jpg";
StringbucketName3 = "saas03";
Stringkey3 = "启动日志.txt";
StringuploadFilePath3 = "G:/test/启动日志.txt";
try{
/*对各自的bucket操作,都有权限*/
System.out.println("正在上传...");
uploadFile(client1,bucketName1, key1, uploadFilePath1);
uploadFile(client2,bucketName2, key2, uploadFilePath2);
uploadFile(client1,bucketName3, key3, uploadFilePath3);
}catch (OSSException e) {
e.printStackTrace();
}catch (ClientException e) {
e.printStackTrace();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("上传私有...");
try{
uploadFile(client2,bucketName1, key1+"(上传私有)", uploadFilePath1);
}catch (Exception e) {
System.out.println(e.getMessage());//无写入权限,抛出异常
}
System.out.println("上传只读...");
try{
uploadFile(client2,bucketName3, key3+"(上传只读)", uploadFilePath3);
}catch (Exception e) {
System.out.println(e.getMessage());//无写入权限,抛出异常
}
System.out.println("上传公共读写...");
try{
uploadFile(client1,bucketName2, key2+"(上传公共读写)", uploadFilePath2);
}catch (Exception e) {
System.out.println(e.getMessage());//成功上传,无异常
}
List<Bucket>list1 = new ArrayList<Bucket>();
list1= client1.listBuckets();
System.out.println("client1:"+ client1.toString());
for(Bucket a : list1) {
System.out.println("client1info:" + a.toString());
}
list1= client2.listBuckets();
System.out.println("client2:"+ client2.toString());
for(Bucket a : list1) {
System.out.println("client2info:" + a.toString());
}
/*自己下载自己的*/
System.out.println("正在下载(自己下载自己的)...");
downloadFile(client1, bucketName1, key1, "G:/test/download/"+key1);
downloadFile(client2, bucketName2, key2, "G:/test/download/"+key2);
downloadFile(client1, bucketName3, key3, "G:/test/download/"+key3);
/*下载私有的*/
System.out.println("正在下载(下载私有)...");
try {
downloadFile(client2,bucketName1, key1, "G:/test/download/(下载私有)"+key1);
}catch (Exception e) {
System.out.println(e.getMessage());//此处会抛出异常,无权限下载
}
System.out.println("正在下载(下载只读)...");
try {
downloadFile(client2,bucketName3, key3, "G:/test/download/(下载只读)"+key3);
}catch (Exception e) {
System.out.println(e.getMessage());//成功下载
}
System.out.println("正在下载(下载公共读写)...");
try {
downloadFile(client1,bucketName2, key2, "G:/test/download/(下载公共读写)"+key2);
}catch (Exception e) {
System.out.println(e.getMessage());//成功下载
}
client1.shutdown();
client2.shutdown();
System.out.println("-----------------------SUCCESS----------------------");
}
publicstatic void createBucket(OSSClient client, String bucketName,
CannedAccessControlListacl) {
/*通过一个Bucket对象来创建 */
CreateBucketRequestbucketObj = new CreateBucketRequest(null);// 构造函数入参为Bucket名称,可以为空
bucketObj.setBucketName(bucketName);//设置bucketObj名称
bucketObj.setCannedACL(acl);//设置bucketObj访问权限acl
client.createBucket(bucketObj);//创建Bucket
}
//上传文件
privatestatic void uploadFile(OSSClient client, String bucketName,
Stringkey, String filename) throws OSSException, ClientException,
FileNotFoundException{
Filefile = new File(filename);
ObjectMetadataobjectMeta = new ObjectMetadata();
objectMeta.setContentLength(file.length());
//可以在metadata中标记文件类型
//objectMeta.setContentType("image/jpeg");
InputStreaminput = new FileInputStream(file);
PutObjectResultrs = client.putObject(bucketName, key, input,
objectMeta);
}
//下载文件
privatestatic void downloadFile(OSSClient client, String bucketName,
Stringkey, String filename) throws OSSException, ClientException {
client.getObject(newGetObjectRequest(bucketName, key), new File(
filename));
}