All Products
Search
Document Center

Object Storage Service:Manage directories

Last Updated:Oct 18, 2023

Object Storage Service (OSS) uses a flat structure instead of a hierarchical structure that is used by traditional file systems to store data. All data in OSS are stored as objects in buckets. You can use directories in OSS to help you categorize objects and control access to your objects in a simplified manner.

Create a directory

The following sample code provides an example on how to create a directory:

const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret'
  // Specify the name of the bucket. 
  bucket: 'examplebucket', 
});

async function putBuffer () {
  try {
    // Specify the name of the directory. The directory name must end with a forward slash (/). 
    const result = await client.put('exampledir/', new Buffer(''));
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

putBuffer();

Delete a directory

Warning

If you delete a directory, the subdirectories and all objects in the directory are synchronously deleted. We recommend that you exercise caution when you delete a directory.

The following sample code provides an example on how to delete a directory named log/ and all objects in the directory:

const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
});

// Handle request failures, prevent the interruption of promise.all, and return failure causes and the names of objects that fail to be deleted. 
async function handleDel(name, options) {
  try {
    await client.delete(name);
  } catch (error) {
    error.failObjectName = name;
    return error;
  }
}

// Delete multiple objects at a time. 
async function deletePrefix(prefix) {
  const list = await client.list({
    prefix: prefix,
  });

  list.objects = list.objects || [];
  const result = await Promise.all(list.objects.map((v) => handleDel(v.name)));
  console.log(result);
}
// Delete the directory and all objects in the directory. 
deletePrefix('log/')

References

  • Create a directory

    For more information about the API operation that you can call to create a directory, see PutObject.

  • Delete a directory

    • For the complete sample code that is used to delete a directory and all objects in the directory, visit GitHub.

    • For more information about the API operation that you can call to delete a directory and all objects in the directory, see DeleteObject.