Search in sources :

Example 26 with StorageException

use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.

the class CloudStorageFileSystemProvider method copy.

@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
    initStorage();
    boolean wantCopyAttributes = false;
    boolean wantReplaceExisting = false;
    boolean setContentType = false;
    boolean setCacheControl = false;
    boolean setContentEncoding = false;
    boolean setContentDisposition = false;
    CloudStoragePath toPath = CloudStorageUtil.checkPath(target);
    BlobInfo.Builder tgtInfoBuilder = BlobInfo.newBuilder(toPath.getBlobId()).setContentType("");
    int blockSize = -1;
    for (CopyOption option : options) {
        if (option instanceof StandardCopyOption) {
            switch((StandardCopyOption) option) {
                case COPY_ATTRIBUTES:
                    wantCopyAttributes = true;
                    break;
                case REPLACE_EXISTING:
                    wantReplaceExisting = true;
                    break;
                case ATOMIC_MOVE:
                default:
                    throw new UnsupportedOperationException(option.toString());
            }
        } else if (option instanceof CloudStorageOption) {
            if (option instanceof OptionBlockSize) {
                blockSize = ((OptionBlockSize) option).size();
            } else if (option instanceof OptionMimeType) {
                tgtInfoBuilder.setContentType(((OptionMimeType) option).mimeType());
                setContentType = true;
            } else if (option instanceof OptionCacheControl) {
                tgtInfoBuilder.setCacheControl(((OptionCacheControl) option).cacheControl());
                setCacheControl = true;
            } else if (option instanceof OptionContentEncoding) {
                tgtInfoBuilder.setContentEncoding(((OptionContentEncoding) option).contentEncoding());
                setContentEncoding = true;
            } else if (option instanceof OptionContentDisposition) {
                tgtInfoBuilder.setContentDisposition(((OptionContentDisposition) option).contentDisposition());
                setContentDisposition = true;
            } else {
                throw new UnsupportedOperationException(option.toString());
            }
        } else {
            throw new UnsupportedOperationException(option.toString());
        }
    }
    CloudStoragePath fromPath = CloudStorageUtil.checkPath(source);
    blockSize = blockSize != -1 ? blockSize : Ints.max(fromPath.getFileSystem().config().blockSize(), toPath.getFileSystem().config().blockSize());
    if (fromPath.seemsLikeADirectory() && toPath.seemsLikeADirectory()) {
        if (fromPath.getFileSystem().config().usePseudoDirectories() && toPath.getFileSystem().config().usePseudoDirectories()) {
            // NOOP: This would normally create an empty directory.
            return;
        } else {
            checkArgument(!fromPath.getFileSystem().config().usePseudoDirectories() && !toPath.getFileSystem().config().usePseudoDirectories(), "File systems associated with paths don't agree on pseudo-directories.");
        }
    }
    if (fromPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
        throw new CloudStoragePseudoDirectoryException(fromPath);
    }
    if (toPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
        throw new CloudStoragePseudoDirectoryException(toPath);
    }
    try {
        if (wantCopyAttributes) {
            BlobInfo blobInfo = storage.get(fromPath.getBlobId());
            if (null == blobInfo) {
                throw new NoSuchFileException(fromPath.toString());
            }
            if (!setCacheControl) {
                tgtInfoBuilder.setCacheControl(blobInfo.getCacheControl());
            }
            if (!setContentType) {
                tgtInfoBuilder.setContentType(blobInfo.getContentType());
            }
            if (!setContentEncoding) {
                tgtInfoBuilder.setContentEncoding(blobInfo.getContentEncoding());
            }
            if (!setContentDisposition) {
                tgtInfoBuilder.setContentDisposition(blobInfo.getContentDisposition());
            }
            tgtInfoBuilder.setAcl(blobInfo.getAcl());
            tgtInfoBuilder.setMetadata(blobInfo.getMetadata());
        }
        BlobInfo tgtInfo = tgtInfoBuilder.build();
        Storage.CopyRequest.Builder copyReqBuilder = Storage.CopyRequest.newBuilder().setSource(fromPath.getBlobId());
        if (wantReplaceExisting) {
            copyReqBuilder = copyReqBuilder.setTarget(tgtInfo);
        } else {
            copyReqBuilder = copyReqBuilder.setTarget(tgtInfo, Storage.BlobTargetOption.doesNotExist());
        }
        CopyWriter copyWriter = storage.copy(copyReqBuilder.build());
        copyWriter.getResult();
    } catch (StorageException oops) {
        throw asIoException(oops);
    }
}
Also used : CopyOption(java.nio.file.CopyOption) StandardCopyOption(java.nio.file.StandardCopyOption) NoSuchFileException(java.nio.file.NoSuchFileException) BlobInfo(com.google.cloud.storage.BlobInfo) CopyWriter(com.google.cloud.storage.CopyWriter) StandardCopyOption(java.nio.file.StandardCopyOption) StorageException(com.google.cloud.storage.StorageException)

