/** * * Create a bucket with the specified name in the specified project. If successful, return the * created bucket metadata. * * <p>For more information, see <a * href="https://cloud.google.com/storage/docs/json_api/v1/buckets/insert" * target="_blank">Buckets:insert</a> * * @param bucketName The name of the bucket to create. * @throws IOException IO error * @return true if the operation succeeded; otherwise, false */ public static boolean insertBucket(String bucketName) throws IOException { displayMessageHeader("Create the bucket: " + bucketName); Storage.Buckets buckets = storageService.buckets(); boolean bucketCreated = false; // Set the bucket access control list. ObjectAccessControl acl = new ObjectAccessControl(); acl.setEntity("allAuthenticatedUsers").setRole("READER"); // Create bucket. Bucket newBucket = new Bucket().setName(bucketName); newBucket.setLocation("US"); newBucket.setDefaultObjectAcl(ImmutableList.of(acl)); Storage.Buckets.Insert insertBucket = buckets.insert(settings.getProject(), newBucket); try { @SuppressWarnings("unused") Bucket createdBucket = insertBucket.execute(); bucketCreated = true; System.out.println(String.format("Bucket %s created", bucketName)); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); if (error.getCode() == HTTP_CONFLICT && error.getMessage().contains("You already own this bucket.")) { System.out.println("already exists"); } else { throw e; } } return bucketCreated; }