edit-icon download-icon

Manage objects

Last Updated: Jan 23, 2018

A bucket can have a lot of objects. The SDK provides a series of APIs 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: Only list objects with specific prefixes.
  • marker: Only list objects with names after the specified marker.
  • delimiter: Obtain the common prefix of a group of objects.
  • max-keys: Specify the maximum number of objects to be 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. // If no parameter is specified, 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 that are prefixed with "my-".
  20. var result = yield client.list({
  21. prefix: 'my-'
  22. });
  23. console.log(result);
  24. // List all objects whose names are prefixed with "my-" and after the marker of "my-object".
  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 bythe key of the object, and they are not hierarchical. Such a structure allows users to efficiently store objects in OSS,but some users may want to manage objects in a conventional way by putting different types of objectsunder different “directories”. OSS provides a “Common Prefix” feature to convenientlycreate a stimulated directory structure. For more information about the common prefix, see 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 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 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 meta information

When uploading an object to OSS, you can specify certain property information about the object in addition to the content. The property information is called “meta information”,which is stored together with the object during uploading.

Because the object meta information is stored in HTTP headers during object uploading, the meta information cannot containany complex characters according to HTTP. Therefore, the meta information must only contain simple printable ASCII characters and cannot contain line breaks. Allmeta information cannot exceed 8 KB.

You can specify the object meta information by specifying the meta parameter when using the putand 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.multipartUpload('object-key', 'local-file', {
  11. meta: {
  12. year: 2017,
  13. people: 'mary'
  14. }
  15. });
  16. console.log(result);
  17. }).catch(function (err) {
  18. console.log(err);
  19. });

Delete objects

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 batch

The following code uses deleteMulti to delete objects in batch. You can set the quiet parameter todetermine 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.