@Provides @Singleton Bucket getBucket(@ProjectId String projectId) { Storage storage = StorageOptions.getDefaultInstance().getService(); // Must match BUCKET_NAME for user data bucket in setup_gke_environment.sh String bucketId = "user-data-" + projectId; return storage.get(bucketId); }
/** Example of listing the Bucket-Level IAM Roles and Members */ public Policy listBucketIamMembers(String bucketName) { // [START view_bucket_iam_members] // Initialize a Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // Get IAM Policy for a bucket Policy policy = storage.getIamPolicy(bucketName); // Print Roles and its identities Map<Role, Set<Identity>> policyBindings = policy.getBindings(); for (Map.Entry<Role, Set<Identity>> entry : policyBindings.entrySet()) { System.out.printf("Role: %s Identities: %s\n", entry.getKey(), entry.getValue()); } // [END view_bucket_iam_members] return policy; }
private void initStorage() { if (this.storage != null) { return; } if (storageOptions == null) { this.storage = StorageOptions.getDefaultInstance().getService(); } else { this.storage = storageOptions.getService(); } }
public static void main(String... args) throws IOException { Storage storage = StorageOptions.getDefaultInstance().getService(); BlobId blobId = BlobId.of("bucket", "blob_name"); Blob blob = storage.get(blobId); if (blob != null) { byte[] prevContent = blob.getContent(); System.out.println(new String(prevContent, UTF_8)); WritableByteChannel channel = blob.writer(); channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8))); channel.close(); } } }
/** Example of retrieving Requester pays status on a bucket. */ public Bucket getRequesterPaysStatus(String bucketName) throws StorageException { // [START get_requester_pays_status] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the bucket to retrieve requester-pays status, eg. "my-bucket" // String bucketName = "my-bucket" // Retrieve the bucket, throws StorageException on failure Bucket bucket = storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.BILLING)); System.out.println("Requester pays status : " + bucket.requesterPays()); // [END get_requester_pays_status] return bucket; }
/** Example of default auth */ public Page<Bucket> authListBuckets() { // [START auth_cloud_implicit] // If you don't specify credentials when constructing the client, the // client library will look for credentials in the environment. Storage storage = StorageOptions.getDefaultInstance().getService(); Page<Bucket> buckets = storage.list(); for (Bucket bucket : buckets.iterateAll()) { // do something with the info } // [END auth_cloud_implicit] return buckets; }
/** Example of how to get default event-based hold for a bucket */ public Bucket getDefaultEventBasedHold(String bucketName) throws StorageException { // [START storage_get_default_event_based_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.DEFAULT_EVENT_BASED_HOLD)); if (bucket.getDefaultEventBasedHold() != null && bucket.getDefaultEventBasedHold()) { System.out.println("Default event-based hold is enabled for " + bucketName); } else { System.out.println("Default event-based hold is not enabled for " + bucketName); } // [END storage_get_default_event_based_hold] return bucket; }
/** Example of how to lock a bucket retention policy */ public Bucket lockRetentionPolicy(String bucketName) throws StorageException { // [START storage_lock_retention_policy] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; Bucket bucket = storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.METAGENERATION)); Bucket lockedBucket = bucket.lockRetentionPolicy(Storage.BucketTargetOption.metagenerationMatch()); System.out.println("Retention period for " + bucketName + " is now locked"); System.out.println( "Retention policy effective as of " + new Date(lockedBucket.getRetentionEffectiveTime())); // [END storage_lock_retention_policy] return lockedBucket; }
/** Example of how to disable default event-based hold for a bucket */ public Bucket disableDefaultEventBasedHold(String bucketName) throws StorageException { // [START storage_disable_default_event_based_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; Bucket bucket = storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(false).build()); System.out.println("Default event-based hold was disabled for " + bucketName); // [END storage_disable_default_event_based_hold] return bucket; }
/** Example of how to enable default event-based hold for a bucket */ public Bucket enableDefaultEventBasedHold(String bucketName) throws StorageException { // [START storage_enable_default_event_based_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; Bucket bucket = storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(true).build()); System.out.println("Default event-based hold was enabled for " + bucketName); // [END storage_enable_default_event_based_hold] return bucket; }
/** Example of removing a member from the Bucket-level IAM */ public Policy removeBucketIamMember(String bucketName, Role role, Identity identity) { // [START remove_bucket_iam_member] // Initialize a Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // Get IAM Policy for a bucket Policy policy = storage.getIamPolicy(bucketName); // Remove an identity from a Bucket-level IAM role Policy updatedPolicy = storage.setIamPolicy(bucketName, policy.toBuilder().removeIdentity(role, identity).build()); if (updatedPolicy.getBindings().get(role) == null || !updatedPolicy.getBindings().get(role).contains(identity)) { System.out.printf("Removed %s with role %s from %s\n", identity, role, bucketName); } // [END remove_bucket_iam_member] return updatedPolicy; } }
/** Example of adding a member to the Bucket-level IAM */ public Policy addBucketIamMember(String bucketName, Role role, Identity identity) { // [START add_bucket_iam_member] // Initialize a Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // Get IAM Policy for a bucket Policy policy = storage.getIamPolicy(bucketName); // Add identity to Bucket-level IAM role Policy updatedPolicy = storage.setIamPolicy(bucketName, policy.toBuilder().addIdentity(role, identity).build()); if (updatedPolicy.getBindings().get(role).contains(identity)) { System.out.printf("Added %s with role %s to %s\n", identity, role, bucketName); } // [END add_bucket_iam_member] return updatedPolicy; }
public static void main(String... args) { // [START storage_upload_file] Storage storage = StorageOptions.getDefaultInstance().getService(); BlobId blobId = BlobId.of("bucket", "blob_name"); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8)); // [END storage_upload_file] } }
/** Example of disabling Requester pays on a bucket. */ public Bucket disableRequesterPays(String bucketName) { // [START disable_requester_pays] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the bucket to disable requester-paying for, e.g. "my-bucket" // String bucketName = "my-bucket" BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setRequesterPays(false).build(); // Update the bucket, throws StorageException on failure Bucket bucket = storage.update(bucketInfo); System.out.println("Requester pays status for " + bucketName + ": " + bucket.requesterPays()); // [END disable_requester_pays] return bucket; }
/** Example of enabling Requester pays on a bucket. */ public Bucket enableRequesterPays(String bucketName) throws StorageException { // [START enable_requester_pays] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the existing bucket to enable requester-paying for, e.g. "my-bucket" // String bucketName = "my-bucket" BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setRequesterPays(true).build(); // Update the bucket, throws StorageException on failure Bucket bucket = storage.update(bucketInfo); System.out.println("Requester pay status for " + bucketName + ": " + bucket.requesterPays()); // [END enable_requester_pays] return bucket; }
/** Example of downloading a file. */ public void downloadFile(String bucketName, String srcFilename, Path destFilePath) throws IOException { // [START storage_download_file] // The name of the bucket to access // String bucketName = "my-bucket"; // The name of the remote file to download // String srcFilename = "file.txt"; // The path to which the file should be downloaded // Path destFilePath = Paths.get("/local/path/to/file.txt"); // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // Get specific file from specified bucket Blob blob = storage.get(BlobId.of(bucketName, srcFilename)); // Download file to specified path blob.downloadTo(destFilePath); // [END storage_download_file] }
/** Example of how to set event-based hold for a blob */ public Blob setEventBasedHold(String bucketName, String blobName) throws StorageException { // [START storage_set_event_based_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; // The name of a blob, e.g. "my-blob" // String blobName = "my-blob"; BlobId blobId = BlobId.of(bucketName, blobName); Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(true).build()); System.out.println("Event-based hold was set for " + blobName); // [END storage_set_event_based_hold] return blob; }
/** Example of how to release event-based hold for a blob */ public Blob releaseEventBasedHold(String bucketName, String blobName) throws StorageException { // [START storage_release_event_based_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; // The name of a blob, e.g. "my-blob" // String blobName = "my-blob"; BlobId blobId = BlobId.of(bucketName, blobName); Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(false).build()); System.out.println("Event-based hold was released for " + blobName); // [END storage_release_event_based_hold] return blob; }
/** Example of how to set a temporary hold for a blob */ public Blob setTemporaryHold(String bucketName, String blobName) throws StorageException { // [START storage_set_temporary_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; // The name of a blob, e.g. "my-blob" // String blobName = "my-blob"; BlobId blobId = BlobId.of(bucketName, blobName); Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(true).build()); System.out.println("Temporary hold was set for " + blobName); // [END storage_set_temporary_hold] return blob; }
/** Example of how to release a temporary hold for a blob */ public Blob releaseTemporaryHold(String bucketName, String blobName) throws StorageException { // [START storage_release_temporary_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; // The name of a blob, e.g. "my-blob" // String blobName = "my-blob"; BlobId blobId = BlobId.of(bucketName, blobName); Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(false).build()); System.out.println("Temporary hold was released for " + blobName); // [END storage_release_temporary_hold] return blob; } }