use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCopyBlobUpdateStorageClass.
@Test
public void testCopyBlobUpdateStorageClass() {
String sourceBlobName = "test-copy-blob-update-storage-class-source";
BlobId source = BlobId.of(BUCKET, sourceBlobName);
BlobInfo sourceInfo = BlobInfo.newBuilder(source).setStorageClass(StorageClass.STANDARD).build();
Blob remoteSourceBlob = storage.create(sourceInfo, BLOB_BYTE_CONTENT);
assertNotNull(remoteSourceBlob);
assertEquals(StorageClass.STANDARD, remoteSourceBlob.getStorageClass());
String targetBlobName = "test-copy-blob-update-storage-class-target";
BlobInfo targetInfo = BlobInfo.newBuilder(BUCKET, targetBlobName).setStorageClass(StorageClass.COLDLINE).build();
Storage.CopyRequest req = Storage.CopyRequest.of(source, targetInfo);
CopyWriter copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertEquals(StorageClass.COLDLINE, copyWriter.getResult().getStorageClass());
assertTrue(copyWriter.isDone());
assertTrue(remoteSourceBlob.delete());
assertTrue(storage.delete(BUCKET, targetBlobName));
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class StorageExample method main.
@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
if (args.length < 1) {
System.out.println("Missing required project id and action");
printUsage();
return;
}
StorageOptions.Builder optionsBuilder = StorageOptions.newBuilder();
StorageAction action;
String actionName;
if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
actionName = args[1];
optionsBuilder.setProjectId(args[0]);
action = ACTIONS.get(args[1]);
args = Arrays.copyOfRange(args, 2, args.length);
} else {
actionName = args[0];
action = ACTIONS.get(args[0]);
args = Arrays.copyOfRange(args, 1, args.length);
}
if (action == null) {
System.out.println("Unrecognized action.");
printUsage();
return;
}
Storage storage = optionsBuilder.build().getService();
Object arg;
try {
arg = action.parse(args);
} catch (IllegalArgumentException ex) {
System.out.printf("Invalid input for action '%s'. %s%n", actionName, ex.getMessage());
System.out.printf("Expected: %s%n", action.params());
return;
} catch (Exception ex) {
System.out.println("Failed to parse arguments.");
ex.printStackTrace();
return;
}
action.run(storage, arg);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class AddBucketIamMember method addBucketIamMember.
/**
* Example of adding a member to the Bucket-level IAM
*/
public static void addBucketIamMember(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";
// For more information please read:
// https://cloud.google.com/storage/docs/access-control/iam
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Policy originalPolicy = storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));
String role = "roles/storage.objectViewer";
String member = "group:example@google.com";
// getBindingsList() returns an ImmutableList and copying over to an ArrayList so it's mutable.
List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
// Create a new binding using role and member
Binding.Builder newMemberBindingBuilder = Binding.newBuilder();
newMemberBindingBuilder.setRole(role).setMembers(Arrays.asList(member));
bindings.add(newMemberBindingBuilder.build());
// Update policy to add member
Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
updatedPolicyBuilder.setBindings(bindings).setVersion(3);
Policy updatedPolicy = storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());
System.out.printf("Added %s with role %s to %s\n", member, role, bucketName);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class ChangeDefaultStorageClass method changeDefaultStorageClass.
public static void changeDefaultStorageClass(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";
// See the StorageClass documentation for other valid storage classes:
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
StorageClass storageClass = StorageClass.COLDLINE;
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);
bucket = bucket.toBuilder().setStorageClass(storageClass).build().update();
System.out.println("Default storage class for bucket " + bucketName + " has been set to " + bucket.getStorageClass());
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class CreateBucket method createBucket.
public static void createBucket(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName).build());
System.out.println("Created bucket " + bucket.getName());
}
Aggregations