创建bucket
- OSSCreateBucketRequest * create = [OSSCreateBucketRequest new];
- create.bucketName = @"<bucketName>";
- create.xOssACL = @"public-read";
- create.location = @"oss-cn-hangzhou";
- OSSTask * createTask = [client createBucket:create];
- [createTask continueWithBlock:^id(OSSTask *task) {
- if (!task.error) {
- NSLog(@"create bucket success!");
- } else {
- NSLog(@"create bucket failed, error: %@", task.error);
- }
- return nil;
- }];
上述代码在创建bucket时,指定了Bucket的ACL和所在的数据中心。
- 每个用户的Bucket数量不能超过30个。
- 每个Bucket的名字全局唯一,也就是说创建的Bucket不能和其他用户已经在使用的Bucket同名,否则会创建失败。
- 创建的时候可以选择Bucket ACL权限,如果不设置ACL,默认是private。
- 创建成功结果返回Bucket所在数据中心。
罗列所有bucket
- OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
- OSSTask * getServiceTask = [client getService:getService];
- [getServiceTask continueWithBlock:^id(OSSTask *task) {
- if (!task.error) {
- OSSGetServiceResult * result = task.result;
- NSLog(@"buckets: %@", result.buckets);
- NSLog(@"owner: %@, %@", result.ownerId, result.ownerDispName);
- [result.buckets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- NSDictionary * bucketInfo = obj;
- NSLog(@"BucketName: %@", [bucketInfo objectForKey:@"Name"]);
- NSLog(@"CreationDate: %@", [bucketInfo objectForKey:@"CreationDate"]);
- NSLog(@"Location: %@", [bucketInfo objectForKey:@"Location"]);
- }];
- }
- return nil;
- }];
上处代码返回请求者拥有的所有Bucket。
罗列bucket中的文件
- OSSGetBucketRequest * getBucket = [OSSGetBucketRequest new];
- getBucket.bucketName = @"<bucketName>";
- // getBucket.marker = @"";
- // getBucket.prefix = @"";
- // getBucket.delimiter = @"";
- OSSTask * getBucketTask = [client getBucket:getBucket];
- [getBucketTask continueWithBlock:^id(OSSTask *task) {
- if (!task.error) {
- OSSGetBucketResult * result = task.result;
- NSLog(@"get bucket success!");
- for (NSDictionary * objectInfo in result.contents) {
- NSLog(@"list object: %@", objectInfo);
- }
- } else {
- NSLog(@"get bucket failed, error: %@", task.error);
- }
- return nil;
- }];
上述代码罗列了Bucket中的文件
- 罗列操作必须具备访问该Bukcet的权限。
- 罗列时,可以通过prefix,marker,delimiter和max-keys对list做限定,返回部分结果。
删除bucket
- OSSDeleteBucketRequest * delete = [OSSDeleteBucketRequest new];
- delete.bucketName = @"<bucketName>";
- OSSTask * deleteTask = [client deleteBucket:delete];
- [deleteTask continueWithBlock:^id(OSSTask *task) {
- if (!task.error) {
- NSLog(@"delete bucket success!");
- } else {
- NSLog(@"delete bucket failed, error: %@", task.error);
- }
- return nil;
- }];
上述代码删除了一个Bucket。
- 只有Bucket的拥有者才能删除这个Bucket。
- 为了防止误删除的发生,OSS不允许用户删除一个非空的Bucket。