阿里云oss api解读

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36838191/article/details/79969765

如果要用java来操作oss,首先需要下载,sdk其中的包放在java项目的路径中


您可以使用OSSClient.createBucket创建Bucket。如下代码展示如何新建一个Bucket:

  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. // 创建bucket
  9. String bucketName = "<your-oss-bucket-name>";
  10. ossClient.createBucket(bucketName);
  11. // 关闭client
  12. ossClient.shutdown();

上面创建的BUCKET,权限是私有读写,存储类型是标准类型standrd.创建bucks时可以指定Bucket权限和存储类型

  1. CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
  2. // 设置bucket权限为公共读,默认是私有读写
  3. createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
  4. // 设置bucket存储类型为低频访问类型,默认是标准类型
  5. createBucketRequest.setStorageClass(StorageClass.IA);
  6. ossClient.createBucket(createBucketRequest);

删除Bucket

您可以使用OSSClient.deleteBucket删除Bucket。以下代码展示如何删除一个Bucket:

  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // accessKey请登录https://ak-console.aliyun.com/#/查看
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. // 删除bucket
  9. ossClient.deleteBucket("<bucketName>");
  10. // 关闭client
  11. ossClient.shutdown();

提示:

  • 如果存储空间不为空(存储空间中有文件或者分片上传碎片),则存储空间无法删除;
  • 必须先删除存储空间中的所有文件后,存储空间才能成功删除。

判断Bucket是否存在

您可以使用OSSClient.doesBucketExist接口判断该Bucket是否已存在。以下代码展示如何判断指定Bucket是否存在:

  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // accessKey请登录https://ak-console.aliyun.com/#/查看
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. boolean exists = ossClient.doesBucketExist("<bucketName>");
  9. // 关闭client
  10. ossClient.shutdown();

设置Bucket ACL

Bucket的ACL包含三类:Private(私有读写), PublicRead(公共读私有写), PublicReadWrite(公共读写)。您可以通过OSSClient.setBucketAcl设置bucket的权限。

权限Java SDK对应值
私有读写CannedAccessControlList.Private
公共读私有写CannedAccessControlList.PublicRead
公共读写CannedAccessControlList.PublicReadWrite

以下代码展示如何设置Bucket的权限:

  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // accessKey请登录https://ak-console.aliyun.com/#/查看
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. // 设置bucket权限
  9. ossClient.setBucketAcl("<bucketName>", CannedAccessControlList.Private);
  10. // 关闭client
  11. ossClient.shutdown();

获取Bucket ACL

您可以通过OSSClient.getBucketAcl获取bucket的权限。以下代码展示如何获取Bucket的ACL:

  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // accessKey请登录https://ak-console.aliyun.com/#/查看
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. AccessControlList acl = ossClient.getBucketAcl("<bucketName>");
  9. // Bucket权限
  10. System.out.println(acl.toString());
  11. // 关闭client
  12. ossClient.shutdown();

获取Bucket Location

Bucket Location即Bucket Region,详细请参看基本概念中的Region(区域)。

您可以通过OSSClient.getBucketLocation获取bucket的权限。以下代码展示如何获取Bucket的Location:

  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // accessKey请登录https://ak-console.aliyun.com/#/查看
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. String location = ossClient.getBucketLocation("<bucketName>");
  9. System.out.println(location);
  10. // 关闭client
  11. ossClient.shutdown();

获取Bucket Info

Bucket的Info包括Location、CreationDate、Owner及权限等信息。以下代码展示如何获取Bucket的Info:

  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // accessKey请登录https://ak-console.aliyun.com/#/查看
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. BucketInfo info = ossClient.getBucketInfo("<bucketName>");
  9. // Location
  10. info.getBucket().getLocation();
  11. // 创建日期
  12. info.getBucket().getCreationDate();
  13. // owner
  14. info.getBucket().getOwner();
  15. // 权限
  16. info.getGrants();
  17. // 关闭client
  18. ossClient.shutdown();

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页