use of java.nio.file.CopyOption 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 java.nio.file.CopyOption in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProvider method move.
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
initStorage();
for (CopyOption option : options) {
if (option == StandardCopyOption.ATOMIC_MOVE) {
throw new AtomicMoveNotSupportedException(source.toString(), target.toString(), "Google Cloud Storage does not support atomic move operations.");
}
}
copy(source, target, options);
delete(source);
}
Aggregations