Manage buckets

Last Updated: Oct 26, 2017

Create a bucket

  1. OSSCreateBucketRequest * create = [OSSCreateBucketRequest new];
  2. create.bucketName = @"<bucketName>";
  3. create.xOssACL = @"public-read";
  4. create.location = @"oss-cn-hangzhou";
  5. OSSTask * createTask = [client createBucket:create];
  6. [createTask continueWithBlock:^id(OSSTask *task) {
  7. if (!task.error) {
  8. NSLog(@"create bucket success!");
  9. } else {
  10. NSLog(@"create bucket failed, error: %@", task.error);
  11. }
  12. return nil;
  13. }];

The preceding code specifies the ACL and data center for the created bucket.

  • Each user can have a maximum 30 buckets.

  • The name of each bucket is globally unique. If the name of a newly created bucket is the same as an in-service bucket belonging to another user, the creation fails.

  • When you create a bucket, you can configure the bucket ACL. If no ACL is configured, the bucket permission is private, by default.

  • The result of successful bucket creation is returned to the data center where the bucket is located.

List all buckets

  1. OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
  2. OSSTask * getServiceTask = [client getService:getService];
  3. [getServiceTask continueWithBlock:^id(OSSTask *task) {
  4. if (!task.error) {
  5. OSSGetServiceResult * result = task.result;
  6. NSLog(@"buckets: %@", result.buckets);
  7. NSLog(@"owner: %@, %@", result.ownerId, result.ownerDispName);
  8. [result.buckets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  9. NSDictionary * bucketInfo = obj;
  10. NSLog(@"BucketName: %@", [bucketInfo objectForKey:@"Name"]);
  11. NSLog(@"CreationDate: %@", [bucketInfo objectForKey:@"CreationDate"]);
  12. NSLog(@"Location: %@", [bucketInfo objectForKey:@"Location"]);
  13. }];
  14. }
  15. return nil;
  16. }];

The preceding code returns all buckets of the requester.

Note: Anonymous users do not have the permission to perform the LIST operation.

List all objects in a bucket

  1. OSSGetBucketRequest * getBucket = [OSSGetBucketRequest new];
  2. getBucket.bucketName = @"<bucketName>";
  3. // getBucket.marker = @"";
  4. // getBucket.prefix = @"";
  5. // getBucket.delimiter = @"";
  6. OSSTask * getBucketTask = [client getBucket:getBucket];
  7. [getBucketTask continueWithBlock:^id(OSSTask *task) {
  8. if (!task.error) {
  9. OSSGetBucketResult * result = task.result;
  10. NSLog(@"get bucket success!");
  11. for (NSDictionary * objectInfo in result.contents) {
  12. NSLog(@"list object: %@", objectInfo);
  13. }
  14. } else {
  15. NSLog(@"get bucket failed, error: %@", task.error);
  16. }
  17. return nil;
  18. }];

The preceding code lists all objects in a bucket.

  • To list all objects in a bucket, you must have the permission to access the bucket.

  • You can set parameter prefix, marker, delimiter, and max-keys for listing to return the expected results only.

Delete a bucket

  1. OSSDeleteBucketRequest * delete = [OSSDeleteBucketRequest new];
  2. delete.bucketName = @"<bucketName>";
  3. OSSTask * deleteTask = [client deleteBucket:delete];
  4. [deleteTask continueWithBlock:^id(OSSTask *task) {
  5. if (!task.error) {
  6. NSLog(@"delete bucket success!");
  7. } else {
  8. NSLog(@"delete bucket failed, error: %@", task.error);
  9. }
  10. return nil;
  11. }];

The preceding code deletes a bucket.

  • Only the bucket owner has the permission to delete the bucket.

  • To prevent accidental deletion, OSS does not allow users to delete a non-empty bucket.

Thank you! We've received your feedback.