このトピックでは、すべてのオブジェクト、特定の数のオブジェクト、および名前に特定のプレフィックスが含まれるオブジェクトをバケットに一覧表示する方法について説明します。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。
オブジェクトを一覧表示するには、
oss:ListObjects
権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。
単純なリストを使用してオブジェクトをリストする
次のサンプルコードは、指定したバケット内の100オブジェクトを一覧表示する方法の例を示しています。
using Aliyun.OSS;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// 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 cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
c.SetRegion(region);
try
{
var listObjectsRequest = new ListObjectsRequest(bucketName);
// List objects in the bucket by using simple list. By default, 100 objects are returned.
var result = client.ListObjects(listObjectsRequest);
Console.WriteLine("List objects succeeded");
foreach (var summary in result.ObjectSummaries)
{
Console.WriteLine("File name:{0}", summary.Key);
}
}
catch (Exception ex)
{
Console.WriteLine("List objects failed. {0}", ex.Message);
}
特定の数のオブジェクトを一覧表示する
次のサンプルコードは、特定の数のオブジェクトを一覧表示する方法の例を示しています。
using Aliyun.OSS;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// 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 cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
c.SetRegion(region);
try
{
var listObjectsRequest = new ListObjectsRequest(bucketName)
{
// Specify MaxKeys to set the maximum number of listed objects to 200. The default value of MaxKeys is 100. The maximum value of MaxKeys is 1000.
MaxKeys = 200,
};
var result = client.ListObjects(listObjectsRequest);
Console.WriteLine("List objects succeeded");
foreach (var summary in result.ObjectSummaries)
{
Console.WriteLine(summary.Key);
}
}
catch (Exception ex)
{
Console.WriteLine("List objects failed, {0}", ex.Message);
}
指定されたプレフィックスを名前に含むオブジェクトを一覧表示する
次のサンプルコードは、名前に特定のプレフィックスが含まれるオブジェクトをバケットに一覧表示する方法の例を示しています。
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// List objects whose names contain the "test" prefix. By default, a maximum of 100 objects are listed.
var prefix = "test";
// 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 cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
c.SetRegion(region);
try
{
var keys = new List<string>();
ObjectListing result = null;
string nextMarker = string.Empty;
do
{
var listObjectsRequest = new ListObjectsRequest(bucketName)
{
Marker = nextMarker,
MaxKeys = 100,
Prefix = prefix,
};
result = client.ListObjects(listObjectsRequest);
foreach (var summary in result.ObjectSummaries)
{
Console.WriteLine(summary.Key);
keys.Add(summary.Key);
}
nextMarker = result.NextMarker;
} while (result.IsTruncated);
Console.WriteLine("List objects of bucket:{0} succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
markerの値の後に名前がアルファベット順に表示されるオブジェクトを一覧表示します。
マーカーパラメーターを設定して、リスト操作が開始されるオブジェクトの名前を指定できます。 次のサンプルコードは、markerパラメーターの値の後に名前がアルファベット順になっているオブジェクトを一覧表示する方法の例を示しています。
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the marker parameter. Example: exampleobject.txt.
var marker = "exampleobject.txt";
// 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 cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
c.SetRegion(region);
try
{
var keys = new List<string>();
ObjectListing result = null;
string nextMarker = marker;
do
{
var listObjectsRequest = new ListObjectsRequest(bucketName)
// You can modify the value of MaxKeys to return more objects or list objects by page.
{
Marker = nextMarker,
MaxKeys = 100,
};
result = client.ListObjects(listObjectsRequest);
foreach (var summary in result.ObjectSummaries)
{
Console.WriteLine(summary.Key);
keys.Add(summary.Key);
}
nextMarker = result.NextMarker;
// If the value of IsTruncated is true, the next read starts from NextMarker.
} while (result.IsTruncated);
Console.WriteLine("List objects of bucket:{0} succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
バケット内のすべてのオブジェクトの一覧表示
次のサンプルコードでは、指定したバケット内のすべてのオブジェクトを一覧表示する方法の例を示します。
using Aliyun.OSS;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// 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 cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
c.SetRegion(region);
// List all objects in the bucket.
public void ListObject(string bucketName)
{
try
{
ObjectListing result = null;
string nextMarker = string.Empty;
do
{
// You can configure the MaxKeys parameter to specify the maximum number of objects that can be listed on each page. If the number of returned objects exceeds the value of MaxKeys, the objects are listed by page.
var listObjectsRequest = new ListObjectsRequest(bucketName)
{
Marker = nextMarker,
MaxKeys = 100
};
result = client.ListObjects(listObjectsRequest);
Console.WriteLine("File:");
foreach (var summary in result.ObjectSummaries)
{
Console.WriteLine("Name:{0}", summary.Key);
}
nextMarker = result.NextMarker;
} while (result.IsTruncated);
}
catch (Exception ex)
{
Console.WriteLine("List object failed. {0}", ex.Message);
}
}
バケット内のオブジェクトを非同期にリストする
次のサンプルコードでは、指定したバケット内のオブジェクトを非同期に一覧表示する方法の例を示します。
using System;
using System.IO;
using System.Threading;
using Aliyun.OSS;
namespace AsyncListObjects
{
class Program
{
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
static string endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
static string bucketName = "yourBucketName";
static AutoResetEvent _event = new AutoResetEvent(false);
// 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 cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
c.SetRegion(region);
static void Main(string[] args)
{
Program.AsyncListObjects();
Console.ReadKey();
}
public static void AsyncListObjects()
{
try
{
var listObjectsRequest = new ListObjectsRequest(bucketName);
client.BeginListObjects(listObjectsRequest, ListObjectCallback, null);
_event.WaitOne();
}
catch (Exception ex)
{
Console.WriteLine("Async list objects failed. {0}", ex.Message);
}
}
// Call the ListObjectCallback method to implement callback after the objects are asynchronously listed. If you call asynchronous operations to list objects, the operations must implement callback in similar methods.
private static void ListObjectCallback(IAsyncResult ar)
{
try
{
var result = client.EndListObjects(ar);
foreach (var summary in result.ObjectSummaries)
{
Console.WriteLine("Object name: {0}", summary.Key);
}
_event.Set();
Console.WriteLine("Async list objects succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Async list objects failed. {0}", ex.Message);
}
}
}
}
関連ドキュメント
オブジェクトを同期的に一覧表示するための完全なサンプルコードについては、『GitHub』をご参照ください。
オブジェクトを非同期で一覧表示するための完全なサンプルコードについては、『GitHub』をご参照ください。
オブジェクトを一覧表示するために呼び出すAPI操作の詳細については、「GetBucket (ListObjects) 」をご参照ください。