Example 27 with StorageException

use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.

the class FakeStorageRpc method create.

@Override
public StorageObject create(StorageObject object, InputStream content, Map<Option, ?> options) throws StorageException {
    potentiallyThrow(options);
    String key = fullname(object);
    metadata.put(key, object);
    try {
        contents.put(key, com.google.common.io.ByteStreams.toByteArray(content));
    } catch (IOException e) {
        throw new StorageException(e);
    }
    // TODO: crc, etc
    return object;
}
Also used : IOException(java.io.IOException) StorageException(com.google.cloud.storage.StorageException)

Example 28 with StorageException

use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method batch.

/**
   * Example of using a batch request to delete, update and get a blob.
   */
// [TARGET batch()]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public Blob batch(String bucketName, String blobName1, String blobName2) {
    // [START batch]
    StorageBatch batch = storage.batch();
    BlobId firstBlob = BlobId.of(bucketName, blobName1);
    BlobId secondBlob = BlobId.of(bucketName, blobName2);
    batch.delete(firstBlob).notify(new BatchResult.Callback<Boolean, StorageException>() {

        public void success(Boolean result) {
        // deleted successfully
        }

        public void error(StorageException exception) {
        // delete failed
        }
    });
    batch.update(BlobInfo.newBuilder(secondBlob).setContentType("text/plain").build());
    StorageBatchResult<Blob> result = batch.get(secondBlob);
    batch.submit();
    // returns get result or throws StorageException
    Blob blob = result.get();
    // [END batch]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) StorageBatch(com.google.cloud.storage.StorageBatch) StorageBatchResult(com.google.cloud.storage.StorageBatchResult) BatchResult(com.google.cloud.BatchResult) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException)

Example 29 with StorageException

use of com.google.cloud.storage.StorageException in project gatk by broadinstitute.

the class Main method mainEntry.

/**
     * The entry point to the toolkit from commandline: it uses {@link #instanceMain(String[])} to run the command line
     * program and handle the returned object with {@link #handleResult(Object)}, and exit with 0.
     * If any error occurs, it handles the exception (if non-user exception, through {@link #handleNonUserException(Exception)})
     * and exit with the concrete error exit value.
     *
     * Note: this is the only method that is allowed to call System.exit (because gatk tools may be run from test harness etc)
     */
protected final void mainEntry(final String[] args) {
    final CommandLineProgram program = extractCommandLineProgram(args, getPackageList(), getClassList(), getCommandLineName());
    try {
        final Object result = runCommandLineProgram(program, args);
        handleResult(result);
        System.exit(0);
    } catch (final CommandLineException e) {
        System.err.println(program.getUsage());
        handleUserException(e);
        System.exit(COMMANDLINE_EXCEPTION_EXIT_VALUE);
    } catch (final UserException e) {
        handleUserException(e);
        System.exit(USER_EXCEPTION_EXIT_VALUE);
    } catch (final StorageException e) {
        handleStorageException(e);
        System.exit(ANY_OTHER_EXCEPTION_EXIT_VALUE);
    } catch (final Exception e) {
        handleNonUserException(e);
        System.exit(ANY_OTHER_EXCEPTION_EXIT_VALUE);
    }
}
Also used : CommandLineProgram(org.broadinstitute.hellbender.cmdline.CommandLineProgram) UserException(org.broadinstitute.hellbender.exceptions.UserException) StorageException(com.google.cloud.storage.StorageException) CommandLineException(org.broadinstitute.barclay.argparser.CommandLineException) UserException(org.broadinstitute.hellbender.exceptions.UserException) StorageException(com.google.cloud.storage.StorageException) CommandLineException(org.broadinstitute.barclay.argparser.CommandLineException)

Aggregations

StorageException (com.google.cloud.storage.StorageException)29 Test (org.junit.Test)20 BlobInfo (com.google.cloud.storage.BlobInfo)16 Blob (com.google.cloud.storage.Blob)15 BlobId (com.google.cloud.storage.BlobId)8 IOException (java.io.IOException)5 Acl (com.google.cloud.storage.Acl)4 ByteBuffer (java.nio.ByteBuffer)4 Storage (com.google.cloud.storage.Storage)3 StorageBatch (com.google.cloud.storage.StorageBatch)3 ReadChannel (com.google.cloud.ReadChannel)2 WriteChannel (com.google.cloud.WriteChannel)2 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpResponse (com.google.api.client.http.HttpResponse)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 Get (com.google.api.services.storage.Storage.Objects.Get)1 BatchResult (com.google.cloud.BatchResult)1 User (com.google.cloud.storage.Acl.User)1