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);
}
}
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;
}
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;
}
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);
}
}
Aggregations