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