Manage objects

Last Updated: Jun 08, 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 ‘list’ 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 a group of objects
  • max-keys: Specify the maximum number of objects returned
  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. // Without any parameters. A maximum of 1,000 objects can be returned by default.
  11. var result = yield client.list();
  12. console.log(result);
  13. // Continue to list objects based on nextMarker
  14. if (result.isTruncated) {
  15. var result = yield client.list({
  16. marker: result.nextMarker
  17. });
  18. }
  19. // List all objects whose names contain the "my-" prefix
  20. var result = yield client.list({
  21. prefix: 'my-'
  22. });
  23. console.log(result);
  24. // List all objects whose names are greater than "my-object" marker and contain the "my-" prefix
  25. var result = yield client.list({
  26. prefix: 'my-',
  27. marker: 'my-object'
  28. });
  29. console.log(result);
  30. }).catch(function (err) {
  31. console.log(err);
  32. });

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. There is no hierarchical structure 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 public prefix concept, refer to List Object.

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 called ‘listDir’ to list all the objects and subdirectories under the specified directory:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. function* listDir(dir)
  10. var result = yield client.list({
  11. prefix: dir,
  12. delimiter: '/'
  13. });
  14. result.prefixes.forEach(function (subDir) {
  15. console.log('SubDir: %s', subDir);
  16. });
  17. result.objects.forEach(function (obj) {
  18. console.log(Object: %s', obj.name);
  19. });
  20. end

The execution results are as follows:

  1. > yield listDir('foo/')
  2. => SubDir: foo/bar/
  3. SubDir: foo/hello/
  4. Object: foo/x
  5. Object: foo/y
  6. > yield listDir('foo/bar/')
  7. => Object: foo/bar/a
  8. Object: foo/bar/b
  9. > yield listDir('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.

Because the object meta information is stored in HTTP headers during object uploading/downloading, the meta information cannot contain any complex characters according to HTTP. Therefore, the meta information should only contain simple printable ASCII characters and cannot contain line breaks. The maximum size of all meta information cannot exceed 8KB.

You can specify the object meta information by specifying the ‘meta’ parameter when using the ‘put’, ‘putStream’ and ‘multipartUpload’:

  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. var result = yield client.put('object-key', 'local-file', {
  11. meta: {
  12. year: 2016,
  13. people: 'mary'
  14. }
  15. });
  16. console.log(result);
  17. }).catch(function (err) {
  18. console.log(err);
  19. });

Update the object meta information through the ‘putMeta’ interface:

  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. var result = yield client.putMeta('object-key', {
  11. meta: {
  12. year: 2015,
  13. people: 'mary'
  14. }
  15. });
  16. console.log(result);
  17. }).catch(function (err) {
  18. console.log(err);
  19. });

Copy an object

Copy an object using ‘copy’. The copy may fall under the following two types of scenarios:

  • The same bucket
  • Two different buckets in the region. In this case, the source object name should be in the format of‘/bucket/object’.

In addition, when an object is copied, there are two methods for processing the object metadata:

  • If no ‘meta’ parameter is specified, the metadata remains the same as that of the source object, that is, copying the source object metadata
  • If the ‘meta’ parameter is specified, overwrite the source object information with the new metadata
  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. // Copying between two buckets
  11. var result = yield client.copy('to', '/from-bucket/from');
  12. console.log(result);
  13. // Copy the meta information
  14. var result = yield client.copy('to', 'from');
  15. console.log(result);
  16. // Overwrite the meta information
  17. var result = yield client.copy('to', 'from', {
  18. meta: {
  19. year: 2015,
  20. people: 'mary'
  21. }
  22. });
  23. console.log(result);
  24. }).catch(function (err) {
  25. console.log(err);
  26. });

Delete an object

You can use ‘delete’ to delete an object:

  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. var result = yield client.delete('object-key');
  11. console.log(result);
  12. }).catch(function (err) {
  13. console.log(err);
  14. });

Delete objects in batches

The following code uses ‘deleteMulti’ to delete objects in batches. You can set the ‘quiet’ parameter to determine whether to return the deletion results:

  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. var result = yield client.deleteMulti(['obj-1', 'obj-2', 'obj-3']);
  11. console.log(result);
  12. var result = yield client.deleteMulti(['obj-1', 'obj-2', 'obj-3'], {
  13. quiet: true
  14. });
  15. console.log(result);
  16. }).catch(function (err) {
  17. console.log(err);
  18. });
Thank you! We've received your feedback.