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

com.metreeca.gcp.services.GCPStore Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2013-2022 Metreeca srl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.metreeca.gcp.services;

import com.metreeca.rest.services.Store;

import com.google.cloud.storage.*;

import java.io.*;
import java.nio.channels.Channels;
import java.nio.file.NoSuchFileException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Google Cloud blob store.
 *
 * 

Retrieves blobs managed by the Google Cloud Storage service.

* *

For both {@linkplain #read(String) read} and {@linkplain #write(String) write} operations, blob identifiers not * matching a full GCS URI (i.e. {@code gs://{bucket}/{object}}) or link URL (i.e. {@code * https://storage.cloud.google.com/{bucket}/{object}}) are interpreted as object names in the default project bucket * (i.e. {@code {project}.appspot.com}).

* * @see Google Cloud Plaform - Storage */ public final class GCPStore implements Store { private static final Pattern IdPattern=Pattern .compile("(?:https://storage\\.cloud\\.google\\.com/|gs://)(?[^/]+)/(?[^/]+)"); @FunctionalInterface private static interface Task { public R exec(final String bucket, final String object) throws IOException; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private final Storage storage; public GCPStore() { this(StorageOptions.getDefaultInstance()); } public GCPStore(final StorageOptions options) { if ( options == null ) { throw new NullPointerException("null options"); } this.storage=options.getService(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @Override public boolean has(final String id) throws IOException { if ( id == null ) { throw new NullPointerException("null id"); } return exec(id, this::has); } @Override public InputStream read(final String id) throws IOException { if ( id == null ) { throw new NullPointerException("null id"); } return exec(id, this::read); } @Override public OutputStream write(final String id) throws IOException { if ( id == null ) { throw new NullPointerException("null id"); } return exec(id, this::write); } private V exec(final String id, final Task task) throws IOException { final Matcher matcher=IdPattern.matcher(id); return matcher.matches() ? task.exec(matcher.group("bucket"), matcher.group("object")) : task.exec(storage.getOptions().getProjectId()+".appspot.com", id); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private boolean has(final String bucket, final String object) throws IOException { if ( bucket == null ) { throw new NullPointerException("null bucket"); } if ( object == null ) { throw new NullPointerException("null object"); } try { return storage.get(BlobId.of(bucket, object)) != null; } catch ( final StorageException e ) { throw new IOException(e); } } private InputStream read(final String bucket, final String object) throws IOException { if ( bucket == null ) { throw new NullPointerException("null bucket"); } if ( object == null ) { throw new NullPointerException("null object"); } try { final Blob blob=storage.get(BlobId.of(bucket, object)); if ( blob == null ) { throw new NoSuchFileException(String.format("gs://%s/%s", bucket, object)); } return Channels.newInputStream(blob.reader()); } catch ( final StorageException e ) { throw new IOException(e); } } private OutputStream write(final String bucket, final String object) throws IOException { if ( bucket == null ) { throw new NullPointerException("null bucket"); } if ( object == null ) { throw new NullPointerException("null object"); } try { return Channels.newOutputStream(storage.create(BlobInfo.newBuilder(bucket, object).build()).writer()); } catch ( final StorageException e ) { throw new IOException(e); } } }