Search in sources :

Example 1 with StorageException

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

the class HttpStorageRpc method read.

@Override
public Tuple<String, byte[]> read(StorageObject from, Map<Option, ?> options, long position, int bytes) {
    try {
        Get req = storage.objects().get(from.getBucket(), from.getName()).setGeneration(from.getGeneration()).setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)).setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)).setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)).setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options));
        checkArgument(position >= 0, "Position should be non-negative, is %d", position);
        StringBuilder range = new StringBuilder();
        range.append("bytes=").append(position).append("-").append(position + bytes - 1);
        HttpHeaders requestHeaders = req.getRequestHeaders();
        requestHeaders.setRange(range.toString());
        setEncryptionHeaders(requestHeaders, ENCRYPTION_KEY_PREFIX, options);
        ByteArrayOutputStream output = new ByteArrayOutputStream(bytes);
        HttpResponse httpResponse = req.executeMedia();
        // todo(mziccard) remove when
        // https://github.com/GoogleCloudPlatform/google-cloud-java/issues/982 is fixed
        String contentEncoding = httpResponse.getContentEncoding();
        if (contentEncoding != null && contentEncoding.contains("gzip")) {
            try {
                Field responseField = httpResponse.getClass().getDeclaredField("response");
                responseField.setAccessible(true);
                LowLevelHttpResponse lowLevelHttpResponse = (LowLevelHttpResponse) responseField.get(httpResponse);
                IOUtils.copy(lowLevelHttpResponse.getContent(), output);
            } catch (IllegalAccessException | NoSuchFieldException ex) {
                throw new StorageException(BaseServiceException.UNKNOWN_CODE, "Error parsing gzip response", ex);
            }
        } else {
            httpResponse.download(output);
        }
        String etag = req.getLastResponseHeaders().getETag();
        return Tuple.of(etag, output.toByteArray());
    } catch (IOException ex) {
        StorageException serviceException = translate(ex);
        if (serviceException.getCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
            return Tuple.of(null, new byte[0]);
        }
        throw serviceException;
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Field(java.lang.reflect.Field) Get(com.google.api.services.storage.Storage.Objects.Get) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) StorageException(com.google.cloud.storage.StorageException)

Example 2 with StorageException

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

the class ITBlobSnippets method testBlob.

