Bu konu, bir paket içindeki tüm nesnelerin, belirli sayıda nesnenin ve adları belirli bir önek içeren nesnelerin nasıl listeleneceğini açıklar.
Kullanım notları
Bu konudaki örnek kodu çalıştırmadan önce, özel alan adı veya Security Token Service (STS) gibi yöntemleri kullanarak bir OSSClient örneği oluşturmalısınız. Daha fazla bilgi için bkz. Başlatma (Android SDK).
Belirli sayıda nesneyi listeleme
Aşağıdaki örnek kod, examplebucket adlı bir paketteki 20 nesnenin nasıl listeleneceğini gösterir:
// Specify the bucket name. For example, examplebucket.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the maximum number of objects to return. If this parameter is not set, the default value is 100. The value of maxKeys cannot be greater than 1,000.
request.setMaxKeys(20);
oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
@Override
public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
Log.i("ListObjects", objectSummary.getKey());
}
}
@Override
public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
// Handle request exceptions.
if (clientException != null) {
// Handle client-side exceptions, such as network errors.
clientException.printStackTrace();
}
if (serviceException != null) {
// Handle server-side exceptions.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Adları belirli bir önek içeren nesneleri listeleme
Aşağıdaki örnek kod, examplebucket adlı bir pakette dosya önekiyle başlayan nesnelerin nasıl listeleneceğini gösterir:
// Specify the bucket name. For example, examplebucket.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the prefix.
// Returns all objects whose names start with the specified prefix. For example, a prefix of 'a' returns objects such as abc.txt and abcd.jpg.
request.setPrefix("file");
oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
@Override
public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
Log.i("ListObjects", objectSummary.getKey());
}
}
@Override
public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
// Handle request exceptions.
if (clientException != null) {
// Handle client-side exceptions, such as network errors.
clientException.printStackTrace();
}
if (serviceException != null) {
// Handle server-side exceptions.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Marker değerinden sonra alfabetik sıraya göre gelen nesneleri listeleme
Aşağıdaki örnek kod, examplebucket adlı bir pakette exampleobject.txt nesnesinden sonra alfabetik sıraya göre gelen nesnelerin nasıl listeleneceğini gösterir:
// Specify the bucket name. For example, examplebucket.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Lists objects that alphabetically follow the marker.
// If the marker does not correspond to an existing object, the list starts from the alphabetical position of the marker's value.
request.setMarker("exampleobject.txt");
oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
@Override
public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
Log.i("ListObjects", objectSummary.getKey());
}
}
@Override
public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
// Handle request exceptions.
if (clientException != null) {
// Handle client-side exceptions, such as network errors.
clientException.printStackTrace();
}
if (serviceException != null) {
// Handle server-side exceptions.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Bir paketteki tüm nesneleri sayfalara bölerek listeleme
Aşağıdaki örnek kod, examplebucket adlı bir paketteki tüm nesnelerin sayfalara bölünerek nasıl listeleneceğini gösterir. Her sayfada en fazla 20 nesne listelenebilir.
private String marker = null;
private boolean isCompleted = false;
// List all objects by page.
public void getAllObject() {
do {
OSSAsyncTask task = getObjectList();
// Block the thread to get the NextMarker. To fetch the next page, set the marker to the NextMarker value from the previous response. No marker is needed for the first page.
// This example blocks the thread in a loop to get the NextMarker for pagination. In a production environment, decide whether to block the thread based on your application's requirements.
task.waitUntilFinished();
} while (!isCompleted);
}
// List one page of objects.
public OSSAsyncTask getObjectList() {
// Specify the bucket name.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the maximum number of objects to return per page. If this parameter is not set, the default value is 100. The value of maxKeys cannot be greater than 1,000.
request.setMaxKeys(20);
request.setMarker(marker);
OSSAsyncTask task = oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
@Override
public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
Log.i("ListObjects", objectSummary.getKey());
}
// Last page of results.
if (!result.isTruncated()) {
isCompleted = true;
return;
}
// The marker for the next page of results.
marker = result.getNextMarker();
}
@Override
public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
isCompleted = true;
// Handle request exceptions.
if (clientException != null) {
// Handle client-side exceptions, such as network errors.
clientException.printStackTrace();
}
if (serviceException != null) {
// Handle server-side exceptions.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
return task;
}
Adları belirli bir önek içeren nesneleri sayfalara bölerek listeleme
Aşağıdaki örnek kod, examplebucket adlı bir pakette dosya önekiyle başlayan nesnelerin sayfalara bölünerek nasıl listeleneceğini gösterir. Her sayfada en fazla 20 nesne listelenebilir.
private String marker = null;
private boolean isCompleted = false;
// List all objects with the specified prefix by page.
public void getAllObject() {
do {
OSSAsyncTask task = getObjectList();
// Block the thread to get the NextMarker. To fetch the next page, set the marker to the NextMarker value from the previous response. No marker is needed for the first page.
// This example blocks the thread in a loop to get the NextMarker for pagination. In a production environment, decide whether to block the thread based on your application's requirements.
task.waitUntilFinished();
} while (!isCompleted);
}
// List one page of objects.
public OSSAsyncTask getObjectList() {
// Specify the bucket name.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the maximum number of objects to return per page. If this parameter is not set, the default value is 100. The value of maxKeys cannot be greater than 1,000.
request.setMaxKeys(20);
// Specify the prefix.
// Returns all objects whose names start with the specified prefix. For example, a prefix of 'a' returns objects such as abc.txt and abcd.jpg.
request.setPrefix("file");
request.setMarker(marker);
OSSAsyncTask task = oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
@Override
public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
Log.i("ListObjects", objectSummary.getKey());
}
// Last page of results.
if (!result.isTruncated()) {
isCompleted = true;
return;
}
// The marker for the next page of results.
marker = result.getNextMarker();
}
@Override
public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
isCompleted = true;
// Handle request exceptions.
if (clientException != null) {
// Handle client-side exceptions, such as network errors.
clientException.printStackTrace();
}
if (serviceException != null) {
// Handle server-side exceptions.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
return task;
}
Adları özel karakterler içeren nesneleri listeleme
Bir nesnenin adı aşağıdaki özel karakterlerden birini içeriyorsa, nesneyi aktarmadan önce nesne adını kodlamanız gerekir. OSS yalnızca URL kodlamasını destekler.
Tek tırnak işareti (')
Çift tırnak işareti (")
Ve işareti (&)
Açı parantezleri (<>)
Çince karakterler
Aşağıdaki örnek kod, examplebucket adlı bir pakette özel karakterler içeren nesnelerin nasıl listeleneceğini gösterir:
// Specify the bucket name. For example, examplebucket.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the encoding type for object names.
request.setEncodingType("url");
oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
@Override
public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
Log.i("ListObjects", URLDecoder.decode(objectSummary.getKey(), "UTF-8"));
}
}
@Override
public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
// Handle request exceptions.
if (clientException != null) {
// Handle client-side exceptions, such as network errors.
clientException.printStackTrace();
}
if (serviceException != null) {
// Handle server-side exceptions.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
İlgili kaynaklar
Nesneleri listelemek için çağırabileceğiniz API işlemleri hakkında daha fazla bilgi için bkz. GetBucket (ListObjects) ve ListObjectsV2(GetBucketV2).
Bir OSSClient örneğinin nasıl başlatılacağı hakkında daha fazla bilgi için bkz. Başlatma.