Java SDK for Swift API
openstack4jライブラリを利用して、NAVERクラウドプラットフォームのArchive Storageを使用する方法を説明します。
このテキストは、openstack4j 3.1.0バージョンに基づいて作成されました。
- テキスト : http://www.openstack4j.com
インストール
ライブラリを直接ダウンロードするか、Apache Mavenを用いて使用することができます。
ライブラリをダウンロード
- 3.1.0 バージョン : https://search.maven.org/remotecontent?filepath=org/pacesys/openstack4j/3.1.0/openstack4j-3.1.0.jar
Apache Mavenで利用する
pom.xml
<dependency>
<groupId>org.pacesys</groupId>
<artifactId>openstack4j</artifactId>
<version>3.1.0</version>
</dependency>
例
例で使用されるuser, passwordはポータルマイページ > アカウント管理 > 認証キー管理で作成したAPI認証キーを使用します。(Access Key IDはuser, Secret Keyはpassword)
projectIdとdomainId情報は、Archive Storageコンソールから利用を申込んで、[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)
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)
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);