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]
}
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.");
}
}
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;
}
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());
}
Aggregations