【OSS】Bucket读写权限

来源:互联网 发布:c语言选择语句 编辑:程序博客网 时间:2024/05/01 19:34

用户创建的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;import com.aliyun.oss.model.CannedAccessControlList;import com.aliyun.oss.model.CreateBucketRequest;import com.aliyun.oss.model.GetObjectRequest;import com.aliyun.oss.model.ObjectMetadata;import com.aliyun.oss.model.PutObjectResult;/**文件读写权限测试,需要两个账号 */public class OSSAccessExample {/*公司账号*/private static final String ACCESS_ID1 = "*******************";private static final String ACCESS_KEY1 = "************************";/*自己账号*/private static final String ACCESS_ID2 = "***************";private static final String ACCESS_KEY2 = "**********************";private static final String OSS_ENDPOINT = "http://oss.aliyuncs.com/";public static void main(String[] args) {// 初始化OSS客户端OSSClient client1 = new OSSClient(OSS_ENDPOINT, ACCESS_ID1, ACCESS_KEY1);OSSClient client2 = 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);/*上传文件*/String bucketName1 = "saas01";String key1 = "索隆Q版.jpg";String uploadFilePath1 = "G:/test/索隆Q版.jpg";String bucketName2 = "saas02";String key2 = "女帝.jpg";String uploadFilePath2 = "G:/test/女帝.jpg";String bucketName3 = "saas03";String key3 = "启动日志.txt";String uploadFilePath3 = "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("client1 info:" + a.toString());}list1 = client2.listBuckets();System.out.println("client2:" + client2.toString());for (Bucket a : list1) {System.out.println("client2 info:" + 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----------------------");}public static void createBucket(OSSClient client, String bucketName,CannedAccessControlList acl) {/* 通过一个Bucket对象来创建 */CreateBucketRequest bucketObj = new CreateBucketRequest(null);// 构造函数入参为Bucket名称,可以为空bucketObj.setBucketName(bucketName);// 设置bucketObj名称bucketObj.setCannedACL(acl);// 设置bucketObj访问权限aclclient.createBucket(bucketObj);// 创建Bucket}// 上传文件private static void uploadFile(OSSClient client, String bucketName,String key, String filename) throws OSSException, ClientException,FileNotFoundException {File file = new File(filename);ObjectMetadata objectMeta = new ObjectMetadata();objectMeta.setContentLength(file.length());// 可以在metadata中标记文件类型//objectMeta.setContentType("image/jpeg");InputStream input = new FileInputStream(file);PutObjectResult rs = client.putObject(bucketName, key, input,objectMeta);}// 下载文件private static void downloadFile(OSSClient client, String bucketName,String key, String filename) throws OSSException, ClientException {client.getObject(new GetObjectRequest(bucketName, key), new File(filename));}}


0 0