Manage objects

Last Updated: May 31, 2017

List all objects in a bucket

  1. OSSGetBucketRequest * getBucket = [OSSGetBucketRequest new];
  2. getBucket.bucketName = @"<bucketName>";
  3. // Optional parameter. To see its definition, go to https://docs.aliyun.com/#/pub/oss/api-reference/bucket&GetBucket
  4. // getBucket.marker = @"";
  5. // getBucket.prefix = @"";
  6. // getBucket.delimiter = @"";
  7. OSSTask * getBucketTask = [client getBucket:getBucket];
  8. [getBucketTask continueWithBlock:^id(OSSTask *task) {
  9. if (!task.error) {
  10. OSSGetBucketResult * result = task.result;
  11. NSLog(@"get bucket success!");
  12. for (NSDictionary * objectInfo in result.contents) {
  13. NSLog(@"list object: %@", objectInfo);
  14. }
  15. } else {
  16. NSLog(@"get bucket failed, error: %@", task.error);
  17. }
  18. return nil;
  19. }];

The following table lists the configurable parameters and their functions related to the LIST operation:

Name Function
delimiter A character to group object names. All the objects whose names contain the specified prefix and appear before the delimiter for the first time form a group of elements: CommonPrefixes.
marker Set the returned results to begin from the first entry after the marker in alphabetical order.
maxkeys Limit the maximum number of objects returned for one request. If not specified, the default value is 100. The maxkeys value cannot exceed 1,000.
prefix The keys of returned objects must use the prefix. Note that when you make a query using the prefix, the returned key will still contain the prefix.

Check object availability

The OSS iOS SDK provides a synchronous interface to check whether a specified object exists on the OSS.

  1. NSError * error = nil;
  2. BOOL isExist = [client doesObjectExistInBucket:TEST_BUCKET withObjectKey:@"file1m" withError:&error];
  3. if (!error) {
  4. if(isExist) {
  5. NSLog(@"File exists.");
  6. } else {
  7. NSLog(@"File not exists.");
  8. }
  9. } else {
  10. NSLog(@"Error!");
  11. }

Copy an object

  1. OSSCopyObjectRequest * copy = [OSSCopyObjectRequest new];
  2. copy.bucketName = @<bucketName>;
  3. copy.objectKey = @"<objectKey>";
  4. copy.sourceCopyFrom = [NSString stringWithFormat:@"/%@/%@", @"<bucketName>, @"<objectKey_copyFrom>"];
  5. OSSTask * task = [client copyObject:copy];
  6. [task continueWithBlock:^id(OSSTask *task) {
  7. if (!task.error) {
  8. // ...
  9. }
  10. return nil;s
  11. }];

When you use the above code to copy an object, note the following points:

  • The source object and target object must belong to the same data center.
  • In the COPY operation, if the source object address and target object address are the same, you can modify the meta information of the existing object.
  • The object to be copied must not exceed 1 GB. Otherwise, you should upload it through multipart upload.

Delete an object

To use the following code to delete an object through DeleteObject, you must have the write permission on the bucket that stores the object.

  1. OSSDeleteObjectRequest * delete = [OSSDeleteObjectRequest new];
  2. delete.bucketName = @"<bucketName>";
  3. delete.objectKey = @"<objectKey>";
  4. OSSTask * deleteTask = [client deleteObject:delete];
  5. [deleteTask continueWithBlock:^id(OSSTask *task) {
  6. if (!task.error) {
  7. // ...
  8. }
  9. return nil;
  10. }];
  11. // [deleteTask waitUntilFinished];

Get the metadata of an object only

The following code is used to get the metadata of an object:

  1. OSSHeadObjectRequest * head = [OSSHeadObjectRequest new];
  2. head.bucketName = @"<bucketName>";
  3. head.objectKey = @"<objectKey>";
  4. OSSTask * headTask = [client headObject:head];
  5. [headTask continueWithBlock:^id(OSSTask *task) {
  6. if (!task.error) {
  7. OSSHeadObjectResult * headResult = task.result;
  8. NSLog(@"all response header: %@", headResult.httpResponseHeaderFields);
  9. // some object properties include the 'x-oss-meta-*'s
  10. NSLog(@"head object result: %@", headResult.objectMeta);
  11. } else {
  12. NSLog(@"head object error: %@", task.error);
  13. }
  14. return nil;
  15. }];
Thank you! We've received your feedback.