Search in sources :

Example 6 with CopyOption

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);
    }
}
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 7 with CopyOption

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);
}
Also used : CopyOption(java.nio.file.CopyOption) StandardCopyOption(java.nio.file.StandardCopyOption) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Aggregations

CopyOption (java.nio.file.CopyOption)7 File (java.io.File)4 IOException (java.io.IOException)3 StandardCopyOption (java.nio.file.StandardCopyOption)3 NoSuchFileException (java.nio.file.NoSuchFileException)2 Test (org.junit.Test)2 DelegatingFileSystemAbstraction (org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction)2 DelegateFileSystemAbstraction (org.neo4j.io.fs.DelegateFileSystemAbstraction)2 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)2 BlobInfo (com.google.cloud.storage.BlobInfo)1 CopyWriter (com.google.cloud.storage.CopyWriter)1 StorageException (com.google.cloud.storage.StorageException)1 UploaderException (com.twitter.heron.spi.uploader.UploaderException)1 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 Path (java.nio.file.Path)1 FileHandle (org.neo4j.io.pagecache.FileHandle)1 PagedFile (org.neo4j.io.pagecache.PagedFile)1 User (org.neo4j.kernel.impl.security.User)1