@Test public void testWithAcl() throws IOException { Path path = Paths.get(URI.create("gs://bucket/path")); Acl acl = Acl.of(new Acl.User("king@example.com"), Acl.Role.OWNER); Files.write(path, "(✿◕ ‿◕ )ノ".getBytes(UTF_8), CloudStorageOptions.withAcl(acl)); assertThat(Files.readAttributes(path, CloudStorageFileAttributes.class).acl().get()) .contains(acl); }
/** Example of creating a new ACL entry on a bucket. */ // [TARGET createAcl(String, Acl)] // [VARIABLE "my_unique_bucket"] public Acl createBucketAcl(String bucketName) { // [START createBucketAcl] Acl acl = storage.createAcl(bucketName, Acl.of(User.ofAllAuthenticatedUsers(), Role.READER)); // [END createBucketAcl] return acl; }
static Entity fromPb(String entity) { if (entity.startsWith("user-")) { return new User(entity.substring(5)); } if (entity.equals(User.ALL_USERS)) { return User.ofAllUsers(); } if (entity.equals(User.ALL_AUTHENTICATED_USERS)) { return User.ofAllAuthenticatedUsers(); } if (entity.startsWith("group-")) { return new Group(entity.substring(6)); } if (entity.startsWith("domain-")) { return new Domain(entity.substring(7)); } if (entity.startsWith("project-")) { int idx = entity.indexOf('-', 8); String team = entity.substring(8, idx); String projectId = entity.substring(idx + 1); return new Project(Project.ProjectRole.valueOf(team.toUpperCase()), projectId); } return new RawEntity(entity); } }
private void testBucketAclRequesterPays(boolean requesterPays) { if (requesterPays) { Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID)); assertNull(remoteBucket.requesterPays()); remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build(); Bucket updatedBucket = storage.update(remoteBucket); assertTrue(updatedBucket.requesterPays()); } String projectId = remoteStorageHelper.getOptions().getProjectId(); Storage.BucketSourceOption[] bucketOptions = requesterPays ? new Storage.BucketSourceOption[] {Storage.BucketSourceOption.userProject(projectId)} : new Storage.BucketSourceOption[] {}; assertNull(storage.getAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions)); assertFalse(storage.deleteAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions)); Acl acl = Acl.of(User.ofAllAuthenticatedUsers(), Role.READER); assertNotNull(storage.createAcl(BUCKET, acl, bucketOptions)); Acl updatedAcl = storage.updateAcl(BUCKET, acl.toBuilder().setRole(Role.WRITER).build(), bucketOptions); assertEquals(Role.WRITER, updatedAcl.getRole()); Set<Acl> acls = new HashSet<>(); acls.addAll(storage.listAcls(BUCKET, bucketOptions)); assertTrue(acls.contains(updatedAcl)); assertTrue(storage.deleteAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions)); assertNull(storage.getAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions)); }
BlobId blobId = BlobId.of(BUCKET, "test-blob-acl"); BlobInfo blob = BlobInfo.newBuilder(blobId).build(); storage.create(blob); assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers())); Acl acl = Acl.of(User.ofAllAuthenticatedUsers(), Role.READER); assertNotNull(storage.createAcl(blobId, acl)); Acl updatedAcl = storage.updateAcl(blobId, acl.toBuilder().setRole(Role.OWNER).build()); assertEquals(Role.OWNER, updatedAcl.getRole()); Set<Acl> acls = new HashSet<>(storage.listAcls(blobId)); assertTrue(acls.contains(updatedAcl)); assertTrue(storage.deleteAcl(blobId, User.ofAllAuthenticatedUsers())); assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers())); assertNull(storage.getAcl(otherBlobId, User.ofAllAuthenticatedUsers())); fail("Expected an 'Invalid argument' exception"); } catch (StorageException e) { assertFalse(storage.deleteAcl(otherBlobId, User.ofAllAuthenticatedUsers())); fail("Expected an 'Invalid argument' exception"); } catch (StorageException e) {
@Test public void testCopyBlobWithPredefinedAcl() { String sourceBlobName = "test-copy-blob-source"; BlobId source = BlobId.of(BUCKET, sourceBlobName); ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v"); BlobInfo blob = BlobInfo.newBuilder(source).setContentType(CONTENT_TYPE).setMetadata(metadata).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); String targetBlobName = "test-copy-blob-target"; Storage.CopyRequest req = Storage.CopyRequest.newBuilder() .setSource(source) .setTarget( BlobId.of(BUCKET, targetBlobName), Storage.BlobTargetOption.predefinedAcl(Storage.PredefinedAcl.PUBLIC_READ)) .build(); CopyWriter copyWriter = storage.copy(req); assertEquals(BUCKET, copyWriter.getResult().getBucket()); assertEquals(targetBlobName, copyWriter.getResult().getName()); assertEquals(CONTENT_TYPE, copyWriter.getResult().getContentType()); assertEquals(metadata, copyWriter.getResult().getMetadata()); assertNotNull(copyWriter.getResult().getAcl(User.ofAllUsers())); assertTrue(copyWriter.isDone()); assertTrue(remoteBlob.delete()); assertTrue(storage.delete(BUCKET, targetBlobName)); }
final Blob blob = storage.get(blobId); final ReadChannel reader = storage.reader(blobId, blobSourceOptions.toArray(new Storage.BlobSourceOption[blobSourceOptions.size()])); attributes.put(OWNER_ATTR, ((Acl.User) entity).getEmail()); attributes.put(OWNER_TYPE_ATTR, "user"); } else if (entity instanceof Acl.Group) {
@Test public void testBucketDefaultAcl() { assertNull(storage.getDefaultAcl(BUCKET, User.ofAllAuthenticatedUsers())); assertFalse(storage.deleteDefaultAcl(BUCKET, User.ofAllAuthenticatedUsers())); Acl acl = Acl.of(User.ofAllAuthenticatedUsers(), Role.READER); assertNotNull(storage.createDefaultAcl(BUCKET, acl)); Acl updatedAcl = storage.updateDefaultAcl(BUCKET, acl.toBuilder().setRole(Role.OWNER).build()); assertEquals(Role.OWNER, updatedAcl.getRole()); Set<Acl> acls = new HashSet<>(); acls.addAll(storage.listDefaultAcls(BUCKET)); assertTrue(acls.contains(updatedAcl)); assertTrue(storage.deleteDefaultAcl(BUCKET, User.ofAllAuthenticatedUsers())); assertNull(storage.getDefaultAcl(BUCKET, User.ofAllAuthenticatedUsers())); }
@Test public void testOf() { Acl acl = Acl.of(User.ofAllUsers(), Role.READER); assertEquals(User.ofAllUsers(), acl.getEntity()); assertEquals(Role.READER, acl.getRole()); ObjectAccessControl objectPb = acl.toObjectPb(); assertEquals(acl, Acl.fromPb(objectPb)); BucketAccessControl bucketPb = acl.toBucketPb(); assertEquals(acl, Acl.fromPb(bucketPb)); } }
Page<Blob> blobPages = storage.list(bucket, listOptions.toArray(new Storage.BlobListOption[listOptions.size()])); do { for (Blob blob : blobPages.getValues()) { attributes.put(OWNER_ATTR, ((Acl.User) entity).getEmail()); attributes.put(OWNER_TYPE_ATTR, "user"); } else if (entity instanceof Acl.Group) {
@Test public void testGetAcl() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.getAcl(BLOB_INFO.getBlobId(), User.ofAllAuthenticatedUsers())).andReturn(ACL); replay(storage); initializeBlob(); assertEquals(ACL, blob.getAcl(User.ofAllAuthenticatedUsers())); }
@Test public void testDeleteAcl() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.deleteAcl(BUCKET_INFO.getName(), User.ofAllAuthenticatedUsers())) .andReturn(true); replay(storage); initializeBucket(); assertTrue(bucket.deleteAcl(User.ofAllAuthenticatedUsers())); }
@Test public void testToBuilder() { assertEquals(ACL, ACL.toBuilder().build()); Acl acl = ACL.toBuilder() .setEtag("otherEtag") .setId("otherId") .setRole(Role.READER) .setEntity(User.ofAllUsers()) .build(); assertEquals(Role.READER, acl.getRole()); assertEquals(User.ofAllUsers(), acl.getEntity()); assertEquals("otherEtag", acl.getEtag()); assertEquals("otherId", acl.getId()); }
/** Example of updating a new ACL entry on a bucket. */ // [TARGET updateAcl(String, Acl)] // [VARIABLE "my_unique_bucket"] public Acl updateBucketAcl(String bucketName) { // [START updateBucketAcl] Acl acl = storage.updateAcl(bucketName, Acl.of(User.ofAllAuthenticatedUsers(), Role.OWNER)); // [END updateBucketAcl] return acl; }
/** Example of updating a new default ACL entry on a bucket. */ // [TARGET updateDefaultAcl(String, Acl)] // [VARIABLE "my_unique_bucket"] public Acl updateDefaultBucketAcl(String bucketName) { // [START updateDefaultBucketAcl] Acl acl = storage.updateDefaultAcl(bucketName, Acl.of(User.ofAllAuthenticatedUsers(), Role.OWNER)); // [END updateDefaultBucketAcl] return acl; }
/** Example of creating a new default ACL entry on a bucket. */ // [TARGET createDefaultAcl(String, Acl)] // [VARIABLE "my_unique_bucket"] public Acl createDefaultBucketAcl(String bucketName) { // [START createDefaultBucketAcl] Acl acl = storage.createDefaultAcl(bucketName, Acl.of(User.ofAllAuthenticatedUsers(), Role.READER)); // [END createDefaultBucketAcl] return acl; }
@Test public void testDeleteDefaultAcl() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.deleteDefaultAcl(BUCKET_INFO.getName(), User.ofAllAuthenticatedUsers())) .andReturn(true); replay(storage); initializeBucket(); assertTrue(bucket.deleteDefaultAcl(User.ofAllAuthenticatedUsers())); }
@Test public void testGetDefaultAcl() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.getDefaultAcl(BUCKET_INFO.getName(), User.ofAllAuthenticatedUsers())) .andReturn(ACL); replay(storage); initializeBucket(); assertEquals(ACL, bucket.getDefaultAcl(User.ofAllAuthenticatedUsers())); }
/** Example of getting the ACL entry for a specific user on a bucket. */ // [TARGET getAcl(String, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "google-cloud-java-tests@java-docs-samples-tests.iam.gserviceaccount.com"] public Acl getBucketAcl(String bucketName, String userEmail) { // [START storagePrintBucketAclForUser] Acl acl = storage.getAcl(bucketName, new User(userEmail)); // [END storagePrintBucketAclForUser] return acl; }