Search in sources :

Example 36 with Storage

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);
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 37 with Storage

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);
}
Also used : Storage(com.google.cloud.storage.Storage) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 38 with Storage

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);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 39 with Storage

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);
        }
    }
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) BucketInfo(com.google.cloud.storage.BucketInfo) Map(java.util.Map)

Example 40 with Storage

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);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) BucketInfo(com.google.cloud.storage.BucketInfo)

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