Search in sources :

Example 71 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class DownloadRequesterPaysObject method downloadRequesterPaysObject.

public static void downloadRequesterPaysObject(String projectId, String bucketName, String objectName, Path destFilePath) {
    // The project ID to bill
    // String projectId = "my-billable-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
    // Path destFilePath = Paths.get("/local/path/to/file.txt");
    Storage storage = StorageOptions.getDefaultInstance().getService();
    Blob blob = storage.get(BlobId.of(bucketName, objectName));
    blob.downloadTo(destFilePath, Blob.BlobSourceOption.userProject(projectId));
    System.out.println("Object " + objectName + " downloaded to " + destFilePath + " and billed to " + projectId);
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage)

Example 72 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class ListObjectsWithOldVersions method listObjectsWithOldVersions.

public static void listObjectsWithOldVersions(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);
    Page<Blob> blobs = bucket.list(Storage.BlobListOption.versions(true));
    for (Blob blob : blobs.iterateAll()) {
        System.out.println(blob.getName() + "," + blob.getGeneration());
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 73 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class RotateObjectEncryptionKey method rotateObjectEncryptionKey.

public static void rotateObjectEncryptionKey(String projectId, String bucketName, String objectName, String oldEncryptionKey, String newEncryptionKey) {
    // 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 Base64 encoded AES-256 encryption key originally used to encrypt the object. See the
    // documentation
    // on Customer-Supplied Encryption keys for more info:
    // https://cloud.google.com/storage/docs/encryption/using-customer-supplied-keys
    // String oldEncryptionKey = "TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g="
    // The new encryption key to use
    // String newEncryptionKey = "0mMWhFvQOdS4AmxRpo8SJxXn5MjFhbz7DkKBUdUIef8="
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(bucketName, objectName);
    // You can't change an object's encryption key directly, the only way is to overwrite the object
    Storage.CopyRequest request = Storage.CopyRequest.newBuilder().setSource(blobId).setSourceOptions(Storage.BlobSourceOption.decryptionKey(oldEncryptionKey)).setTarget(blobId, Storage.BlobTargetOption.encryptionKey(newEncryptionKey)).build();
    storage.copy(request);
    System.out.println("Rotated encryption key for object " + objectName + "in bucket " + bucketName);
}
Also used : Storage(com.google.cloud.storage.Storage) BlobId(com.google.cloud.storage.BlobId)

Example 74 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class RemoteStorageHelperTest method testForceDeleteNoTimeout.

@Test
public void testForceDeleteNoTimeout() {
    Storage storageMock = EasyMock.createMock(Storage.class);
    EasyMock.expect(blob1.getBlobId()).andReturn(BLOB_ID1);
    EasyMock.expect(storageMock.delete(BLOB_ID1)).andReturn(true);
    EasyMock.expect(blob2.getBlobId()).andReturn(BLOB_ID2);
    EasyMock.expect(storageMock.delete(BLOB_ID2)).andReturn(true);
    EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))).andReturn(blobPage);
    EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
    EasyMock.replay(storageMock, blob1, blob2);
    RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME);
    EasyMock.verify(storageMock);
}
Also used : Storage(com.google.cloud.storage.Storage) Test(org.junit.Test)

Example 75 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class RemoteStorageHelperTest method testForceDeleteNoTimeoutFail.

@Test
public void testForceDeleteNoTimeoutFail() {
    Storage storageMock = EasyMock.createMock(Storage.class);
    EasyMock.expect(blob1.getBlobId()).andReturn(BLOB_ID1);
    EasyMock.expect(storageMock.delete(BLOB_ID1)).andReturn(true);
    EasyMock.expect(blob2.getBlobId()).andReturn(BLOB_ID2);
    EasyMock.expect(storageMock.delete(BLOB_ID2)).andReturn(true);
    EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))).andReturn(blobPage);
    EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
    EasyMock.replay(storageMock, blob1, blob2);
    thrown.expect(StorageException.class);
    try {
        RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME);
    } finally {
        EasyMock.verify(storageMock);
    }
}
Also used : Storage(com.google.cloud.storage.Storage) Test(org.junit.Test)

Aggregations

Storage (com.google.cloud.storage.Storage)140 Bucket (com.google.cloud.storage.Bucket)45 Blob (com.google.cloud.storage.Blob)44 Test (org.junit.Test)30 BlobId (com.google.cloud.storage.BlobId)24 BlobInfo (com.google.cloud.storage.BlobInfo)14 TestRunner (org.apache.nifi.util.TestRunner)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 Policy (com.google.cloud.Policy)9 ArrayList (java.util.ArrayList)8 Acl (com.google.cloud.storage.Acl)7 Date (java.util.Date)7 HashMap (java.util.HashMap)7 BucketInfo (com.google.cloud.storage.BucketInfo)6 HmacKeyMetadata (com.google.cloud.storage.HmacKey.HmacKeyMetadata)6 MockFlowFile (org.apache.nifi.util.MockFlowFile)6 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)5 Binding (com.google.cloud.Binding)5 WriteChannel (com.google.cloud.WriteChannel)4 IOException (java.io.IOException)4