Search in sources :

Example 21 with ReadChannel

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

the class BlobSnippets method reader.

/**
 * Example of reading the blob's content through a reader.
 */
// [TARGET reader(BlobSourceOption...)]
public void reader() throws IOException {
    // [START reader]
    try (ReadChannel reader = blob.reader()) {
        ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
        while (reader.read(bytes) > 0) {
            bytes.flip();
            // do something with bytes
            bytes.clear();
        }
    }
// [END reader]
}
Also used : ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel)

Example 22 with ReadChannel

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

the class StreamObjectDownload method streamObjectDownload.

public static void streamObjectDownload(String projectId, String bucketName, String objectName, String targetFile) throws IOException {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    // The ID of your GCS object
    // String objectName = "your-object-name";
    // The path to the file to download the object to
    // String targetFile = "path/to/your/file";
    Path targetFilePath = Paths.get(targetFile);
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    try (ReadChannel reader = storage.reader(BlobId.of(bucketName, objectName));
        FileChannel targetFileChannel = FileChannel.open(targetFilePath, StandardOpenOption.WRITE)) {
        ByteStreams.copy(reader, targetFileChannel);
        System.out.println("Downloaded object " + objectName + " from bucket " + bucketName + " to " + targetFile + " using a ReadChannel.");
    }
}
Also used : Path(java.nio.file.Path) Storage(com.google.cloud.storage.Storage) FileChannel(java.nio.channels.FileChannel) ReadChannel(com.google.cloud.ReadChannel)

Example 23 with ReadChannel

use of com.google.cloud.ReadChannel in project spanner-jdbc by olavloite.

the class CloudSpannerConnection method getCredentialsFromFile.

public static GoogleCredentials getCredentialsFromFile(String credentialsPath) throws IOException {
    if (credentialsPath == null || credentialsPath.length() == 0)
        throw new IllegalArgumentException("credentialsPath may not be null or empty");
    GoogleCredentials credentials = null;
    if (credentialsPath.startsWith(GOOGLE_CLOUD_STORAGE_PREFIX)) {
        try {
            Storage storage = StorageOptions.newBuilder().build().getService();
            String bucketName = getBucket(credentialsPath);
            String blobName = getBlob(credentialsPath);
            Blob blob = storage.get(bucketName, blobName);
            ReadChannel reader = blob.reader();
            InputStream inputStream = Channels.newInputStream(reader);
            credentials = GoogleCredentials.fromStream(inputStream, CloudSpannerOAuthUtil.HTTP_TRANSPORT_FACTORY);
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid credentials path: " + credentialsPath + ". Reason: " + e.getMessage(), e);
        }
    } else {
        File credentialsFile = new File(credentialsPath);
        if (!credentialsFile.isFile()) {
            throw new IOException(String.format("Error reading credential file %s: File does not exist", credentialsPath));
        }
        try (InputStream credentialsStream = new FileInputStream(credentialsFile)) {
            credentials = GoogleCredentials.fromStream(credentialsStream, CloudSpannerOAuthUtil.HTTP_TRANSPORT_FACTORY);
        }
    }
    return credentials;
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) IOException(java.io.IOException) File(java.io.File) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) JSONException(org.json.JSONException) SQLException(java.sql.SQLException) IOException(java.io.IOException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) SpannerException(com.google.cloud.spanner.SpannerException) FileInputStream(java.io.FileInputStream) ReadChannel(com.google.cloud.ReadChannel)

Example 24 with ReadChannel

use of com.google.cloud.ReadChannel in project data-transfer-project by google.

the class GoogleTempFileStore method getStream.

InputStreamWrapper getStream(UUID jobId, String keyName) {
    String blobName = getDataKeyName(jobId, keyName);
    Blob blob = bucket.get(blobName);
    ReadChannel channel = blob.reader();
    return new InputStreamWrapper(Channels.newInputStream(channel), blob.getSize());
}
Also used : Blob(com.google.cloud.storage.Blob) InputStreamWrapper(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper) ReadChannel(com.google.cloud.ReadChannel)

Aggregations

ReadChannel (com.google.cloud.ReadChannel)24 Test (org.junit.Test)15 ByteBuffer (java.nio.ByteBuffer)11 BlobInfo (com.google.cloud.storage.BlobInfo)8 Blob (com.google.cloud.storage.Blob)6 WriteChannel (com.google.cloud.WriteChannel)5 BlobId (com.google.cloud.storage.BlobId)3 Storage (com.google.cloud.storage.Storage)3 StorageException (com.google.cloud.storage.StorageException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Random (java.util.Random)2 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 Restorable (com.google.cloud.Restorable)1 SpannerException (com.google.cloud.spanner.SpannerException)1 Acl (com.google.cloud.storage.Acl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1