/**
   * * 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;
  }
Пример #2
0
  @Test
  public void testBucketDoesNotExistBecauseOfAccessError() throws IOException {
    GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
    GcsUtil gcsUtil = pipelineOptions.getGcsUtil();

    Storage mockStorage = Mockito.mock(Storage.class);
    gcsUtil.setStorageClient(mockStorage);

    Storage.Buckets mockStorageObjects = Mockito.mock(Storage.Buckets.class);
    Storage.Buckets.Get mockStorageGet = Mockito.mock(Storage.Buckets.Get.class);

    BackOff mockBackOff = new AttemptBoundedExponentialBackOff(3, 200);
    GoogleJsonResponseException expectedException =
        googleJsonResponseException(
            HttpStatusCodes.STATUS_CODE_FORBIDDEN,
            "Waves hand mysteriously",
            "These aren't the buckets your looking for");

    when(mockStorage.buckets()).thenReturn(mockStorageObjects);
    when(mockStorageObjects.get("testbucket")).thenReturn(mockStorageGet);
    when(mockStorageGet.execute()).thenThrow(expectedException);

    assertFalse(
        gcsUtil.bucketExists(
            GcsPath.fromComponents("testbucket", "testobject"),
            mockBackOff,
            new FastNanoClockAndSleeper()));
  }
Пример #3
0
  @Test
  public void testBucketDoesNotExist() throws IOException {
    GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
    GcsUtil gcsUtil = pipelineOptions.getGcsUtil();

    Storage mockStorage = Mockito.mock(Storage.class);
    gcsUtil.setStorageClient(mockStorage);

    Storage.Buckets mockStorageObjects = Mockito.mock(Storage.Buckets.class);
    Storage.Buckets.Get mockStorageGet = Mockito.mock(Storage.Buckets.Get.class);

    BackOff mockBackOff = new AttemptBoundedExponentialBackOff(3, 200);

    when(mockStorage.buckets()).thenReturn(mockStorageObjects);
    when(mockStorageObjects.get("testbucket")).thenReturn(mockStorageGet);
    when(mockStorageGet.execute())
        .thenThrow(
            googleJsonResponseException(
                HttpStatusCodes.STATUS_CODE_NOT_FOUND, "It don't exist", "Nothing here to see"));

    assertFalse(
        gcsUtil.bucketExists(
            GcsPath.fromComponents("testbucket", "testobject"),
            mockBackOff,
            new FastNanoClockAndSleeper()));
  }
Пример #4
0
  @Test
  public void testBucketExists() throws IOException {
    GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
    GcsUtil gcsUtil = pipelineOptions.getGcsUtil();

    Storage mockStorage = Mockito.mock(Storage.class);
    gcsUtil.setStorageClient(mockStorage);

    Storage.Buckets mockStorageObjects = Mockito.mock(Storage.Buckets.class);
    Storage.Buckets.Get mockStorageGet = Mockito.mock(Storage.Buckets.Get.class);

    BackOff mockBackOff = new AttemptBoundedExponentialBackOff(3, 200);

    when(mockStorage.buckets()).thenReturn(mockStorageObjects);
    when(mockStorageObjects.get("testbucket")).thenReturn(mockStorageGet);
    when(mockStorageGet.execute())
        .thenThrow(new SocketTimeoutException("SocketException"))
        .thenReturn(new Bucket());

    assertTrue(
        gcsUtil.bucketExists(
            GcsPath.fromComponents("testbucket", "testobject"),
            mockBackOff,
            new FastNanoClockAndSleeper()));
  }