com.google.api.services.storage.model.Bucket Java Examples

The following examples show how to use com.google.api.services.storage.model.Bucket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleStorageWebsiteDistributionConfiguration.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void write(final Path file, final Distribution distribution, final LoginCallback prompt) throws BackgroundException {
    final Path container = containerService.getContainer(file);
    try {
        String suffix = "index.html";
        if(StringUtils.isNotBlank(distribution.getIndexDocument())) {
            suffix = PathNormalizer.name(distribution.getIndexDocument());
        }
        // Enable website endpoint
        session.getClient().buckets().patch(container.getName(), new Bucket()
            .setLogging(new Bucket.Logging()
                .setLogObjectPrefix(distribution.isEnabled() ? PreferencesFactory.get().getProperty("google.logging.prefix") : null)
                .setLogBucket(StringUtils.isNotBlank(distribution.getLoggingContainer()) ? distribution.getLoggingContainer() : container.getName()))
            .setWebsite(
                distribution.isEnabled() ? new Bucket.Website().setMainPageSuffix(suffix) : null
            )).execute();
    }
    catch(IOException e) {
        throw new GoogleStorageExceptionMappingService().map("Cannot write website configuration", e);
    }
}
 
Example #2
Source File: GoogleCloudStorageImpl.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * See {@link GoogleCloudStorage#listBucketInfo()} for details about expected behavior.
 */
@Override
public List<GoogleCloudStorageItemInfo> listBucketInfo()
    throws IOException {
  logger.atFine().log("listBucketInfo()");
  List<Bucket> allBuckets = listBucketsInternal();
  List<GoogleCloudStorageItemInfo> bucketInfos = new ArrayList<>(allBuckets.size());
  for (Bucket bucket : allBuckets) {
    bucketInfos.add(
        new GoogleCloudStorageItemInfo(
            new StorageResourceId(bucket.getName()),
            bucket.getTimeCreated().getValue(),
            bucket.getUpdated().getValue(),
            /* size= */ 0,
            bucket.getLocation(),
            bucket.getStorageClass()));
  }
  return bucketInfos;
}
 
Example #3
Source File: GoogleStorage.java    From halyard with Apache License 2.0 6 votes vote down vote up
private static Bucket createBucket(
    Storage storage, String projectId, String locationId, String bucketId) {
  try {
    Bucket bucket =
        new Bucket()
            .setLocation(locationId)
            .setName(bucketId)
            .setVersioning(new Bucket.Versioning().setEnabled(true));

    if (!StringUtils.isEmpty(locationId)) {
      bucket.setLocation(locationId);
    }

    return storage.buckets().insert(projectId, bucket).execute();
  } catch (IOException e) {
    throw new RuntimeException("Unable to create bucket", e);
  }
}
 
Example #4
Source File: GcsUtilTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBucket() 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 = BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff());

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

  assertNotNull(
      gcsUtil.getBucket(
          GcsPath.fromComponents("testbucket", "testobject"),
          mockBackOff,
          new FastNanoClockAndSleeper()));
}
 
Example #5
Source File: GcsUtilTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testBucketAccessible() 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 = BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff());

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

  assertTrue(
      gcsUtil.bucketAccessible(
          GcsPath.fromComponents("testbucket", "testobject"),
          mockBackOff,
          new FastNanoClockAndSleeper()));
}
 
Example #6
Source File: GoogleCloudStorageImpl.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/** Helper for converting a StorageResourceId + Bucket into a GoogleCloudStorageItemInfo. */
public static GoogleCloudStorageItemInfo createItemInfoForBucket(
    StorageResourceId resourceId, Bucket bucket) {
  Preconditions.checkArgument(resourceId != null, "resourceId must not be null");
  Preconditions.checkArgument(bucket != null, "bucket must not be null");
  Preconditions.checkArgument(
      resourceId.isBucket(), "resourceId must be a Bucket. resourceId: %s", resourceId);
  Preconditions.checkArgument(
      resourceId.getBucketName().equals(bucket.getName()),
      "resourceId.getBucketName() must equal bucket.getName(): '%s' vs '%s'",
      resourceId.getBucketName(), bucket.getName());

  // For buckets, size is 0.
  return new GoogleCloudStorageItemInfo(
      resourceId,
      bucket.getTimeCreated().getValue(),
      bucket.getUpdated().getValue(),
      /* size= */ 0,
      bucket.getLocation(),
      bucket.getStorageClass());
}
 