@Test
public void testBlob() throws IOException {
    BlobSnippets blobSnippets = new BlobSnippets(blob);
    assertTrue(blobSnippets.exists());
    assertArrayEquals(EMPTY_CONTENT, blobSnippets.getContent());
    try {
        assertNotNull(blobSnippets.reload());
        fail("Expected StorageException to be thrown");
    } catch (StorageException ex) {
    // expected
    }
    Blob updatedBlob = blobSnippets.update();
    assertEquals(ImmutableMap.of("key", "value"), updatedBlob.getMetadata());
    Blob copiedBlob = blobSnippets.copyToStrings(BUCKET, "copyBlob");
    assertNotNull(copiedBlob);
    copiedBlob.delete();
    copiedBlob = blobSnippets.copyToId(BUCKET, "copyBlob");
    assertNotNull(copiedBlob);
    copiedBlob.delete();
    copiedBlob = blobSnippets.copyToBucket(BUCKET);
    assertNotNull(copiedBlob);
    copiedBlob.delete();
    blobSnippets.reload();
    blobSnippets.writer();
    URL signedUrl = blobSnippets.signUrl();
    URLConnection connection = signedUrl.openConnection();
    byte[] readBytes = new byte[CONTENT.length];
    try (InputStream responseStream = connection.getInputStream()) {
        assertEquals(CONTENT.length, responseStream.read(readBytes));
        assertArrayEquals(CONTENT, readBytes);
    }
    signedUrl = blobSnippets.signUrlWithSigner(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
    connection = signedUrl.openConnection();
    try (InputStream responseStream = connection.getInputStream()) {
        assertEquals(CONTENT.length, responseStream.read(readBytes));
        assertArrayEquals(CONTENT, readBytes);
    }
    assertFalse(blobSnippets.delete());
    blobSnippets = new BlobSnippets(storage.get(blob.getBucket(), blob.getName()));
    byte[] subcontent = blobSnippets.readContentRange(1, 8);
    assertArrayEquals("ello, W".getBytes(UTF_8), subcontent);
    assertNull(blobSnippets.getAcl());
    assertNotNull(blobSnippets.createAcl());
    Acl updatedAcl = blobSnippets.updateAcl();
    assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(blobSnippets.listAcls());
    assertTrue(acls.contains(updatedAcl));
    assertTrue(blobSnippets.deleteAcl());
    assertNull(blobSnippets.getAcl());
    storage.delete(BlobId.of(BUCKET, BLOB));
}
Also used : Blob(com.google.cloud.storage.Blob) InputStream(java.io.InputStream) Acl(com.google.cloud.storage.Acl) StorageException(com.google.cloud.storage.StorageException) URL(java.net.URL) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 3 with StorageException

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

the class ITStorageSnippets method testBlobAcl.

@Test
public void testBlobAcl() {
    String blobName = "test-blob-acl";
    BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
    BlobInfo blob = BlobInfo.newBuilder(blobId).build();
    Blob createdBlob = storage.create(blob);
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertNotNull(storageSnippets.createBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    Acl updatedAcl = storageSnippets.updateBlobAcl(BUCKET, blobName, createdBlob.getGeneration());
    assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(acls.contains(updatedAcl));
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL));
    storage.createAcl(BlobId.of(BUCKET, blobName), Acl.of(new User(USER_EMAIL), Role.READER));
    Acl userAcl = storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL);
    assertNotNull(userAcl);
    assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail());
    updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration());
    assertEquals(Acl.Role.READER, updatedAcl.getRole());
    assertEquals(User.ofAllUsers(), updatedAcl.getEntity());
    acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(acls.contains(updatedAcl));
    assertNotNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    // test non-existing blob
    String nonExistingBlob = "test-blob-acl";
    assertNull(storageSnippets.getBlobAcl(BUCKET, nonExistingBlob, -1L));
    assertFalse(storageSnippets.deleteBlobAcl(BUCKET, nonExistingBlob, -1L));
    try {
        storageSnippets.createBlobAcl(BUCKET, nonExistingBlob, -1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storageSnippets.updateBlobAcl(BUCKET, nonExistingBlob, -1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storageSnippets.listBlobAcls(BUCKET, nonExistingBlob, -1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
}
Also used : Blob(com.google.cloud.storage.Blob) User(com.google.cloud.storage.Acl.User) BlobInfo(com.google.cloud.storage.BlobInfo) Acl(com.google.cloud.storage.Acl) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 4 with StorageException

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

the class ITStorageSnippets method testCreateCopyAndGetBlob.

@Test
public void testCreateCopyAndGetBlob() {
    String blobName = "test-create-copy-get-blob";
    Blob blob = storageSnippets.createBlobFromByteArray(BUCKET, blobName);
    assertNotNull(blob);
    Blob copiedBlob = storageSnippets.copyBlobInChunks(BUCKET, blobName, "copy-blob");
    assertNotNull(copiedBlob);
    try {
        storageSnippets.getBlobFromIdWithMetageneration(BUCKET, blobName, -1);
        fail("Expected StorageException to be thrown");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(storageSnippets.deleteBlobFromIdWithGeneration(BUCKET, blobName, blob.getGeneration()));
    copiedBlob.delete();
}
Also used : Blob(com.google.cloud.storage.Blob) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 5 with StorageException

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

the class CloudStorageFileSystemProvider method newWriteChannel.

private SeekableByteChannel newWriteChannel(Path path, Set<? extends OpenOption> options) throws IOException {
    initStorage();
    CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
    if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
        throw new CloudStoragePseudoDirectoryException(cloudPath);
    }
    BlobId file = cloudPath.getBlobId();
    BlobInfo.Builder infoBuilder = BlobInfo.newBuilder(file);
    List<Storage.BlobWriteOption> writeOptions = new ArrayList<>();
    List<Acl> acls = new ArrayList<>();
    HashMap<String, String> metas = new HashMap<>();
    for (OpenOption option : options) {
        if (option instanceof OptionMimeType) {
            infoBuilder.setContentType(((OptionMimeType) option).mimeType());
        } else if (option instanceof OptionCacheControl) {
            infoBuilder.setCacheControl(((OptionCacheControl) option).cacheControl());
        } else if (option instanceof OptionContentDisposition) {
            infoBuilder.setContentDisposition(((OptionContentDisposition) option).contentDisposition());
        } else if (option instanceof OptionContentEncoding) {
            infoBuilder.setContentEncoding(((OptionContentEncoding) option).contentEncoding());
        } else if (option instanceof OptionUserMetadata) {
            OptionUserMetadata opMeta = (OptionUserMetadata) option;
            metas.put(opMeta.key(), opMeta.value());
        } else if (option instanceof OptionAcl) {
            acls.add(((OptionAcl) option).acl());
        } else if (option instanceof OptionBlockSize) {
        // TODO: figure out how to plumb in block size.
        } else if (option instanceof StandardOpenOption) {
            switch((StandardOpenOption) option) {
                case CREATE:
                case TRUNCATE_EXISTING:
                case WRITE:
                    // Default behavior.
                    break;
                case SPARSE:
                    // Ignored by specification.
                    break;
                case CREATE_NEW:
                    writeOptions.add(Storage.BlobWriteOption.doesNotExist());
                    break;
                case READ:
                    throw new IllegalArgumentException("READ+WRITE not supported yet");
                case APPEND:
                case DELETE_ON_CLOSE:
                case DSYNC:
                case SYNC:
                default:
                    throw new UnsupportedOperationException(option.toString());
            }
        } else if (option instanceof CloudStorageOption) {
        // XXX: We need to interpret these later
        } else {
            throw new UnsupportedOperationException(option.toString());
        }
    }
    if (!metas.isEmpty()) {
        infoBuilder.setMetadata(metas);
    }
    if (!acls.isEmpty()) {
        infoBuilder.setAcl(acls);
    }
    try {
        return new CloudStorageWriteChannel(storage.writer(infoBuilder.build(), writeOptions.toArray(new Storage.BlobWriteOption[writeOptions.size()])));
    } catch (StorageException oops) {
        throw asIoException(oops);
    }
}
Also used : HashMap(java.util.HashMap) StandardOpenOption(java.nio.file.StandardOpenOption) ArrayList(java.util.ArrayList) BlobInfo(com.google.cloud.storage.BlobInfo) Acl(com.google.cloud.storage.Acl) StandardOpenOption(java.nio.file.StandardOpenOption) OpenOption(java.nio.file.OpenOption) Storage(com.google.cloud.storage.Storage) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException)

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