Search in sources :

Example 41 with Storage

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

the class ListBuckets method listBuckets.

public static void listBuckets(String projectId) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Page<Bucket> buckets = storage.list();
    for (Bucket bucket : buckets.iterateAll()) {
        System.out.println(bucket.getName());
    }
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 42 with Storage

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

the class RemoveBucketIamConditionalBinding method removeBucketIamConditionalBinding.

/**
 * Example of removing a conditional binding to the Bucket-level IAM
 */
public static void removeBucketIamConditionalBinding(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    // For more information please read:
    // https://cloud.google.com/storage/docs/access-control/iam
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Policy originalPolicy = storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));
    String role = "roles/storage.objectViewer";
    // getBindingsList() returns an ImmutableList and copying over to an ArrayList so it's mutable.
    List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
    // Create a condition to compare against
    Condition.Builder conditionBuilder = Condition.newBuilder();
    conditionBuilder.setTitle("Title");
    conditionBuilder.setDescription("Description");
    conditionBuilder.setExpression("resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")");
    Iterator iterator = bindings.iterator();
    while (iterator.hasNext()) {
        Binding binding = (Binding) iterator.next();
        boolean foundRole = binding.getRole().equals(role);
        boolean conditionsEqual = conditionBuilder.build().equals(binding.getCondition());
        // Remove condition when the role and condition are equal
        if (foundRole && conditionsEqual) {
            iterator.remove();
            break;
        }
    }
    // Update policy to remove conditional binding
    Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
    updatedPolicyBuilder.setBindings(bindings).setVersion(3);
    Policy updatedPolicy = storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());
    System.out.println("Conditional Binding was removed.");
}
Also used : Policy(com.google.cloud.Policy) Binding(com.google.cloud.Binding) Condition(com.google.cloud.Condition) Storage(com.google.cloud.storage.Storage) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator)

Example 43 with Storage

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

the class SetDefaultRpo method setDefaultRpo.

public static void setDefaultRpo(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    bucket.toBuilder().setRpo(Rpo.DEFAULT).build().update();
    System.out.println("Replication was set to default for " + bucketName);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 44 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 45 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)

Aggregations

Storage (com.google.cloud.storage.Storage)339 Blob (com.google.cloud.storage.Blob)122 Bucket (com.google.cloud.storage.Bucket)91 BlobId (com.google.cloud.storage.BlobId)74 Test (org.junit.Test)65 BlobInfo (com.google.cloud.storage.BlobInfo)56 IOException (java.io.IOException)26 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)23 ArrayList (java.util.ArrayList)23 Before (org.junit.Before)21 PrintStream (java.io.PrintStream)18 Acl (com.google.cloud.storage.Acl)16 Path (java.nio.file.Path)16 StorageException (com.google.cloud.storage.StorageException)14 StorageOptions (com.google.cloud.storage.StorageOptions)13 TestRunner (org.apache.nifi.util.TestRunner)12 WriteChannel (com.google.cloud.WriteChannel)11 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)10 File (java.io.File)10