Search in sources :

Example 71 with BlobInfo

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

the class ITStorageTest method testUpdateBlobsFail.

@Test
public void testUpdateBlobsFail() {
    String sourceBlobName1 = "test-update-blobs-fail-1";
    String sourceBlobName2 = "test-update-blobs-fail-2";
    BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
    BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
    BlobInfo remoteBlob1 = storage.create(sourceBlob1);
    assertNotNull(remoteBlob1);
    List<Blob> updatedBlobs = storage.update(remoteBlob1.toBuilder().setContentType(CONTENT_TYPE).build(), sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build());
    assertEquals(sourceBlob1.getBucket(), updatedBlobs.get(0).getBucket());
    assertEquals(sourceBlob1.getName(), updatedBlobs.get(0).getName());
    assertEquals(CONTENT_TYPE, updatedBlobs.get(0).getContentType());
    assertNull(updatedBlobs.get(1));
    assertTrue(updatedBlobs.get(0).delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 72 with BlobInfo

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

the class ITStorageTest method testReadAndWriteChannelsWithDifferentFileSize.

@Test
public void testReadAndWriteChannelsWithDifferentFileSize() throws IOException {
    String blobNamePrefix = "test-read-and-write-channels-blob-";
    int[] blobSizes = { 0, 700, 1024 * 256, 2 * 1024 * 1024, 4 * 1024 * 1024, 4 * 1024 * 1024 + 1 };
    Random rnd = new Random();
    for (int blobSize : blobSizes) {
        String blobName = blobNamePrefix + blobSize;
        BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
        byte[] bytes = new byte[blobSize];
        rnd.nextBytes(bytes);
        try (WriteChannel writer = storage.writer(blob)) {
            writer.write(ByteBuffer.wrap(bytes));
        }
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try (ReadChannel reader = storage.reader(blob.getBlobId())) {
            ByteBuffer buffer = ByteBuffer.allocate(64 * 1024);
            while (reader.read(buffer) > 0) {
                buffer.flip();
                output.write(buffer.array(), 0, buffer.limit());
                buffer.clear();
            }
        }
        assertArrayEquals(bytes, output.toByteArray());
        assertTrue(storage.delete(BUCKET, blobName));
    }
}
Also used : Random(java.util.Random) WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 73 with BlobInfo

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

the class ITStorageTest method testComposeBlob.

@Test
public void testComposeBlob() {
    String sourceBlobName1 = "test-compose-blob-source-1";
    String sourceBlobName2 = "test-compose-blob-source-2";
    BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
    BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
    Blob remoteSourceBlob1 = storage.create(sourceBlob1, BLOB_BYTE_CONTENT);
    Blob remoteSourceBlob2 = storage.create(sourceBlob2, BLOB_BYTE_CONTENT);
    assertNotNull(remoteSourceBlob1);
    assertNotNull(remoteSourceBlob2);
    String targetBlobName = "test-compose-blob-target";
    BlobInfo targetBlob = BlobInfo.newBuilder(BUCKET, targetBlobName).build();
    Storage.ComposeRequest req = Storage.ComposeRequest.of(ImmutableList.of(sourceBlobName1, sourceBlobName2), targetBlob);
    Blob remoteTargetBlob = storage.compose(req);
    assertNotNull(remoteTargetBlob);
    assertEquals(targetBlob.getName(), remoteTargetBlob.getName());
    assertEquals(targetBlob.getBucket(), remoteTargetBlob.getBucket());
    assertNull(remoteTargetBlob.getContentType());
    byte[] readBytes = storage.readAllBytes(BUCKET, targetBlobName);
    byte[] composedBytes = Arrays.copyOf(BLOB_BYTE_CONTENT, BLOB_BYTE_CONTENT.length * 2);
    System.arraycopy(BLOB_BYTE_CONTENT, 0, composedBytes, BLOB_BYTE_CONTENT.length, BLOB_BYTE_CONTENT.length);
    assertArrayEquals(composedBytes, readBytes);
    assertTrue(remoteSourceBlob1.delete());
    assertTrue(remoteSourceBlob2.delete());
    assertTrue(remoteTargetBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 74 with BlobInfo

use of com.google.cloud.storage.BlobInfo 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 75 with BlobInfo

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

the class CloudStorageFileSystemProvider method readAttributes.

@Override
public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException {
    checkNotNull(type);
    CloudStorageUtil.checkNotNullArray(options);
    if (type != CloudStorageFileAttributes.class && type != BasicFileAttributes.class) {
        throw new UnsupportedOperationException(type.getSimpleName());
    }
    initStorage();
    CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
    if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
        @SuppressWarnings("unchecked") A result = (A) new CloudStoragePseudoDirectoryAttributes(cloudPath);
        return result;
    }
    BlobInfo blobInfo = storage.get(cloudPath.getBlobId());
    // null size indicate a file that we haven't closed yet, so GCS treats it as not there yet.
    if (null == blobInfo || blobInfo.getSize() == null) {
        throw new NoSuchFileException(cloudPath.getBlobId().getBucket() + "/" + cloudPath.getBlobId().getName());
    }
    CloudStorageObjectAttributes ret;
    ret = new CloudStorageObjectAttributes(blobInfo);
    @SuppressWarnings("unchecked") A result = (A) ret;
    return result;
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) BlobInfo(com.google.cloud.storage.BlobInfo) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

BlobInfo (com.google.cloud.storage.BlobInfo)94 Test (org.junit.Test)61 Blob (com.google.cloud.storage.Blob)56 BlobId (com.google.cloud.storage.BlobId)31 Storage (com.google.cloud.storage.Storage)21 StorageException (com.google.cloud.storage.StorageException)17 WriteChannel (com.google.cloud.WriteChannel)13 ReadChannel (com.google.cloud.ReadChannel)7 CopyWriter (com.google.cloud.storage.CopyWriter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteBuffer (java.nio.ByteBuffer)7 InputStream (java.io.InputStream)4 URL (java.net.URL)4 StorageBatch (com.google.cloud.storage.StorageBatch)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 TestRunner (org.apache.nifi.util.TestRunner)3 Acl (com.google.cloud.storage.Acl)2 Bucket (com.google.cloud.storage.Bucket)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2