Bu konu, Object Storage Service (OSS) paketindeki tüm nesnelerin, belirli sayıda nesnenin ve adları belirli bir ön ek içeren nesnelerin nasıl listeleneceğini açıklar.
Yöntemler
Bir paketteki en fazla 1.000 nesneyi tek seferde listelemek için list veya listV2 yöntemini çağırabilirsiniz. Nesneleri farklı şekillerde listelemek üzere parametreleri yapılandırabilirsiniz. Örneğin, listeleme işlemini belirli bir başlangıç noktasından başlatacak, belirli bir dizindeki nesneleri ve alt dizinleri listeleyecek veya nesneleri sayfa sayfa gösterecek parametreleri ayarlayabilirsiniz. Aşağıda list ve listV2 yöntemleri arasındaki farklar açıklanmaktadır:
Nesneleri listelemek için
listyöntemini çağırdığınızda, nesne sahiplerine ilişkin bilgiler varsayılan olarak döndürülür.-
Nesneleri listelemek için
listV2yöntemini çağırdığınızda, yanıtta nesne sahiplerine ilişkin bilgilerin dahil edilip edilmeyeceğini belirtmek için fetch-owner parametresini yapılandırmanız gerekir.NotSürüm oluşturulmuş bir paketteki nesneleri listelemek için
listV2yöntemini çağırmanızı öneririz.
Aşağıdaki tablolarda, nesneleri listelemek amacıyla list ve listV2 yöntemlerini çağırırken yapılandırabileceğiniz parametreler açıklanmaktadır.
-
Nesneleri listelemek için list yöntemini çağırma
Aşağıdaki tabloda, nesneleri listelemek için list yöntemini çağırırken yapılandırabileceğiniz parametreler açıklanmaktadır.
Parametre
Tür
Açıklama
prefix
string
Listelenen nesnelerin adlarında bulunması gereken ön ek.
delimiter
string
Nesneleri ada göre gruplamak için kullanılan karakter.
marker
string
Listeleme işleminin başladığı nesnenin adı. Bu parametre belirtilirse, marker parametresinin değerinden alfabetik olarak sonra gelen adlara sahip nesneler döndürülür.
max-keys
number | string
Döndürülen maksimum nesne sayısı.
encoding-type
'url' | ''
Yanıttaki nesne adlarının URL kodlamalı olduğunu belirtir.
-
Nesneleri listelemek için listV2 yöntemini çağırma
Aşağıdaki tabloda, nesneleri listelemek için listV2 yöntemini çağırırken yapılandırabileceğiniz parametreler açıklanmaktadır.
Parametre
Tür
Açıklama
prefix
string
Listelenen nesnelerin adlarında bulunması gereken ön ek.
continuation-token
string
Nesne listesinin alındığı konumu belirten belirteç.
delimiter
string
Nesneleri ada göre gruplamak için kullanılan karakter.
max-keys
number | string
Döndürülen maksimum nesne sayısı.
start-after
string
Listeleme işleminin başladığı nesnenin adı. Bu parametre belirtilirse, start-after parametresinin değerinden alfabetik olarak sonra gelen adlara sahip nesneler döndürülür.
fetch-owner
boolean
Yanıtta nesne sahiplerine ilişkin bilgilerin dahil edilip edilmeyeceğini belirtir.
encoding-type
'url' | ''
Yanıttaki nesne adlarının URL kodlamalı olduğunu belirtir.
Basit listeleme kullanarak nesneleri listeleme
Aşağıdaki örnek kod, belirtilen bir paketteki nesnelerin nasıl listeleneceğini gösterir. Varsayılan olarak en fazla 100 nesne listelenir.
-
Nesneleri listelemek için list yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); async function list () { // By default, if no parameter is specified, up to 100 objects can be returned. const result = await client.list(); console.log(result); } list(); -
Nesneleri listelemek için listV2 yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); async function list () { // By default, if no parameter is specified, up to 100 objects can be returned. const result = await client.listV2(); console.log(result); } list();
Bir paketteki belirli sayıda nesneyi listeleme
Aşağıdaki örnek kod, max-keys parametresi yapılandırılarak bir paketteki belirli sayıda nesnenin nasıl listeleneceğini gösterir.
-
Nesneleri listelemek için list yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); async function list () { const result = await client.list({ // Specify that the first 10 objects in alphabetical order are returned. "max-keys": 10 }); console.log(result); } list(); -
Nesneleri listelemek için listV2 yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); async function list () { const result = await client.listV2({ // Specify that the first 10 objects in alphabetical order are returned. "max-keys": 10 }); console.log(result); } list();
Adları belirtilen ön eki içeren nesneleri listeleme
Aşağıdaki örnek kod, prefix parametresi yapılandırılarak bir pakette adları belirtilen ön eki içeren nesnelerin nasıl listeleneceğini gösterir.
-
Nesneleri listelemek için list yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); async function list () { const result = await client.list({ // List 10 objects. "max-keys": 10, // List objects whose names contain the foo/ prefix. prefix: 'foo/' }); console.log(result); } list(); -
Nesneleri listelemek için listV2 yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); async function list () { const result = await client.listV2({ // List 10 objects. "max-keys": 10, // List objects whose names contain the foo/ prefix. prefix: 'foo/' }); console.log(result); } list();
Adları alfabetik olarak belirtilen nesne adından sonra gelen nesneleri listeleme
Aşağıdaki örnek kod, marker veya startAfter parametresiyle belirtilen bir dizeden alfabetik olarak sonra gelen adlara sahip nesnelerin nasıl listeleneceğini gösterir.
-
Nesneleri listelemek için list yöntemini çağırma
Listeleme işleminin başladığı nesnenin adı marker parametresiyle belirtilir.
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // List the objects whose names are alphabetically after the object name test. By default, up to 100 objects are listed. const marker = 'test' async function list () { const result = await client.list({ marker }); console.log(result); } list(); -
Nesneleri listelemek için listV2 yöntemini çağırma
Listeleme işleminin başladığı nesnenin adı startAfter parametresiyle belirtilir.
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); async function list () { const result = await client.listV2({ // List the objects and subfolders whose names are alphabetically after a/b in the a/ folder. delimiter: '/', prefix: 'a/', 'start-after': 'a/b' }); console.log(result.objects, result.prefixes); } list();
Bir paketteki tüm nesneleri sayfa sayfa listeleme
Aşağıdaki örnek kod, belirtilen bir paketteki tüm nesnelerin sayfa sayfa nasıl listeleneceğini gösterir. Her sayfada listelenecek maksimum nesne sayısını belirtmek için max-keys parametresini yapılandırabilirsiniz.
-
Nesneleri listelemek için list yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); let marker = null; // List up to 20 objects on each page. const maxKeys = 20; async function list () { do { const result = await client.list({ marker: marker, 'max-keys': maxKeys }); marker = result.nextMarker; console.log(result); } while (marker); } list(); -
Nesneleri listelemek için listV2 yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); async function list () { let continuationToken = null; // List up to 20 objects on each page. const maxKeys = 20; do { const result = await client.listV2({ 'continuation-token': continuationToken, 'max-keys': maxKeys }); continuationToken = result.nextContinuationToken; console.log(result); }while(continuationToken) } list();
Nesneleri ve sahip bilgilerini listeleme
Aşağıdaki örnek kod, nesnelerin ve sahip bilgilerinin nasıl listeleneceğini gösterir:
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',
// Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
// By default, the information about the object owners is not listed. To include the object owner information in the response, you must set the fetch-owner parameter to true.
async function list () {
const result = await client.listV2({
'fetch-owner': true
});
console.log(result.objects);
}
list();
Belirtilen bir klasördeki nesneleri ve alt klasörleri listeleme
OSS, nesneleri depolamak için düz bir yapı kullanır. Dizin, adı eğik çizgi (/) ile biten sıfır baytlık bir nesnedir. Bu nesneyi yükleyebilir ve indirebilirsiniz. Varsayılan olarak, adı eğik çizgi (/) ile biten bir nesne OSS konsolunda dizin olarak görüntülenir. Dizin oluşturmaya yönelik tam örnek kod için GitHub adresini ziyaret edin.
İstekte prefix değerini bir dizin adı olarak ayarlarsanız, adları bu ön eki içeren nesneler ve alt dizinler listelenir.
İstekte bir ön ek belirtir ve delimiter değerini eğik çizgi (/) olarak ayarlarsanız, dizindeki adı belirtilen ön ekle başlayan nesneler ve alt dizinler listelenir. Her alt dizin, CommonPrefixes içinde tek bir sonuç öğesi olarak listelenir. Bu alt dizinlerdeki nesneler ve dizinler listelenmez.
Bir pakette aşağıdaki nesnelerin depolandığını varsayalım:
foo/x
foo/y
foo/bar/a
foo/bar/b
foo/hello/C/1
foo/hello/C/2
...
foo/hello/C/9999
Aşağıdaki örnek kod, belirtilen bir klasördeki nesneleri ve alt klasörleri listelemek için list veya listV2 yönteminin nasıl çağrılacağını gösterir.
-
Nesneleri listelemek için list yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // Call the listDir function and configure different prefixes to list the required objects. async function listDir(dir) { try { const result = await client.list({ prefix: dir, delimiter: '/' }); if (result && result.prefixes) { result.prefixes.forEach(subDir => { console.log('SubDir: %s', subDir); }); } if (result && result.objects) { result.objects.forEach(obj => { console.log('Object: %s', obj.name); }); } } catch (e) { console.log(e); } } listDir('foo/'); // Expected result: // SubDir: foo/bar/ // SubDir: foo/hello/ // Object: foo/x // Object: foo/y listDir('foo/bar/'); // Expected result: // Object: foo/bar/a // Object: foo/bar/b listDir('foo/hello/C/'); // Expected result: // Object: foo/hello/C/1 // Object: foo/hello/C/2 // ... // Object: foo/hello/C/9999 -
Nesneleri listelemek için listV2 yöntemini çağırma
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', // Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // Call the listV2Dir function and configure different prefixes to list the required objects. async function listV2Dir(dir) { try { const result = await client.listV2({ prefix: dir, delimiter: '/' }); if (result && result.prefixes) { result.prefixes.forEach(subDir => { console.log('SubDir: %s', subDir); }); } if (result && result.objects) { result.objects.forEach(obj => { console.log('Object: %s', obj.name); }); } } catch (e) { console.log(e); } } listDir('foo/'); // Expected result: // SubDir: foo/bar/ // SubDir: foo/hello/ // Object: foo/x // Object: foo/y listDir('foo/bar/'); // Expected result: // Object: foo/bar/a // Object: foo/bar/b listDir('foo/hello/C/'); // Expected result: // Object: foo/hello/C/1 // Object: foo/hello/C/2 // ... // Object: foo/hello/C/9999
İlgili konular
Nesneleri listelemeye yönelik tam örnek kod için GitHub adresini ziyaret edin.
Nesneleri listelemek için çağırabileceğiniz API işlemleri hakkında daha fazla bilgi için bkz. ListObjects (GetBucket) ve ListObjectsV2 (GetBucketV2).