このトピックでは、OSS Java SDK を使用してバケットの作成、オブジェクトのアップロード、オブジェクトのダウンロードなどのルーチン操作を実行する方法について説明します。
サンプルプロジェクト
OSS Java SDK は、 Maven ベースと Ant ベースのサンプルプロジェクトを提供しています。 サンプルプロジェクトをローカルデバイスでコンパイルして実行したり、サンプルプロジェクトに基づいて独自のアプリケーションを開発することが可能です。 プロジェクトのコンパイルおよび実行方法については、プロジェクトディレクトリの「README.md」をご参照ください。
- Maven サンプルプロジェクト: aliyun-oss-java-sdk-demo-mvn.zip
- Ant サンプルプロジェクト:aliyun-oss-java-sdk-demo-ant.zip
バケットの作成
バケットは OSS のグローバルネームスペースです。 ファイルを格納するデータコンテナーのようなものです。 次のコードを実行してバケットを作成します。
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// Create a bucket.
ossClient.createBucket(bucketName);
// Close your OSSClient instance.
ossClient.shutdown();
バケットの命名ルールの詳細については、「基本概念」の命名ルールをご参照ください。 バケットの作成の詳細については、「 バケットの管理」をご参照ください。
エンドポイントの詳細については、「リージョンおよびエンドポイント」をご参照ください。
オブジェクトのアップロード
次のコードを実行してファイルを OSS にオブジェクトをアップロードします。
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// Upload content to the specified bucket and save it as the specified object name.
String content = "Hello OSS";
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
// Close your OSSClient instance.
ossClient.shutdown();
詳しくは、「オブジェクトのアップロード」をご参照ください。
オブジェクトのダウンロード
次のコードを実行して、オブジェクトのコンテンツを取り込みます。
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// Call ossClient.getObject to return an OSSObject instance. The OSSObject instance contains the object content and Object Meta.
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
// Call ossObject.getObjectContent to obtain object InputStream. Read the InputStream to obtain object content.
InputStream content = ossObject.getObjectContent();
if (content ! = null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
while (true) {
String line = reader.readLine();
if (line == null) break;
System.out.println("\n" + line);
}
// If you do not close the reader after the data is read, connection leaks may occur. Consequently, no available connections are left and an exception occurs.
content.close();
}
// Close your OSSClient instance.
ossClient.shutdown();
詳しくは、「オブジェクトのダウンロード」をご参照ください。
オブジェクトの一覧表示
次のコードを実行して、バケット内のオブジェクトを一覧で表示します。 デフォルトでは最大 100 個のオブジェクトをリストできます。
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// Use ossClient.listObjects to return an ObjectListing instance. The ObjectListing instance contains the response from listObject.
ObjectListing objectListing = ossClient.listObjects(bucketName);
// Use objectListing.getObjectSummaries to obtain the descriptions of all objects.
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " +
"(size = " + objectSummary.getSize() + ")");
}
// Close your OSSClient instance.
ossClient.shutdown();
詳しくは、「オブジェクトの管理」の「オブジェクトの一覧表示」をご参照ください。
オブジェクトの削除
オブジェクトを削除するための完全なコードは、「GitHub」をご参照ください。
指定したオブジェクトを削除するには、次のコードを実行します。
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// Delete objects.
ossClient.deleteObject(bucketName, objectName);
// Close your OSSClient instance.
ossClient.shutdown();