Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Manage directories (Node.js SDK)

Son Güncelleme:Oct 01, 2026

OSS, bir klasör yapısını simüle eden dizinler sağlayarak çok sayıda nesneyi düzenlemenizi, bulmanızı ve toplu işlemler gerçekleştirmenizi kolaylaştırır.

Bir dizin oluşturun

Aşağıdaki kod, bir dizinin nasıl oluşturulacağını gösterir.

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

const client = new OSS({
  // Specify the region where your bucket is located. For example, use 'oss-cn-hangzhou' for the China (Hangzhou) region.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the bucket name.
  bucket: 'examplebucket',
});

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

putBuffer();

Bir dizini silin

Uyarı

Bir dizini silmek, alt dizinlerini ve içindeki tüm nesneleri de siler. Lütfen dikkatli olun.

Aşağıdaki kod, log/ dizinini ve içindeki tüm nesneleri nasıl sileceğinizi gösterir.

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

const client = new OSS({
  // Specify the region where your bucket is located. For example, for the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'yourregion',
  // This example loads access credentials from environment variables. Before running, ensure that `OSS_ACCESS_KEY_ID` and `OSS_ACCESS_KEY_SECRET` are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});

// Handle a failed request to prevent `promise.all` from being interrupted. Returns the failure reason and the name of the object that failed to be deleted.
async function handleDel(name, options) {
  try {
    await client.delete(name);
  } catch (error) {
    error.failObjectName = name;
    return error;
  }
}

// Delete multiple objects.
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 it.
deletePrefix('log/')

İlgili konular

  • Klasör oluşturma

    Klasör oluşturmak için kullanılan API işlemi hakkında daha fazla bilgi için bkz. PutObject.

  • Dizin silme

    • Bir dizini ve içindeki tüm nesneleri silen tam örnek kod için GitHub örneğine

    • Klasörleri ve tüm dosyalarını silmek için kullanılan API işlemi hakkında daha fazla bilgi için bkz. DeleteObject.