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);
}
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());
}
}
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);
}
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);
}
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);
}
}
Aggregations