edit-icon download-icon

Manage objects

Last Updated: Dec 22, 2017

A bucket may have a lot of objects. The SDK provides a series of interfaces for you to conveniently manage the objects.

View all objects

The following code uses Bucket#list_objects to list all objects in the current bucket. Main parameters are described as follows:

  • :prefix: Specify to only list objects with specific prefixes.
  • :marker: Specify to list objects whose names are greater than a specified marker.
  • :delimiter: Obtain the common prefix of objects
  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. # List all objects
  7. objects = bucket.list_objects
  8. objects.each { |o| puts o.key }
  9. # List all objects with the "my-" prefix in the name
  10. objects = bucket.list_objects(:prefix => 'my-')
  11. objects.each { |o| puts o.key }
  12. # List all objects that follow the "my-object" marker and whose names contain the "my-" prefix
  13. objects = bucket.list_objects(:prefix => 'my-', :marker => 'my-object')
  14. objects.each { |o| puts o.key }

Simulate the directory structure

OSS is an object-based storage service. It has no directory. All objects stored in a bucket are uniquely identified by the key of the object. No hierarchical structure is established among these objects. Such a structure allows OSS to efficiently store objects. However, you may want to put different types of objects under different “directories” to manage objects in a conventional way. OSS provides a “Public Prefix” feature for you to conveniently create a stimulated directory structure. For more information about the concept of a public prefix, see View Object List.

Suppose that the bucket already contains the following objects:

  1. foo/x
  2. foo/y
  3. foo/bar/a
  4. foo/bar/b
  5. foo/hello/C/1
  6. foo/hello/C/2
  7. ...
  8. foo/hello/C/9999

Next we will implement a function named list_dir to list all objects and subdirectories in a specified directory:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. def list_dir(dir)
  7. objects = bucket.list_objects(:prefix => dir, :delimiter => '/')
  8. objects.each do |obj|
  9. if obj.is_a?(OSS::Object) # object
  10. puts "Object: #{obj.key}"
  11. else # common prefix
  12. puts "SubDir: #{obj}"
  13. end
  14. end
  15. end

The execution results are as follows:

  1. > list_dir('foo/')
  2. => SubDir: foo/bar/
  3. SubDir: foo/hello/
  4. Object: foo/x
  5. Object: foo/y
  6. > list_dir('foo/bar/')
  7. => Object: foo/bar/a
  8. Object: foo/bar/b
  9. > list_dir('foo/hello/C/')
  10. => Object: foo/hello/C/1
  11. Object: foo/hello/C/2
  12. ...
  13. Object: foo/hello/C/9999

Object metadata

When uploading an object to OSS, except for the object content, you can specify certain attribute information for the object. The attribute information is called meta information. The metadata is stored together with the object during uploading, and returned together with the object during downloading.

In the SDK, the metadata of an object is expressed by a Hash, and the object key and value are both of the String type, and only contain simple printable ASCII characters and cannot contain line breaks. The total size of all metadata cannot exceed 8 KB.

Note: Because the object meta information is stored in HTTP headers during object uploading/downloading, the meta information cannot contain any complex character according to HTTP.

When using Bucket#put_object, Bucket#append_object, and Bucket#resumable_upload, you can specify the object metadata by specifying the :metas parameter.

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. bucket.put_object(
  7. 'my-object-1',
  8. :file => 'local-file',
  9. :metas => {'year' => '2016', 'people' => 'mary'})
  10. bucket.append_object(
  11. 'my-object-2', 0,
  12. :file => 'local-file',
  13. :metas => {'year' => '2016', 'people' => 'mary'})
  14. bucket.resumable_upload(
  15. 'my-object',
  16. 'local-file',
  17. :metas => {'year' => '2016', 'people' => 'mary'})

Run the Bucket#update_object_metas command to update the metadata of an object.

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. bucket.update_object_metas('my-object', {'year' => '2017'})

Copy an object

The following code uses Bucket#copy_object to copy an object. When an object is copied, two methods can be used to process the object metadata. The method used is specified by the :meta_directive parameter:

  • Method 1: Stay consistent with the source object metadata through the COPY operation.
  • Method 2: Overwrite the source object metadata with the new metadata.
  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. # Copy the source object metadata
  7. bucket.copy_object(
  8. 'my-object', 'copy-object',
  9. :meta_directive => Aliyun::OSS::MetaDirective::COPY)
  10. # Overwrite the source object metadata
  11. bucket.copy_object(
  12. 'my-object', 'copy-object',
  13. :metas => {'year' => '2017'},
  14. :meta_directive => Aliyun::OSS::MetaDirective::REPLACE)

Delete an object

The following code uses Bucket#delete_object to delete an object:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. bucket.delete_object('my-object')

Delete objects in batches

The following code uses Bucket#batch_delete_object to delete objects in batches. You can set the :quiet parameter to determine whether to return the deletion results:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. objs = ['my-object-1', 'my-object-2']
  7. result = bucket.batch_delete_object(objs)
  8. puts result #['my-object-1', 'my-object-2']. Names of successfully deleted objects are returned by default.
  9. objs = ['my-object-3', 'my-object-4']
  10. result = bucket.batch_delete_object(objs, :quiet => true)
  11. puts result #[]. The deletion result is not returned.
Thank you! We've received your feedback.