Example #7
Source File: GcsUtilTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateBucket() throws IOException {
  GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
  GcsUtil gcsUtil = pipelineOptions.getGcsUtil();

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

  Storage.Buckets.Insert mockStorageInsert = Mockito.mock(Storage.Buckets.Insert.class);

  BackOff mockBackOff = BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff());

  when(mockStorage.buckets()).thenReturn(mockStorageObjects);
  when(mockStorageObjects.insert(any(String.class), any(Bucket.class)))
      .thenReturn(mockStorageInsert);
  when(mockStorageInsert.execute())
      .thenThrow(new SocketTimeoutException("SocketException"))
      .thenReturn(new Bucket());

  gcsUtil.createBucket("a", new Bucket(), mockBackOff, new FastNanoClockAndSleeper());
}
 
Example #8
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/** Test handling of rate-limiting and back-off in GoogleCloudStorage.create(String). */
@Test
public void testCreateBucketRateLimited() throws Exception {
  Bucket bucket = newBucket(BUCKET_NAME);
  MockHttpTransport transport =
      mockTransport(jsonErrorResponse(ErrorResponses.RATE_LIMITED), jsonDataResponse(bucket));

  GoogleCloudStorage gcs = mockedGcs(transport);

  gcs.create(BUCKET_NAME);

  assertThat(trackingHttpRequestInitializer.getAllRequestStrings())
      .containsExactly(
          createBucketRequestString(PROJECT_ID), createBucketRequestString(PROJECT_ID))
      .inOrder();
}
 
Example #9
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/** Test handling of rate-limiting and back-off in GoogleCloudStorage.delete(1). */
@Test
public void testDeleteBucketRateLimited() throws Exception {
  Bucket bucket = newBucket(BUCKET_NAME);
  MockHttpTransport transport =
      mockTransport(jsonErrorResponse(ErrorResponses.RATE_LIMITED), jsonDataResponse(bucket));

  GoogleCloudStorage gcs = mockedGcs(transport);

  gcs.deleteBuckets(ImmutableList.of(BUCKET_NAME));

  assertThat(trackingHttpRequestInitializer.getAllRequestStrings())
      .containsExactly(
          deleteBucketRequestString(BUCKET_NAME), deleteBucketRequestString(BUCKET_NAME))
      .inOrder();
}
 
Example #10
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/** Test for GoogleCloudStorage.listBucketNames(0). */
@Test
public void testListBucketNames() throws IOException {
  List<Bucket> buckets =
      ImmutableList.of(newBucket("bucket0"), newBucket("bucket1"), newBucket("bucket2"));

  MockHttpTransport transport = mockTransport(jsonDataResponse(new Buckets().setItems(buckets)));

  GoogleCloudStorage gcs = mockedGcs(transport);

  List<String> bucketNames = gcs.listBucketNames();

  assertThat(bucketNames).containsExactly("bucket0", "bucket1", "bucket2").inOrder();
  assertThat(trackingHttpRequestInitializer.getAllRequestStrings())
      .containsExactly(listBucketsRequestString(PROJECT_ID))
      .inOrder();
}
 
Example #11
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/** Test for GoogleCloudStorage.listBucketInfo(0). */
@Test
public void testListBucketInfo() throws IOException {
  List<Bucket> buckets =
      ImmutableList.of(newBucket("bucket0"), newBucket("bucket1"), newBucket("bucket2"));

  MockHttpTransport transport = mockTransport(jsonDataResponse(new Buckets().setItems(buckets)));

  GoogleCloudStorage gcs = mockedGcs(transport);

  List<GoogleCloudStorageItemInfo> bucketInfos = gcs.listBucketInfo();

  assertThat(bucketInfos)
      .containsExactlyElementsIn(
          buckets.stream()
              .map(b -> createItemInfoForBucket(new StorageResourceId(b.getName()), b))
              .toArray())
      .inOrder();
  assertThat(trackingHttpRequestInitializer.getAllRequestStrings())
      .containsExactly(listBucketsRequestString(PROJECT_ID))
      .inOrder();
}
 
