Java Code Examples for com.google.cloud.storage.Blob#downloadTo()

The following examples show how to use com.google.cloud.storage.Blob#downloadTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownloadRequesterPaysObject.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void downloadRequesterPaysObject(
    String projectId, String bucketName, String objectName, Path destFilePath) {
  // The project ID to bill
  // String projectId = "my-billable-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 which the file should be downloaded
  // Path destFilePath = Paths.get("/local/path/to/file.txt");

  Storage storage = StorageOptions.getDefaultInstance().getService();
  Blob blob = storage.get(BlobId.of(bucketName, objectName));
  blob.downloadTo(destFilePath, Blob.BlobSourceOption.userProject(projectId));

  System.out.println(
      "Object " + objectName + " downloaded to " + destFilePath + " and billed to " + projectId);
}
 
Example 2
Source File: DownloadEncryptedObject.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void downloadEncryptedObject(
    String projectId,
    String bucketName,
    String objectName,
    Path destFilePath,
    String decryptionKey)
    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 which the file should be downloaded
  // Path destFilePath = Paths.get("/local/path/to/file.txt");

  // The Base64 encoded decryption key, which should be the same key originally used to encrypt
  // the object
  // String decryptionKey = "TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g=";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

  Blob blob = storage.get(bucketName, objectName);
  blob.downloadTo(destFilePath, Blob.BlobSourceOption.decryptionKey(decryptionKey));

  System.out.println(
      "Downloaded object "
          + objectName
          + " from bucket name "
          + bucketName
          + " to "
          + destFilePath
          + " using customer-supplied encryption key");
}
 
Example 3
Source File: DownloadPublicObject.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void downloadPublicObject(
    String bucketName, String publicObjectName, Path destFilePath) {
  // The name of the bucket to access
  // String bucketName = "my-bucket";

  // The name of the remote public file to download
  // String publicObjectName = "publicfile.txt";

  // The path to which the file should be downloaded
  // Path destFilePath = Paths.get("/local/path/to/file.txt");

  // Instantiate an anonymous Google Cloud Storage client, which can only access public files
  Storage storage = StorageOptions.getUnauthenticatedInstance().getService();

  Blob blob = storage.get(BlobId.of(bucketName, publicObjectName));
  blob.downloadTo(destFilePath);

  System.out.println(
      "Downloaded public object "
          + publicObjectName
          + " from bucket name "
          + bucketName
          + " to "
          + destFilePath);
}
 
Example 4
Source File: ImageMagick.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void blur(BlobInfo blobInfo) throws IOException {
  String bucketName = blobInfo.getBucket();
  String fileName = blobInfo.getName();

  // Download image
  Blob blob = storage.get(BlobId.of(bucketName, fileName));
  Path download = Paths.get("/tmp/", fileName);
  blob.downloadTo(download);

  // Construct the command.
  Path upload = Paths.get("/tmp/", "blurred-" + fileName);
  List<String> args = List.of("convert", download.toString(), "-blur", "0x8", upload.toString());
  try {
    ProcessBuilder pb = new ProcessBuilder(args);
    Process process = pb.start();
    process.waitFor();
  } catch (Exception e) {
    logger.info(String.format("Error: %s", e.getMessage()));
  }

  // Upload image to blurred bucket.
  BlobId blurredBlobId = BlobId.of(BLURRED_BUCKET_NAME, fileName);
  BlobInfo blurredBlobInfo =
      BlobInfo.newBuilder(blurredBlobId).setContentType(blob.getContentType()).build();

  byte[] blurredFile = Files.readAllBytes(upload);
  storage.create(blurredBlobInfo, blurredFile);
  logger.info(
      String.format("Blurred image uploaded to: gs://%s/%s", BLURRED_BUCKET_NAME, fileName));

  // Remove images from fileSystem
  Files.delete(download);
  Files.delete(upload);
}
 
Example 5
Source File: DownloadObject.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void downloadObject(
    String projectId, String bucketName, String objectName, Path destFilePath) {
  // 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 which the file should be downloaded
  // Path destFilePath = Paths.get("/local/path/to/file.txt");

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

  Blob blob = storage.get(BlobId.of(bucketName, objectName));
  blob.downloadTo(destFilePath);

  System.out.println(
      "Downloaded object "
          + objectName
          + " from bucket name "
          + bucketName
          + " to "
          + destFilePath);
}
 
Example 6
Source File: GcsPinotFS.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Override
public void copyToLocalFile(URI srcUri, File dstFile) throws Exception {
  LOGGER.info("Copy {} to local {}", srcUri, dstFile.getAbsolutePath());
  Blob blob = getBlob(srcUri);
  checkState(existsBlob(blob), "File '%s' does not exists", srcUri);
  blob.downloadTo(dstFile.toPath());
}
 
Example 7
Source File: StorageIntegrationTest.java    From gcp-ingestion with Mozilla Public License 2.0 4 votes vote down vote up
private void downloadOutputFiles(String prefix) {
  Page<Blob> blobs = storage.list(bucket, BlobListOption.prefix(prefix));
  for (Blob blob : blobs.iterateAll()) {
    blob.downloadTo(Paths.get(tempFolder.getRoot().getPath(), blob.getName()));
  }
}