Search in sources :

Example 56 with Storage

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

the class DownloadObject method downloadObject.

public static void downloadObject(String projectId, String bucketName, String objectName, String 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
    // String destFilePath = "/local/path/to/file.txt";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Blob blob = storage.get(BlobId.of(bucketName, objectName));
    blob.downloadTo(Paths.get(destFilePath));
    System.out.println("Downloaded object " + objectName + " from bucket name " + bucketName + " to " + destFilePath);
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage)

Example 57 with Storage

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

the class DownloadPublicObject method downloadPublicObject.

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);
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage)

Example 58 with Storage

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

the class ListObjectsWithPrefix method listObjectsWithPrefix.

public static void listObjectsWithPrefix(String projectId, String bucketName, String directoryPrefix) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    // The directory prefix to search for
    // String directoryPrefix = "myDirectory/"
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    /**
     * Using the Storage.BlobListOption.currentDirectory() option here causes the results to display
     * in a "directory-like" mode, showing what objects are in the directory you've specified, as
     * well as what other directories exist in that directory. For example, given these blobs:
     *
     * <p>a/1.txt a/b/2.txt a/b/3.txt
     *
     * <p>If you specify prefix = "a/" and don't use Storage.BlobListOption.currentDirectory(),
     * you'll get back:
     *
     * <p>a/1.txt a/b/2.txt a/b/3.txt
     *
     * <p>However, if you specify prefix = "a/" and do use
     * Storage.BlobListOption.currentDirectory(), you'll get back:
     *
     * <p>a/1.txt a/b/
     *
     * <p>Because a/1.txt is the only file in the a/ directory and a/b/ is a directory inside the
     * /a/ directory.
     */
    Page<Blob> blobs = storage.list(bucketName, Storage.BlobListOption.prefix(directoryPrefix), Storage.BlobListOption.currentDirectory());
    for (Blob blob : blobs.iterateAll()) {
        System.out.println(blob.getName());
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage)

Example 59 with Storage

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

the class MoveObject method moveObject.

public static void moveObject(String projectId, String sourceBucketName, String sourceObjectName, String targetBucketName, String targetObjectName) {
    // 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 sourceObjectName = "your-object-name";
    // The ID of the bucket to move the object objectName to
    // String targetBucketName = "target-object-bucket"
    // The ID of your GCS object
    // String targetObjectName = "your-new-object-name";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Blob blob = storage.get(sourceBucketName, sourceObjectName);
    // Write a copy of the object to the target bucket
    CopyWriter copyWriter = blob.copyTo(targetBucketName, targetObjectName);
    Blob copiedBlob = copyWriter.getResult();
    // Delete the original blob now that we've copied to where we want it, finishing the "move"
    // operation
    blob.delete();
    System.out.println("Moved object " + sourceObjectName + " from bucket " + sourceBucketName + " to " + targetObjectName + " in bucket " + copiedBlob.getBucket());
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) CopyWriter(com.google.cloud.storage.CopyWriter)

Example 60 with Storage

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

the class ChangeObjectStorageClass method changeObjectStorageClass.

public static void changeObjectStorageClass(String projectId, String bucketName, String objectName) {
    // 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";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(bucketName, objectName);
    // See the StorageClass documentation for other valid storage classes:
    // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
    StorageClass storageClass = StorageClass.COLDLINE;
    // You can't change an object's storage class directly, the only way is to rewrite the object
    // with the
    // desired storage class
    Storage.CopyRequest request = Storage.CopyRequest.newBuilder().setSource(blobId).setTarget(BlobInfo.newBuilder(blobId).setStorageClass(storageClass).build()).build();
    Blob updatedBlob = storage.copy(request).getResult();
    System.out.println("Object " + objectName + " in bucket " + bucketName + " had its storage class set to " + updatedBlob.getStorageClass().name());
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) StorageClass(com.google.cloud.storage.StorageClass) BlobId(com.google.cloud.storage.BlobId)

Aggregations

Storage (com.google.cloud.storage.Storage)140 Bucket (com.google.cloud.storage.Bucket)45 Blob (com.google.cloud.storage.Blob)44 Test (org.junit.Test)30 BlobId (com.google.cloud.storage.BlobId)24 BlobInfo (com.google.cloud.storage.BlobInfo)14 TestRunner (org.apache.nifi.util.TestRunner)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 Policy (com.google.cloud.Policy)9 ArrayList (java.util.ArrayList)8 Acl (com.google.cloud.storage.Acl)7 Date (java.util.Date)7 HashMap (java.util.HashMap)7 BucketInfo (com.google.cloud.storage.BucketInfo)6 HmacKeyMetadata (com.google.cloud.storage.HmacKey.HmacKeyMetadata)6 MockFlowFile (org.apache.nifi.util.MockFlowFile)6 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)5 Binding (com.google.cloud.Binding)5 WriteChannel (com.google.cloud.WriteChannel)4 IOException (java.io.IOException)4