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);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class GetBucketMetadata method getBucketMetadata.
public static void getBucketMetadata(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();
// Select all fields. Fields can be selected individually e.g. Storage.BucketField.NAME
Bucket bucket = storage.get(bucketName, Storage.BucketGetOption.fields(Storage.BucketField.values()));
// Print bucket metadata
System.out.println("BucketName: " + bucket.getName());
System.out.println("DefaultEventBasedHold: " + bucket.getDefaultEventBasedHold());
System.out.println("DefaultKmsKeyName: " + bucket.getDefaultKmsKeyName());
System.out.println("Id: " + bucket.getGeneratedId());
System.out.println("IndexPage: " + bucket.getIndexPage());
System.out.println("Location: " + bucket.getLocation());
System.out.println("LocationType: " + bucket.getLocationType());
System.out.println("Metageneration: " + bucket.getMetageneration());
System.out.println("NotFoundPage: " + bucket.getNotFoundPage());
System.out.println("RetentionEffectiveTime: " + bucket.getRetentionEffectiveTime());
System.out.println("RetentionPeriod: " + bucket.getRetentionPeriod());
System.out.println("RetentionPolicyIsLocked: " + bucket.retentionPolicyIsLocked());
System.out.println("RequesterPays: " + bucket.requesterPays());
System.out.println("SelfLink: " + bucket.getSelfLink());
System.out.println("StorageClass: " + bucket.getStorageClass().name());
System.out.println("TimeCreated: " + bucket.getCreateTime());
System.out.println("VersioningEnabled: " + bucket.versioningEnabled());
if (bucket.getLabels() != null) {
System.out.println("\n\n\nLabels:");
for (Map.Entry<String, String> label : bucket.getLabels().entrySet()) {
System.out.println(label.getKey() + "=" + label.getValue());
}
}
if (bucket.getLifecycleRules() != null) {
System.out.println("\n\n\nLifecycle Rules:");
for (BucketInfo.LifecycleRule rule : bucket.getLifecycleRules()) {
System.out.println(rule);
}
}
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class GetPublicAccessPrevention method getPublicAccessPrevention.
public static void getPublicAccessPrevention(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);
// Gets Bucket Metadata and prints publicAccessPrevention value (either 'inherited' or
// 'enforced').
BucketInfo.PublicAccessPrevention publicAccessPrevention = bucket.getIamConfiguration().getPublicAccessPrevention();
System.out.println("Public access prevention is set to " + publicAccessPrevention.getValue() + " for " + bucketName);
}
Aggregations