Storage.Buckets.Insert insertBucket = storage.buckets().insert(projectName, newBucket);
@Override public Bucket create(Bucket bucket, Map<Option, ?> options) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_CREATE_BUCKET); Scope scope = tracer.withSpan(span); try { return storage .buckets() .insert(this.options.getProjectId(), bucket) .setProjection(DEFAULT_PROJECTION) .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) .setPredefinedDefaultObjectAcl(Option.PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) .execute(); } catch (IOException ex) { span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage())); throw translate(ex); } finally { scope.close(); span.end(); } }
public static Bucket createInProject(Storage storage, String project, Bucket bucket) throws IOException { try { Storage.Buckets.Insert insertBucket = storage.buckets().insert(project, bucket); return insertBucket.execute(); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); if (error != null && error.getCode() == HTTP_CONFLICT && error.getMessage().contains("You already own this bucket.")) { System.out.println("already exists"); return bucket; } System.err.println(error.getMessage()); throw e; } }
@VisibleForTesting void createBucket(String projectId, Bucket bucket, BackOff backoff, Sleeper sleeper) throws IOException { Storage.Buckets.Insert insertBucket = storageClient.buckets().insert(projectId, bucket); insertBucket.setPredefinedAcl("projectPrivate"); insertBucket.setPredefinedDefaultObjectAcl("projectPrivate");
/** * See {@link GoogleCloudStorage#create(String, CreateBucketOptions)} for details about expected * behavior. */ @Override public void create(String bucketName, CreateBucketOptions options) throws IOException { logger.atFine().log("create(%s)", bucketName); Preconditions.checkArgument( !Strings.isNullOrEmpty(bucketName), "bucketName must not be null or empty"); checkNotNull(options, "options must not be null"); checkNotNull(storageOptions.getProjectId(), "projectId must not be null"); Bucket bucket = new Bucket(); bucket.setName(bucketName); bucket.setLocation(options.getLocation()); bucket.setStorageClass(options.getStorageClass()); Storage.Buckets.Insert insertBucket = configureRequest(gcs.buckets().insert(storageOptions.getProjectId(), bucket), bucketName); // TODO(user): To match the behavior of throwing FileNotFoundException for 404, we probably // want to throw org.apache.commons.io.FileExistsException for 409 here. try { ResilientOperation.retry( ResilientOperation.getGoogleRequestCallable(insertBucket), backOffFactory.newBackOff(), rateLimitedRetryDeterminer, IOException.class, sleeper); } catch (InterruptedException e) { throw new IOException(e); // From sleep } }
@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()); }
@Override public Bucket create(Bucket bucket, Map<Option, ?> options) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_CREATE_BUCKET); Scope scope = tracer.withSpan(span); try { return storage .buckets() .insert(this.options.getProjectId(), bucket) .setProjection(DEFAULT_PROJECTION) .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) .setPredefinedDefaultObjectAcl(Option.PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) .execute(); } catch (IOException ex) { span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage())); throw translate(ex); } finally { scope.close(); span.end(); } }
@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()); }
/** * Creates a bucket * * @param bucketName * Name of bucket to create * @throws Exception */ public static void createBucket(String bucketName) throws Exception { Storage storage = getStorage(); Bucket bucket = new Bucket(); bucket.setName(bucketName); storage.buckets().insert( getProperties().getProperty(PROJECT_ID_PROPERTY), bucket).execute(); }
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); } }
@Override public Bucket create(Bucket bucket, Map<Option, ?> options) { try { return storage.buckets() .insert(this.options.projectId(), bucket) .setProjection(DEFAULT_PROJECTION) .setPredefinedAcl(PREDEFINED_ACL.getString(options)) .setPredefinedDefaultObjectAcl(PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) .execute(); } catch (IOException ex) { throw translate(ex); } }
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); } }
/** * Creates the storage bucket used by the load test. */ private void createStorageBucket() throws IOException { try { storage.buckets().insert(projectName, new Bucket() .setName(projectName + "-cloud-pubsub-loadtest")).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() != ALREADY_EXISTS) { throw e; } } }
storage.buckets().insert(projectName, bucket).execute(); } catch (IOException e2) { log.error("Could not create bucket {} in project {}: {}", bucketName, projectName, e2);