Example #12
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * Test GoogleCloudStorage.getItemInfo(StorageResourceId) when arguments represent only a bucket.
 */
@Test
public void testGetItemInfoBucket() throws IOException {
  Bucket bucket = newBucket(BUCKET_NAME);
  StorageResourceId bucketId = new StorageResourceId(bucket.getName());

  MockHttpTransport transport = mockTransport(jsonDataResponse(bucket));

  GoogleCloudStorage gcs = mockedGcs(transport);

  GoogleCloudStorageItemInfo info = gcs.getItemInfo(bucketId);

  assertThat(info).isEqualTo(createItemInfoForBucket(bucketId, bucket));
  assertThat(trackingHttpRequestInitializer.getAllRequestStrings())
      .containsExactly(getBucketRequestString(BUCKET_NAME))
      .inOrder();
}
 
Example #13
Source File: GoogleStorageStorageClassFeature.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setClass(final Path file, final String redundancy) throws BackgroundException {
    try {
        if(containerService.isContainer(file)) {
            // Changing the default storage class of a bucket
            session.getClient().buckets().patch(containerService.getContainer(file).getName(),
                new Bucket().setStorageClass(redundancy)
            ).execute();
        }
        else {
            session.getClient().objects().rewrite(containerService.getContainer(file).getName(),
                containerService.getKey(file), containerService.getContainer(file).getName(), containerService.getKey(file),
                new StorageObject().setStorageClass(redundancy)
            ).execute();
        }
    }
    catch(IOException e) {
        throw new GoogleStorageExceptionMappingService().map("Failure to write attributes of {0}", e, file);
    }
}
 
Example #14
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/** Test handling of mismatch in Bucket.getName() vs StorageResourceId.getBucketName(). */
@Test
public void testGetItemInfoBucketReturnMismatchedName() throws IOException {
  Bucket bucket = newBucket("wrong-bucket-name");
  StorageResourceId bucketId = new StorageResourceId(BUCKET_NAME);

  MockHttpTransport transport = mockTransport(jsonDataResponse(bucket));

  GoogleCloudStorage gcs = mockedGcs(transport);

  IllegalArgumentException thrown =
      assertThrows(IllegalArgumentException.class, () -> gcs.getItemInfo(bucketId));

  String expectedMsg =
      String.format(
          "resourceId.getBucketName() must equal bucket.getName(): '%s' vs '%s'",
          BUCKET_NAME, bucket.getName());
  assertThat(thrown).hasMessageThat().isEqualTo(expectedMsg);

  assertThat(trackingHttpRequestInitializer.getAllRequestStrings())
      .containsExactly(getBucketRequestString(BUCKET_NAME))
      .inOrder();
}
 
Example #15
Source File: GoogleStorageAccessControlListFeature.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setPermission(final Path file, final Acl acl) throws BackgroundException {
    try {
        if(containerService.isContainer(file)) {
            final List<BucketAccessControl> bucketAccessControls = this.toBucketAccessControl(acl);
            session.getClient().buckets().update(containerService.getContainer(file).getName(),
                new Bucket().setAcl(bucketAccessControls)).execute();
        }
        else {
            final List<ObjectAccessControl> objectAccessControls = this.toObjectAccessControl(acl);
            session.getClient().objects().update(containerService.getContainer(file).getName(), containerService.getKey(file),
                new StorageObject().setAcl(objectAccessControls)).execute();
        }
    }
    catch(IOException e) {
        final BackgroundException failure = new GoogleStorageExceptionMappingService().map("Cannot change permissions of {0}", e, file);
        if(file.isPlaceholder()) {
            if(failure instanceof NotfoundException) {
                // No placeholder file may exist but we just have a common prefix
                return;
            }
        }
        // 400 Bad Request response for buckets with uniform bucket-level access enabled
        throw failure;
    }
}
 
