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); } }
@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); }
@Test public void testAcl() throws IOException { Acl acl = Acl.of(new Acl.User("serf@example.com"), Acl.Role.READER); Files.write(path, HAPPY, CloudStorageOptions.withAcl(acl)); assertThat(Files.readAttributes(path, CloudStorageFileAttributes.class).acl().get()) .contains(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); } }
public static User ofAllUsers() { return new User(ALL_USERS); }
public static User ofAllAuthenticatedUsers() { return new User(ALL_AUTHENTICATED_USERS); } }
@Override Tuple<BlobId, Acl> parse(String... args) { if (args.length >= 3) { BlobId blob; int nextArg; if (args.length == 3) { blob = BlobId.of(args[0], ""); nextArg = 1; } else if (args.length == 4) { blob = BlobId.of(args[0], args[1]); nextArg = 2; } else { throw new IllegalArgumentException("Too many arguments."); } String user = args[nextArg++]; Acl.Role role = Acl.Role.valueOf(args[nextArg]); return Tuple.of(blob, Acl.of(new Acl.User(user), role)); } throw new IllegalArgumentException("Missing required bucket, userEmail or role arguments."); }
public static User ofAllUsers() { return new User(ALL_USERS); }
public static User ofAllAuthenticatedUsers() { return new User(ALL_AUTHENTICATED_USERS); } }
/** 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; }
@Test public void testUserEntity() { User acl = new User("u1"); assertEquals("u1", acl.getEmail()); assertEquals(Type.USER, acl.getType()); String pb = acl.toPb(); assertEquals(acl, Entity.fromPb(pb)); }
/** Example of getting the ACL entry for a specific user on a blob. */ // [TARGET getAcl(BlobId, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "google-cloud-java-tests@java-docs-samples-tests.iam.gserviceaccount.com"] public Acl getBlobAcl(String bucketName, String blobName, String userEmail) { // [START storagePrintFileAclForUser] BlobId blobId = BlobId.of(bucketName, blobName); Acl acl = storage.getAcl(blobId, new User(userEmail)); // [END storagePrintFileAclForUser] return acl; }