use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method getUniformBucketLevelAccess.
/**
* Example of how to get uniform bucket-level access metadata for a bucket
*/
public Bucket getUniformBucketLevelAccess(String bucketName) throws StorageException {
// [START storage_get_uniform_bucket_level_access]
// 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.IAMCONFIGURATION));
BucketInfo.IamConfiguration iamConfiguration = bucket.getIamConfiguration();
Boolean enabled = iamConfiguration.isUniformBucketLevelAccessEnabled();
Date lockedTime = new Date(iamConfiguration.getUniformBucketLevelAccessLockedTime());
if (enabled != null && enabled) {
System.out.println("Uniform bucket-level access is enabled for " + bucketName);
System.out.println("Bucket will be locked on " + lockedTime);
} else {
System.out.println("Uniform bucket-level access is disabled for " + bucketName);
}
// [END storage_get_uniform_bucket_level_access]
return bucket;
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method activateHmacKey.
/**
* Example of activating a previously deactivated HMAC key. *
*/
public HmacKeyMetadata activateHmacKey(String accessId, String projectId) throws StorageException {
// [START storage_activate_hmac_key]
// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();
// The access ID of the HMAC key, e.g. "GOOG0234230X00"
// String accessId = "GOOG0234230X00";
//
// The ID of the project to which the service account belongs.
// String projectId = "project-id";
HmacKeyMetadata metadata = storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));
HmacKeyMetadata newMetadata = storage.updateHmacKeyState(metadata, HmacKeyState.ACTIVE);
System.out.println("The HMAC key is now active.");
System.out.println("The HMAC key metadata is:");
System.out.println("ID: " + newMetadata.getId());
System.out.println("Access ID: " + newMetadata.getAccessId());
System.out.println("Project ID: " + newMetadata.getProjectId());
System.out.println("Service Account Email: " + newMetadata.getServiceAccount().getEmail());
System.out.println("State: " + newMetadata.getState().toString());
System.out.println("Time Created: " + new Date(newMetadata.getCreateTime()).toString());
System.out.println("Time Updated: " + new Date(newMetadata.getUpdateTime()).toString());
System.out.println("ETag: " + newMetadata.getEtag());
// [END storage_activate_hmac_key]
return newMetadata;
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class CreateAndListBucketsAndBlobs method main.
public static void main(String... args) {
// Create a service object
// Credentials are inferred from the environment.
Storage storage = StorageOptions.getDefaultInstance().getService();
// Create a bucket
// Change this to something unique
String bucketName = "my_unique_bucket";
Bucket bucket = storage.create(BucketInfo.of(bucketName));
// Upload a blob to the newly created bucket
Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");
// Read the blob content from the server
String blobContent = new String(blob.getContent(), UTF_8);
// List all your buckets
System.out.println("My buckets:");
for (Bucket currentBucket : storage.list().iterateAll()) {
System.out.println(currentBucket);
}
// List the blobs in a particular bucket
System.out.println("My blobs:");
for (Blob currentBlob : bucket.list().iterateAll()) {
System.out.println(currentBlob);
}
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class UploadObjectFromMemory method uploadObjectFromMemory.
public static void uploadObjectFromMemory(String projectId, String bucketName, String objectName, String contents) 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 string of contents you wish to upload
// String contents = "Hello world!";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
byte[] content = contents.getBytes(StandardCharsets.UTF_8);
storage.createFrom(blobInfo, new ByteArrayInputStream(content));
System.out.println("Object " + objectName + " uploaded to bucket " + bucketName + " with contents " + contents);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class GetBucketRpo method getBucketRpo.
public static void getBucketRpo(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);
String rpo = bucket.getRpo().toString();
System.out.println("The RPO setting of bucket " + bucketName + " is " + rpo);
}
Aggregations