Example #16
Source File: GoogleCloudStorageImpl.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * See {@link GoogleCloudStorage#getItemInfo(StorageResourceId)} for details about expected
 * behavior.
 */
@Override
public GoogleCloudStorageItemInfo getItemInfo(StorageResourceId resourceId)
    throws IOException {
  logger.atFine().log("getItemInfo(%s)", resourceId);

  // Handle ROOT case first.
  if (resourceId.isRoot()) {
    return GoogleCloudStorageItemInfo.ROOT_INFO;
  }

  GoogleCloudStorageItemInfo itemInfo = null;

  // Determine object size.
  //
  // For buckets, size is 0.
  // For objects not found, size is -1.
  // For objects that exist, size is in number of bytes.
  if (resourceId.isBucket()) {
    Bucket bucket = getBucket(resourceId.getBucketName());
    if (bucket != null) {
      itemInfo = createItemInfoForBucket(resourceId, bucket);
    }
  } else {
    StorageObject object = getObject(resourceId);
    if (object != null) {
      itemInfo = createItemInfoForStorageObject(resourceId, object);
    }
  }

  if (itemInfo == null) {
    itemInfo = GoogleCloudStorageItemInfo.createNotFound(resourceId);
  }
  logger.atFine().log("getItemInfo: %s", itemInfo);
  return itemInfo;
}
 
Example #17
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetItemInfos() throws IOException {
  Bucket bucket = newBucket(BUCKET_NAME);
  StorageObject storageObject = newStorageObject(BUCKET_NAME, OBJECT_NAME);

  MockHttpTransport transport =
      mockBatchTransport(
          /* requestsPerBatch= */ 2, jsonDataResponse(storageObject), jsonDataResponse(bucket));

  GoogleCloudStorage gcs = mockedGcs(transport);

  // Call in order of StorageObject, Bucket.
  List<GoogleCloudStorageItemInfo> itemInfos =
      gcs.getItemInfos(
          ImmutableList.of(
              new StorageResourceId(BUCKET_NAME, OBJECT_NAME),
              new StorageResourceId(BUCKET_NAME)));

  assertThat(itemInfos)
      .containsExactly(
          GoogleCloudStorageImpl.createItemInfoForStorageObject(
              new StorageResourceId(BUCKET_NAME, OBJECT_NAME), storageObject),
          GoogleCloudStorageImpl.createItemInfoForBucket(
              new StorageResourceId(BUCKET_NAME), bucket))
      .inOrder();

  assertThat(trackingHttpRequestInitializer.getAllRequestStrings())
      .containsExactly(
          batchRequestString(),
          getRequestString(BUCKET_NAME, OBJECT_NAME),
          getBucketRequestString(BUCKET_NAME))
      .inOrder();
}
 
Example #18
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
static Bucket newBucket(String name) {
  return new Bucket()
      .setName(name)
      .setLocation("us-central1-a")
      .setStorageClass("class-af4")
      .setTimeCreated(new DateTime(new Date()))
      .setUpdated(new DateTime(new Date()));
}
 
Example #19
Source File: GoogleStorageWebsiteDistributionConfiguration.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Distribution read(final Path file, final Distribution.Method method, final LoginCallback prompt) throws BackgroundException {
    final Path container = containerService.getContainer(file);
    final URI origin = URI.create(String.format("%s://%s.%s", method.getScheme(), container.getName(), this.getHostname()));
    try {
        final Bucket configuration = session.getClient().buckets().get(container.getName()).execute();
        final Bucket.Website website = configuration.getWebsite();
        final Distribution distribution = new Distribution(
            origin, method, website != null);
        if(website != null) {
            distribution.setUrl(URI.create(String.format("%s://%s.%s", method.getScheme(), container.getName(), this.getHostname())));
            distribution.setStatus(LocaleFactory.localizedString("Deployed", "S3"));
            distribution.setIndexDocument(website.getMainPageSuffix());
        }
        final Bucket.Logging logging = configuration.getLogging();
        if(logging != null) {
            distribution.setLogging(logging.getLogObjectPrefix() != null);
            distribution.setLoggingContainer(logging.getLogBucket());
            distribution.setContainers(new GoogleStorageBucketListService(session).list(
                new Path(String.valueOf(Path.DELIMITER), EnumSet.of(Path.Type.volume, Path.Type.directory)), new DisabledListProgressListener()).toList());
        }
        return distribution;
    }
    catch(IOException e) {
        throw new GoogleStorageExceptionMappingService().map("Cannot read CDN configuration", e);
    }
}
 
Example #20
Source File: GcsStorageService.java    From front50 with Apache License 2.0 5 votes vote down vote up
/** Returns true if the storage service supports versioning. */
@Override
public boolean supportsVersioning() {
  try {
    Bucket bucket = storage.buckets().get(bucketName).execute();
    Bucket.Versioning v = bucket.getVersioning();
    return v != null && v.getEnabled();
  } catch (IOException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #21
Source File: GoogleCloudStorageImpl.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * See {@link GoogleCloudStorage#listBucketNames()} for details about expected behavior.
 */
@Override
public List<String> listBucketNames()
    throws IOException {
  logger.atFine().log("listBucketNames()");
  List<Bucket> allBuckets = listBucketsInternal();
  List<String> bucketNames = new ArrayList<>(allBuckets.size());
  for (Bucket bucket : allBuckets) {
    bucketNames.add(bucket.getName());
  }
  return bucketNames;
}
 
Example #22
Source File: StorageSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches the metadata for the given bucket.
 *
 * @param bucketName the name of the bucket to get metadata about.
 * @return a Bucket containing the bucket's metadata.
 */
public static Bucket getBucket(String bucketName) throws IOException, GeneralSecurityException {
  Storage client = StorageFactory.getService();

  Storage.Buckets.Get bucketRequest = client.buckets().get(bucketName);
  // Fetch the full set of the bucket's properties (e.g. include the ACLs in the response)
  bucketRequest.setProjection("full");
  return bucketRequest.execute();
}
 
Example #23
Source File: StorageHandler.java    From tech-gallery with Apache License 2.0 5 votes vote down vote up
/**
 * Method to get a bucket that already exists.
 *
 * @author <a href="mailto:[email protected]"> João Felipe de Medeiros Moreira </a>
 * @since 13/10/2015
 *
 * @param bucketName to be founded.
 *
 * @return the bucket founded.
 *
 * @throws IOException in case a IO problem.
 * @throws GeneralSecurityException in case a security problem.
 */
public static Bucket getExistingBucket(String bucketName)
    throws IOException, GeneralSecurityException {
  logger.finest("###### Searching a bucket");
  Storage client = getService();
  Storage.Buckets.Get bucketRequest = client.buckets().get(BUCKET_NAME + bucketName);
  // Fetch the full set of the bucket's properties (e.g. include the ACLs in the response)
  bucketRequest.setProjection(FULL);
  try {
    return bucketRequest.execute();
  } catch (Exception e) {
    // If the bucket doesn't exists, return null to create a new one.
    return null;
  }
}
 
Example #24
Source File: GoogleStorageBucketListService.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
    try {
        final AttributedList<Path> buckets = new AttributedList<Path>();
        Buckets response;
        String page = null;
        do {
            response = session.getClient().buckets().list(session.getHost().getCredentials().getUsername())
                .setMaxResults(preferences.getLong("googlestorage.listing.chunksize"))
                .setPageToken(page)
                .execute();
            if(null != response.getItems()) {
                for(Bucket item : response.getItems()) {
                    final Path bucket = new Path(PathNormalizer.normalize(item.getName()), EnumSet.of(Path.Type.volume, Path.Type.directory),
                        attributes.toAttributes(item)
                    );
                    buckets.add(bucket);
                    listener.chunk(directory, buckets);
                }
            }
            page = response.getNextPageToken();
        }
        while(page != null);
        return buckets;
    }
    catch(IOException e) {
        throw new GoogleStorageExceptionMappingService().map("Listing directory {0} failed", e, directory);
    }
}
 
Example #25
Source File: GoogleStorageLifecycleFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setConfiguration(final Path file, final LifecycleConfiguration configuration) throws BackgroundException {
    final Path container = containerService.getContainer(file);
    try {
        if(configuration.getTransition() != null || configuration.getExpiration() != null) {
            final Bucket.Lifecycle config = new Bucket.Lifecycle();
            // Unique identifier for the rule. The value cannot be longer than 255 characters. When you specify an empty prefix, the rule applies to all objects in the bucket
            final List<Bucket.Lifecycle.Rule> rules = new ArrayList<>();
            if(configuration.getTransition() != null) {
                rules.add(new Bucket.Lifecycle.Rule().setCondition(new Bucket.Lifecycle.Rule.Condition().setIsLive(true).setAge(configuration.getTransition()))
                    .setAction(new Bucket.Lifecycle.Rule.Action().setType("SetStorageClass").setStorageClass(configuration.getStorageClass())));
            }
            if(configuration.getExpiration() != null) {
                rules.add(new Bucket.Lifecycle.Rule().setCondition(new Bucket.Lifecycle.Rule.Condition().setIsLive(true).setAge(configuration.getExpiration()))
                    .setAction(new Bucket.Lifecycle.Rule.Action().setType("Delete")));
            }
            session.getClient().buckets().patch(container.getName(), new Bucket().setLifecycle(
                config.setRule(rules))).execute();
        }
        else {
            // Empty lifecycle configuration
            session.getClient().buckets().patch(container.getName(), new Bucket().setLifecycle(new Bucket.Lifecycle().setRule(Collections.emptyList()))).execute();
        }
    }
    catch(IOException e) {
        throw new GoogleStorageExceptionMappingService().map("Failure to write attributes of {0}", e, container);
    }
}
 
Example #26
Source File: GoogleStorageAttributesFinderFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
protected PathAttributes toAttributes(final Bucket bucket) {
    final PathAttributes attributes = new PathAttributes();
    attributes.setRegion(bucket.getLocation());
    attributes.setStorageClass(bucket.getStorageClass());
    attributes.setCreationDate(bucket.getTimeCreated().getValue());
    attributes.setETag(bucket.getEtag());
    if(bucket.getEncryption() != null) {
        attributes.setEncryption(new Encryption.Algorithm("AES256", bucket.getEncryption().getDefaultKmsKeyName()));
    }
    attributes.setRegion(bucket.getLocation());
    return attributes;
}
 
Example #27
Source File: GcsUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateBucketAccessErrors() 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.Insert mockStorageInsert = Mockito.mock(Storage.Buckets.Insert.class);

  BackOff mockBackOff = BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff());
  GoogleJsonResponseException expectedException =
      googleJsonResponseException(
          HttpStatusCodes.STATUS_CODE_FORBIDDEN,
          "Waves hand mysteriously",
          "These aren't the buckets you're looking for");

  when(mockStorage.buckets()).thenReturn(mockStorageObjects);
  when(mockStorageObjects.insert(any(String.class), any(Bucket.class)))
      .thenReturn(mockStorageInsert);
  when(mockStorageInsert.execute()).thenThrow(expectedException);

  thrown.expect(AccessDeniedException.class);

  gcsUtil.createBucket("a", new Bucket(), mockBackOff, new FastNanoClockAndSleeper());
}
 
Example #28
Source File: GcpOptionsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateBucketCreateBucketFails() throws Exception {
  doReturn(fakeProject).when(mockGet).execute();
  doThrow(new IOException("badness"))
      .when(mockGcsUtil)
      .createBucket(any(String.class), any(Bucket.class));

  thrown.expect(RuntimeException.class);
  thrown.expectMessage("Unable create default bucket");
  GcpTempLocationFactory.tryCreateDefaultBucket(options, mockCrmClient);
}
 
Example #29
Source File: GoogleStorageLoggingFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setConfiguration(final Path container, final LoggingConfiguration configuration) throws BackgroundException {
    try {
        session.getClient().buckets().patch(container.getName(),
            new Bucket().setLogging(new Bucket.Logging()
                .setLogObjectPrefix(configuration.isEnabled() ? PreferencesFactory.get().getProperty("google.logging.prefix") : null)
                .setLogBucket(StringUtils.isNotBlank(configuration.getLoggingTarget()) ? configuration.getLoggingTarget() : container.getName()))
        ).execute();
    }
    catch(IOException e) {
        throw new GoogleStorageExceptionMappingService().map("Failure to write attributes of {0}", e, container);
    }
}
 
Example #30
Source File: StorageResourceController.java    From pubsub with Apache License 2.0 5 votes vote down vote up
private void createStorageBucket() throws IOException {
  try {
    storage.buckets().insert(project, new Bucket().setName(bucketName(project))).execute();
  } catch (GoogleJsonResponseException e) {
    if (e.getStatusCode() != HttpStatus.SC_CONFLICT) {
      throw e;
    }
  }
}