Topik ini menjelaskan cara menampilkan daftar objek dalam bucket yang telah diaktifkan fitur versioning. Anda dapat menampilkan semua objek, sejumlah objek tertentu, atau objek yang namanya mengandung awalan tertentu.
Catatan penggunaan
Pada topik ini digunakan titik akhir publik wilayah China (Hangzhou). Untuk mengakses OSS dari layanan Alibaba Cloud lainnya dalam wilayah yang sama, gunakan titik akhir internal. Untuk detail wilayah dan titik akhir yang didukung, lihat Wilayah dan titik akhir.
Pada topik ini, kredensial akses diperoleh dari variabel lingkungan. Untuk informasi lebih lanjut tentang cara mengonfigurasi kredensial akses, lihat Konfigurasi kredensial akses.
Pada topik ini, instans OSSClient dibuat menggunakan titik akhir OSS. Jika Anda ingin membuat instans OSSClient menggunakan nama domain kustom atau Security Token Service (STS), lihat Contoh konfigurasi untuk skenario umum.
Untuk menampilkan daftar objek, Anda harus memiliki izin
oss:ListObjectVersions. Untuk informasi lebih lanjut, lihat Lampirkan kebijakan kustom ke RAM user.
Tampilkan semua versi objek dalam bucket
Kode berikut memberikan contoh cara menampilkan semua versi objek, termasuk penanda hapus, dalam sebuah bucket.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Tetapkan titik akhir. Contoh ini menggunakan titik akhir publik wilayah China (Hangzhou). Tentukan titik akhir yang sebenarnya.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Peroleh kredensial akses dari variabel lingkungan. Sebelum menjalankan kode contoh, pastikan variabel lingkungan OSS_ACCESS_KEY_ID dan OSS_ACCESS_KEY_SECRET telah dikonfigurasi.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Tentukan nama bucket. Contoh: examplebucket.
String bucketName = "examplebucket";
// Tentukan wilayah tempat bucket berada. Misalnya, jika bucket berada di wilayah China (Hangzhou), atur wilayah menjadi cn-hangzhou.
String region = "cn-hangzhou";
// Buat instans OSSClient.
// Setelah instans OSSClient tidak digunakan lagi, panggil metode shutdown untuk melepas sumber daya.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Tampilkan versi semua objek, termasuk penanda hapus.
String nextKeyMarker = null;
String nextVersionMarker = null;
VersionListing versionListing = null;
do {
ListVersionsRequest listVersionsRequest = new ListVersionsRequest()
.withBucketName(bucketName)
.withKeyMarker(nextKeyMarker)
.withVersionIdMarker(nextVersionMarker);
versionListing = ossClient.listVersions(listVersionsRequest);
for (OSSVersionSummary ossVersion : versionListing.getVersionSummaries()) {
System.out.println("key name: " + ossVersion.getKey());
System.out.println("versionid: " + ossVersion.getVersionId());
System.out.println("Is latest: " + ossVersion.isLatest());
System.out.println("Is delete marker: " + ossVersion.isDeleteMarker());
}
nextKeyMarker = versionListing.getNextKeyMarker();
nextVersionMarker = versionListing.getNextVersionIdMarker();
} while (versionListing.isTruncated());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}Tampilkan versi objek yang memiliki awalan tertentu
Kode berikut memberikan contoh cara menampilkan versi objek yang memiliki awalan tertentu.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.internal.OSSConstants;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Tetapkan titik akhir. Contoh ini menggunakan titik akhir publik wilayah China (Hangzhou). Tentukan titik akhir yang sebenarnya.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Peroleh kredensial akses dari variabel lingkungan. Sebelum menjalankan kode contoh, pastikan variabel lingkungan OSS_ACCESS_KEY_ID dan OSS_ACCESS_KEY_SECRET telah dikonfigurasi.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Tentukan nama bucket. Contoh: examplebucket.
String bucketName = "examplebucket";
// Tentukan wilayah tempat bucket berada. Misalnya, jika bucket berada di wilayah China (Hangzhou), atur wilayah menjadi cn-hangzhou.
String region = "cn-hangzhou";
// Buat instans OSSClient.
// Setelah instans OSSClient tidak digunakan lagi, panggil metode shutdown untuk melepas sumber daya.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Tampilkan versi objek yang namanya memiliki awalan "test-".
String prefix = "test-";
String nextKeyMarker = null;
String nextVersionMarker = null;
VersionListing versionListing = null;
do {
ListVersionsRequest listVersionsRequest = new ListVersionsRequest()
.withBucketName(bucketName)
.withKeyMarker(nextKeyMarker)
.withVersionIdMarker(nextVersionMarker)
.withEncodingType(OSSConstants.URL_ENCODING)
.withPrefix(prefix);
versionListing = ossClient.listVersions(listVersionsRequest);
// Lihat informasi versi objek.
for (OSSVersionSummary ossVersion : versionListing.getVersionSummaries()) {
System.out.println("key name: " + ossVersion.getKey());
System.out.println("versionid: " + ossVersion.getVersionId());
System.out.println("Is latest: " + ossVersion.isLatest());
System.out.println("Is delete marker: " + ossVersion.isDeleteMarker());
}
nextKeyMarker = versionListing.getNextKeyMarker();
nextVersionMarker = versionListing.getNextVersionIdMarker();
} while (versionListing.isTruncated());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}Tampilkan jumlah tertentu versi objek
Kode berikut memberikan contoh cara menampilkan jumlah tertentu versi objek.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Tetapkan titik akhir. Contoh ini menggunakan titik akhir publik wilayah China (Hangzhou). Tentukan titik akhir yang sebenarnya.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Peroleh kredensial akses dari variabel lingkungan. Sebelum menjalankan kode contoh, pastikan variabel lingkungan OSS_ACCESS_KEY_ID dan OSS_ACCESS_KEY_SECRET telah dikonfigurasi.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Tentukan nama bucket. Contoh: examplebucket.
String bucketName = "examplebucket";
// Tentukan wilayah tempat bucket berada. Misalnya, jika bucket berada di wilayah China (Hangzhou), atur wilayah menjadi cn-hangzhou.
String region = "cn-hangzhou";
// Buat instans OSSClient.
// Setelah instans OSSClient tidak digunakan lagi, panggil metode shutdown untuk melepas sumber daya.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Tentukan bahwa maksimal 200 hasil dikembalikan.
ListVersionsRequest listVersionsRequest = new ListVersionsRequest()
.withBucketName(bucketName)
.withMaxResults(200);
VersionListing versionListing = ossClient.listVersions(listVersionsRequest);
for (OSSVersionSummary ossVersion : versionListing.getVersionSummaries()) {
System.out.println("key name: " + ossVersion.getKey());
// Jika versioning tidak diaktifkan, VersionId bernilai none.
System.out.println("versionid: " + ossVersion.getVersionId());
System.out.println("Is latest: " + ossVersion.isLatest());
System.out.println("Is delete marker: " + ossVersion.isDeleteMarker());
}
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}Tampilkan semua versi objek per halaman
Kode berikut memberikan contoh cara menampilkan semua versi objek per halaman.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Tetapkan titik akhir. Contoh ini menggunakan titik akhir publik wilayah China (Hangzhou). Tentukan titik akhir yang sebenarnya.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Peroleh kredensial akses dari variabel lingkungan. Sebelum menjalankan kode contoh, pastikan variabel lingkungan OSS_ACCESS_KEY_ID dan OSS_ACCESS_KEY_SECRET telah dikonfigurasi.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Tentukan nama bucket. Contoh: examplebucket.
String bucketName = "examplebucket";
// Atur jumlah file yang ditampilkan per halaman menjadi 200.
int maxKeys = 200;
// Tentukan wilayah tempat bucket berada. Misalnya, jika bucket berada di wilayah China (Hangzhou), atur wilayah menjadi cn-hangzhou.
String region = "cn-hangzhou";
// Buat instans OSSClient.
// Setelah instans OSSClient tidak digunakan lagi, panggil metode shutdown untuk melepas sumber daya.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Tampilkan semua versi objek per halaman.
String nextKeyMarker = null;
String nextVersionMarker = null;
VersionListing versionListing = null;
do {
ListVersionsRequest listVersionsRequest = new ListVersionsRequest()
.withBucketName(bucketName)
.withKeyMarker(nextKeyMarker)
.withMaxResults(maxKeys)
.withVersionIdMarker(nextVersionMarker);
versionListing = ossClient.listVersions(listVersionsRequest);
for (OSSVersionSummary ossVersion : versionListing.getVersionSummaries()) {
System.out.println("key name: " + ossVersion.getKey());
System.out.println("versionid: " + ossVersion.getVersionId());
System.out.println("Is latest: " + ossVersion.isLatest());
System.out.println("Is delete marker: " + ossVersion.isDeleteMarker());
}
nextKeyMarker = versionListing.getNextKeyMarker();
nextVersionMarker = versionListing.getNextVersionIdMarker();
} while (versionListing.isTruncated());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}Tampilkan objek yang namanya mengandung karakter khusus
Jika nama objek mengandung karakter khusus, Anda harus melakukan URL-encode terhadap nama objek tersebut saat transmisi. OSS hanya mendukung URL encoding.
Tanda kutip tunggal (' ')
Tanda kutip ganda (" ")
Ampersand (&)
Tanda kurung sudut (< >)
Koma enumerasi (、)
Karakter Tionghoa
Kode berikut memberikan contoh cara menampilkan objek yang namanya mengandung karakter khusus.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.internal.OSSConstants;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Tetapkan titik akhir. Contoh ini menggunakan titik akhir publik wilayah China (Hangzhou). Tentukan titik akhir yang sebenarnya.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Peroleh kredensial akses dari variabel lingkungan. Sebelum menjalankan kode contoh, pastikan variabel lingkungan OSS_ACCESS_KEY_ID dan OSS_ACCESS_KEY_SECRET telah dikonfigurasi.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Tentukan nama bucket. Contoh: examplebucket.
String bucketName = "examplebucket";
// Tentukan wilayah tempat bucket berada. Misalnya, jika bucket berada di wilayah China (Hangzhou), atur wilayah menjadi cn-hangzhou.
String region = "cn-hangzhou";
// Buat instans OSSClient.
// Setelah instans OSSClient tidak digunakan lagi, panggil metode shutdown untuk melepas sumber daya.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Tentukan bahwa hasil dikodekan dengan URL untuk transmisi.
String nextKeyMarker = null;
String nextVersionMarker = null;
VersionListing versionListing = null;
do {
ListVersionsRequest listVersionsRequest = new ListVersionsRequest()
.withBucketName(bucketName)
.withKeyMarker(nextKeyMarker)
.withVersionIdMarker(nextVersionMarker)
.withEncodingType(OSSConstants.URL_ENCODING);
versionListing = ossClient.listVersions(listVersionsRequest);
// Lihat informasi versi objek.
for (OSSVersionSummary ossVersion : versionListing.getVersionSummaries()) {
System.out.println("key name: " + ossVersion.getKey());
System.out.println("versionid: " + ossVersion.getVersionId());
System.out.println("Is latest: " + ossVersion.isLatest());
System.out.println("Is delete marker: " + ossVersion.isDeleteMarker());
}
nextKeyMarker = versionListing.getNextKeyMarker();
nextVersionMarker = versionListing.getNextVersionIdMarker();
} while (versionListing.isTruncated());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}Fitur folder
OSS tidak memiliki konsep folder. Semua elemen disimpan sebagai objek. Membuat folder setara dengan membuat objek berukuran nol byte yang diakhiri dengan garis miring (/). Anda dapat mengunggah dan mengunduh objek ini. Konsol OSS menampilkan objek yang diakhiri dengan garis miring (/) sebagai folder.
Anda dapat menggunakan parameter delimiter dan prefix untuk mensimulasikan folder.
Jika Anda mengatur parameter prefix ke nama folder, objek yang namanya memiliki awalan tersebut akan ditampilkan. Artinya, semua objek dan subdirektori dalam folder tersebut dikembalikan.
Jika Anda mengatur parameter prefix dan mengatur parameter delimiter ke garis miring (/), hanya objek dan subdirektori dalam folder tersebut yang ditampilkan. Subdirektori dalam folder tersebut dikembalikan sebagai CommonPrefixes. Objek dan folder dalam subdirektori tidak ditampilkan.
Asumsikan sebuah bucket berisi objek oss.jpg, fun/test.jpg, fun/movie/001.avi, dan fun/movie/007.avi, serta pemisah folder adalah garis miring (/). Contoh berikut menunjukkan cara menampilkan daftar objek dengan mensimulasikan folder.
Tampilkan versi objek dalam direktori root
Kode berikut memberikan contoh cara menampilkan versi objek dalam direktori root.
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.internal.OSSConstants; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // Tetapkan titik akhir. Contoh ini menggunakan titik akhir publik wilayah China (Hangzhou). Tentukan titik akhir yang sebenarnya. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Peroleh kredensial akses dari variabel lingkungan. Sebelum menjalankan kode contoh, pastikan variabel lingkungan OSS_ACCESS_KEY_ID dan OSS_ACCESS_KEY_SECRET telah dikonfigurasi. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Tentukan nama bucket. Contoh: examplebucket. String bucketName = "examplebucket"; // Tentukan wilayah tempat bucket berada. Misalnya, jika bucket berada di wilayah China (Hangzhou), atur wilayah menjadi cn-hangzhou. String region = "cn-hangzhou"; // Buat instans OSSClient. // Setelah instans OSSClient tidak digunakan lagi, panggil metode shutdown untuk melepas sumber daya. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Atur parameter delimiter ke garis miring (/) untuk menampilkan versi objek dan nama folder dalam direktori root. String delimiter = "/"; String nextKeyMarker = null; String nextVersionMarker = null; VersionListing versionListing = null; do { ListVersionsRequest listVersionsRequest = new ListVersionsRequest() .withBucketName(bucketName) .withKeyMarker(nextKeyMarker) .withVersionIdMarker(nextVersionMarker) .withEncodingType(OSSConstants.URL_ENCODING) .withDelimiter(delimiter); versionListing = ossClient.listVersions(listVersionsRequest); // Lihat informasi versi objek. for (OSSVersionSummary ossVersion : versionListing.getVersionSummaries()) { System.out.println("key name: " + ossVersion.getKey()); System.out.println("versionid: " + ossVersion.getVersionId()); System.out.println("Is latest: " + ossVersion.isLatest()); System.out.println("Is delete marker: " + ossVersion.isDeleteMarker()); } // Lihat nama folder yang diakhiri dengan garis miring (/) dalam direktori root. for (String commonPrefix : versionListing.getCommonPrefixes()) { System.out.println("commonPrefix: " + commonPrefix); } nextKeyMarker = versionListing.getNextKeyMarker(); nextVersionMarker = versionListing.getNextVersionIdMarker(); } while (versionListing.isTruncated()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }Tanggapan:
key name: oss.jpg versionid: CAEQEhiBgMCw8Y7FqBciIGIzMDE3MTEzOWRiMDRmZmFhMmRlMjljZWI0MWU4**** Is latest: true Is delete marker: false commonPrefix: fun/Tampilkan objek dan subdirektori dalam direktori tertentu
Kode berikut memberikan contoh cara menampilkan objek dan subdirektori dalam direktori tertentu.
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.internal.OSSConstants; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // Tetapkan titik akhir. Contoh ini menggunakan titik akhir publik wilayah China (Hangzhou). Tentukan titik akhir yang sebenarnya. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Peroleh kredensial akses dari variabel lingkungan. Sebelum menjalankan kode contoh, pastikan variabel lingkungan OSS_ACCESS_KEY_ID dan OSS_ACCESS_KEY_SECRET telah dikonfigurasi. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Tentukan nama bucket. Contoh: examplebucket. String bucketName = "examplebucket"; // Tentukan wilayah tempat bucket berada. Misalnya, jika bucket berada di wilayah China (Hangzhou), atur wilayah menjadi cn-hangzhou. String region = "cn-hangzhou"; // Buat instans OSSClient. // Setelah instans OSSClient tidak digunakan lagi, panggil metode shutdown untuk melepas sumber daya. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Atur parameter prefix untuk memperoleh semua objek dan folder dalam direktori fun. Atur parameter delimiter ke garis miring (/) sebagai pemisah folder. String prefix = "fun/"; String delimiter = "/"; String nextKeyMarker = null; String nextVersionMarker = null; VersionListing versionListing = null; do { ListVersionsRequest listVersionsRequest = new ListVersionsRequest() .withBucketName(bucketName) .withKeyMarker(nextKeyMarker) .withVersionIdMarker(nextVersionMarker) .withEncodingType(OSSConstants.URL_ENCODING) .withDelimiter(delimiter) .withPrefix(prefix); versionListing = ossClient.listVersions(listVersionsRequest); // Lihat informasi versi objek. for (OSSVersionSummary ossVersion : versionListing.getVersionSummaries()) { System.out.println("key name: " + ossVersion.getKey()); System.out.println("versionid: " + ossVersion.getVersionId()); System.out.println("Is latest: " + ossVersion.isLatest()); System.out.println("Is delete marker: " + ossVersion.isDeleteMarker()); } // Lihat nama subdirektori dalam direktori fun. for (String commonPrefix : versionListing.getCommonPrefixes()) { System.out.println("commonPrefix: " + commonPrefix); } nextKeyMarker = versionListing.getNextKeyMarker(); nextVersionMarker = versionListing.getNextVersionIdMarker(); } while (versionListing.isTruncated()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }Tanggapan:
key name: fun/test.jpg versionid: CAEQEhiBgIC48Y7FqBciIDU4NjAzMjczMTY5NDRjYmVhNGY4NTM2YTdjY2Ji**** Is latest: true Is delete marker: false commonPrefix: fun/movie/