- 印刷する
- PDF
Java SDK
- 印刷する
- PDF
Classic/VPC環境で利用できます。
openstack4jライブラリを利用し、NAVERクラウドプラットフォーム Archive Storageを使用する例です。このガイドでは、openstack4j 3.1.0バージョンを基準に説明します。関連文書は openstack4jをご参照ください。
インストール
ライブラリを直接ダウンロードしたり、Apache Mavenでインストールできます。
ライブラリのダウンロード
openstack4j 3.1.0 バージョンダウンロードApache Mavenでインストール
<dependency> <groupId>org.pacesys</groupId> <artifactId>openstack4j</artifactId> <version>3.1.0</version> </dependency>
例
例で使用する user
、 password
に適用する値は NAVERクラウドプラットフォームポータルで作成した API認証キー情報を使用します。API認証キー情報で Access Key IDを user
の値に、Secret Keyを password
の値に入力します。domainid
、projectid
は Archive Storage画面の [API利用情報の確認] ボタンをクリックし、表示されるポップアップウィンドウの情報を使用します。Domain IDを domainid
の値に、Project IDの値を projectid
の値に入力します。
- API認証キー情報を確認する方法は API認証キー作成および確認をご参照ください。
- project_idと domain_id情報を確認する方法は API利用情報の確認をご確認ください。
クライアント作成
認証情報にて作成されたトークンオブジェクトで OSクライアントを作成します。クライアントでトークンが無効または期限切れになると、自動的にトークンを更新します。マルチスレッド環境で、トークンオブジェクトを共有するため、不要な認証手順を削減できます。
このガイドで説明するすべての例は、認証情報にて作成したトークンオブジェクトを利用します。
final String endpoint = "https://kr.archive.ncloudstorage.com:5000/v3";
final String user = "ACCESS_KEY_ID";
final String password = "SECRET_KEY";
final String domainId = "DOMAIN_ID";
final String projectId = "PROJECT_ID";
Token token = OSFactory.builderV3()
.endpoint(endpoint)
.credentials(user, password, Identifier.byId(domainId))
.scopeToProject(Identifier.byId(projectId), Identifier.byId(domainId))
.authenticate()
.token;
OSClientV3 client = OSFactory.clientFromToken(token);
// Spawn off a thread giving it the access
myThreadExecutor.submit(new MyRunnableOrCallable(token));
// Example of the Runnable or other object invoked in a new thread
public class MyRunnable implements Runnable {
private OSClientV3 client;
public MyRunnable(Access access) {
client = OSFactory.clientFromToken(token);
}
public void run() {
// can now use the client :)
}
}
コンテナ(バケット)リストの照会
コンテナ(バケット)リストを照会する例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageContainerService containerService = client.objectStorage().containers();
List<? extends SwiftContainer> containers = containerService.list(ContainerListOptions.create().limit(100));
for (SwiftContainer container : containers) {
System.out.println(container);
}
コンテナ(バケット)の作成
コンテナ(バケット)を作成する例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageContainerService containerService = client.objectStorage().containers();
String containerName = "sample-container";
// with metadata
// X-Container-Meta-Test-Meta-Key: test-meta-value
Map<String, String> metadata = new HashMap<>();
metadata.put("test-meta-key", "test-meta-value");
ActionResponse response containerService.create(containerName, CreateUpdateContainerOptions.create().metadata(metadata));
System.out.println(response);
コンテナ(バケット)の削除
コンテナ(バケット)を削除する例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageContainerService containerService = client.objectStorage().containers();
String containerName = "sample-container";
ActionResponse response = containerService.delete(containerName);
System.out.println(response);
オブジェクトアップロード
オブジェクトをアップロードする例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();
String containerName = "sample-container";
String objectName = "sample-object.txt";
String contentType = "text/plain";
File file = new File("/tmp/sample-object.txt");
// with metadata
// X-Object-Meta-Test-Meta-Key : test-meta-value
Map<String, String> metadata = new HashMap<>();
metadata.put("test-meta-key", "test-meta-value");
String etag = objectService.put(containerName, objectName, Payloads.create(file),
ObjectPutOptions.create().contentType(contentType).metadata(metadata));
System.out.println(etag);
ディレクトリオブジェクトの作成
ディレクトリオブジェクトを作成する例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageContainerService containerService = client.objectStorage().containers();
String containerName = "sample-container";
String directoryName = "sample-directory";
String etag = containerService.createPath(containerName, directoryName);
System.out.println(etag);
オブジェクトリストの照会
オブジェクトリストを照会する例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();
String containerName = "sample-container";
String directoryName = "sample-directory";
// simple
List<? extends SwiftObject> objects = objectService.list(containerName);
for (SwiftObject object : objects) {
System.out.println(object);
}
// list for directory
List<? extends SwiftObject> objectsForDirectory = objectService.list(containerName,
ObjectListOptions.create().path(directoryName));
for (SwiftObject object : objectsForDirectory) {
System.out.println(object);
}
オブジェクトのダウンロード
オブジェクトをダウンロードする例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();
String containerName = "sample-container";
String objectName = "sample-object.txt";
String downloadPath = "/tmp/sample-object.txt";
DLPayload payload = objectService.download(containerName, objectName);
File file = new File(downloadPath);
payload.writeToFile(file);
オブジェクトの削除
オブジェクトを削除する例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();
String containerName = "sample-container";
String objectName = "sample-object.txt";
ActionResponse response = objectService.delete(containerName, objectName);
System.out.println(response);
大容量ファイルのアップロード(SLO)
SLO方式を使用し大容量ファイルをアップロードする例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(getToken());
ObjectStorageObjectService objectService = client.objectStorage().objects();
String containerName = "sample-container";
String objectName = "sample-big-file.mp4";
String contentType = "video/mp4";
String segmentContainerName = "segment-container";
String segmentPrefix = "segments-for-sample-big-file.mp4/segment-";
List<Map<String, Object>> segmentInfoList = new ArrayList<>();
File file = new File("/tmp/sample.mp4");
FileInputStream fs = new FileInputStream(file);
int bufferSize = 1024 * 1024 * 10; // 10MB
byte[] buffer = new byte[bufferSize];
int segmentNo = 1;
int bytesRead;
while ((bytesRead = fs.read(buffer)) != -1) {
Map<String, Object> segmentInfo = new HashMap<>();
InputStreamPayload payload = new InputStreamPayload(new ByteArrayInputStream(buffer, 0, bytesRead));
String etag = objectService.put(segmentContainerName, segmentPrefix + segmentNo, payload);
segmentInfo.put("path", "/" + segmentContainerName + "/" + segmentPrefix + segmentNo);
segmentInfo.put("etag", etag);
segmentInfo.put("size_bytes", bytesRead);
segmentInfoList.add(segmentInfo);
segmentNo++;
}
fs.close();
String manifest = new ObjectMapper().writeValueAsString(segmentInfoList);
String etag = objectService.put(containerName, objectName,
Payloads.create(new ByteArrayInputStream(manifest.getBytes())),
ObjectPutOptions.create().queryParam("multipart-manifest", "put").contentType(contentType));
System.out.println(etag);
大容量ファイル Segmentsの照会(SLO)
大容量ファイル Segmentsを照会する例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();
String containerName = "sample-container";
String objectName = "sample-big-file.mp4";
ObjectListOptions options = ObjectListOptions.create();
options.getOptions().put("multipart-manifest", "get");
List<? extends SwiftObject> objects = objectService.list(containerName + "/" + objectName, options);
for (SwiftObject object : objects) {
System.out.println(object);
}
大容量ファイルの削除(SLO)
大容量ファイルを削除する例は以下のとおりです。
OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();
String containerName = "sample-container";
String objectName = "sample-big-file.mp4";
ActionResponse response = objectService.delete(ObjectLocation.create(containerName, objectName),
ObjectDeleteOptions.create().queryParam("multipart-manifest", "delete"));
System.out.println(response);