All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yamcs.client.storage.StorageClient Maven / Gradle / Ivy

The newest version!
package org.yamcs.client.storage;

import java.util.concurrent.CompletableFuture;

import org.yamcs.api.HttpBody;
import org.yamcs.api.MethodHandler;
import org.yamcs.client.base.ResponseObserver;
import org.yamcs.protobuf.BucketsApiClient;
import org.yamcs.protobuf.GetObjectRequest;
import org.yamcs.protobuf.UploadObjectRequest;

import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;

public class StorageClient {

    private BucketsApiClient bucketService;

    public StorageClient(MethodHandler handler) {
        bucketService = new BucketsApiClient(handler);
    }

    public CompletableFuture uploadObject(ObjectId target, byte[] bytes) {
        UploadObjectRequest request = UploadObjectRequest.newBuilder()
                .setInstance(target.getInstance())
                .setBucketName(target.getBucket())
                .setObjectName(target.getObjectName())
                .setData(HttpBody.newBuilder().setData(ByteString.copyFrom(bytes)))
                .build();
        CompletableFuture f = new CompletableFuture<>();
        bucketService.uploadObject(null, request, new ResponseObserver<>(f));
        return f.thenApply(response -> null);
    }

    public CompletableFuture downloadObject(ObjectId source) {
        GetObjectRequest request = GetObjectRequest.newBuilder()
                .setInstance(source.getInstance())
                .setBucketName(source.getBucket())
                .setObjectName(source.getObjectName())
                .build();
        CompletableFuture f = new CompletableFuture<>();
        bucketService.getObject(null, request, new ResponseObserver<>(f));
        return f.thenApply(response -> response.getData().toByteArray());
    }

    public Bucket getBucket(String bucket) {
        return new Bucket(bucketService, bucket);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy