Search in sources :

Example 6 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method list.

@Override
public Tuple<String, Iterable<StorageObject>> list(final String bucket, Map<Option, ?> options) {
    try {
        Objects objects = storage.objects().list(bucket).setProjection(DEFAULT_PROJECTION).setVersions(Option.VERSIONS.getBoolean(options)).setDelimiter(Option.DELIMITER.getString(options)).setPrefix(Option.PREFIX.getString(options)).setMaxResults(Option.MAX_RESULTS.getLong(options)).setPageToken(Option.PAGE_TOKEN.getString(options)).setFields(Option.FIELDS.getString(options)).execute();
        Iterable<StorageObject> storageObjects = Iterables.concat(firstNonNull(objects.getItems(), ImmutableList.<StorageObject>of()), objects.getPrefixes() != null ? Lists.transform(objects.getPrefixes(), objectFromPrefix(bucket)) : ImmutableList.<StorageObject>of());
        return Tuple.of(objects.getNextPageToken(), storageObjects);
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) Objects(com.google.api.services.storage.model.Objects) IOException(java.io.IOException)

Example 7 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method get.

@Override
public Blob get(BlobId blob, BlobGetOption... options) {
    final StorageObject storedObject = blob.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
    try {
        StorageObject storageObject = runWithRetries(new Callable<StorageObject>() {

            @Override
            public StorageObject call() {
                return storageRpc.get(storedObject, optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock());
        return storageObject == null ? null : Blob.fromPb(this, storageObject);
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 8 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method create.

private Blob create(BlobInfo info, final InputStream content, BlobTargetOption... options) {
    final StorageObject blobPb = info.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(info, options);
    try {
        return Blob.fromPb(this, runWithRetries(new Callable<StorageObject>() {

            @Override
            public StorageObject call() {
                return storageRpc.create(blobPb, firstNonNull(content, new ByteArrayInputStream(EMPTY_BYTE_ARRAY)), optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()));
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) ByteArrayInputStream(java.io.ByteArrayInputStream) Callable(java.util.concurrent.Callable) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 9 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method delete.

@Override
public boolean delete(BlobId blob, BlobSourceOption... options) {
    final StorageObject storageObject = blob.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
    try {
        return runWithRetries(new Callable<Boolean>() {

            @Override
            public Boolean call() {
                return storageRpc.delete(storageObject, optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock());
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 10 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method readAllBytes.

@Override
public byte[] readAllBytes(BlobId blob, BlobSourceOption... options) {
    final StorageObject storageObject = blob.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
    try {
        return runWithRetries(new Callable<byte[]>() {

            @Override
            public byte[] call() {
                return storageRpc.load(storageObject, optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock());
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Aggregations

StorageObject (com.google.api.services.storage.model.StorageObject)35 Test (org.junit.Test)13 Objects (com.google.api.services.storage.model.Objects)10 RetryHelperException (com.google.cloud.RetryHelper.RetryHelperException)7 ArrayList (java.util.ArrayList)7 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)6 IOException (java.io.IOException)6 GcsPath (org.apache.beam.sdk.util.gcsfs.GcsPath)6 RpcBatch (com.google.cloud.storage.spi.v1.RpcBatch)5 Storage (com.google.api.services.storage.Storage)4 BlobSourceOption (com.google.cloud.storage.Storage.BlobSourceOption)4 BlobTargetOption (com.google.cloud.storage.Storage.BlobTargetOption)4 BlobWriteOption (com.google.cloud.storage.Storage.BlobWriteOption)4 BucketSourceOption (com.google.cloud.storage.Storage.BucketSourceOption)4 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)4 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 Map (java.util.Map)3 Callable (java.util.concurrent.Callable)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2