如何使用 java 将 "ACL" 设置为 google 存储文件夹下的所有文件
how to use java to set "ACL" to all files under google storage folder
我想将 GCP 文件夹中的所有文件更改为公开共享。
我了解如何通过 gsutils 执行此操作。
我如何通过 java api 执行此操作?
这是我的尝试:
public static void main(String[] args) throws Exception {
//// more setting up code here...
GoogleCredential credential = GoogleCredential.fromStream(credentialsStream, httpTransport, jsonFactory);
credential = credential.createScoped(StorageScopes.all());
final Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("monkeyduck")
.build();
final Storage.Objects.Get getRequest1 = storage.objects().get(bucketName, "sounds/1.0/arabic_test22/1000meters.mp3");
final StorageObject object1 = getRequest1.execute();
System.out.println(object1);
final List<ObjectAccessControl> aclList = new ArrayList<>();
// final ObjectAccessControl acl = new ObjectAccessControl()
// .setRole("PUBLIC-READER")
// .setProjectTeam(new ObjectAccessControl.ProjectTeam().setTeam("viewers"));
final ObjectAccessControl acl = new ObjectAccessControl()
.setRole("READER").setEntity("allUsers");
//System.out.println(acl);
aclList.add(acl);
object1.setAcl(aclList);
final Storage.Objects.Insert insertRequest = storage.objects().insert(bucketName, object1);
insertRequest.getMediaHttpUploader().setDirectUploadEnabled(true);
insertRequest.execute();
}
}
我得到 NPE 因为 insertRequest.getMediaHttpUploader() == null
尝试使用 ACL API
,而不是使用 objects().insert()
ObjectAccessControl oac = new ObjectAccessControl()
oac.setEntity("allUsers")
oac.setRole("READER");
Insert insert = service.objectAccessControls().insert(bucketName, "sounds/1.0/arabic_test22/1000meters.mp3", oac);
insert.execute();
关于文件夹问题。在云存储中不存在"folder"的概念,只有"bucket"和"object name"。
事实上,您可以看到文件在文件夹内分组(我说的是云存储浏览器),它只是一个图形表示。使用 API 您将始终处理 "bucket" 和 "object name".
了解这一点,Objects: list 提供了一个 prefix
参数,您可以使用该参数过滤名称以它开头的所有对象。如果您认为对象名称的开头是文件夹,此过滤器可以实现您要查找的内容。
来自 API 我引用的文档
In conjunction with the prefix filter, the use of the delimiter
parameter allows the list method to operate like a directory listing,
despite the object namespace being flat. For example, if delimiter
were set to "/", then listing objects from a bucket that contains the
objects "a/b", "a/c", "d", "e", "e/f" would return objects "d" and
"e", and prefixes "a/" and "e/".
我想将 GCP 文件夹中的所有文件更改为公开共享。
我了解如何通过 gsutils 执行此操作。
我如何通过 java api 执行此操作?
这是我的尝试:
public static void main(String[] args) throws Exception {
//// more setting up code here...
GoogleCredential credential = GoogleCredential.fromStream(credentialsStream, httpTransport, jsonFactory);
credential = credential.createScoped(StorageScopes.all());
final Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("monkeyduck")
.build();
final Storage.Objects.Get getRequest1 = storage.objects().get(bucketName, "sounds/1.0/arabic_test22/1000meters.mp3");
final StorageObject object1 = getRequest1.execute();
System.out.println(object1);
final List<ObjectAccessControl> aclList = new ArrayList<>();
// final ObjectAccessControl acl = new ObjectAccessControl()
// .setRole("PUBLIC-READER")
// .setProjectTeam(new ObjectAccessControl.ProjectTeam().setTeam("viewers"));
final ObjectAccessControl acl = new ObjectAccessControl()
.setRole("READER").setEntity("allUsers");
//System.out.println(acl);
aclList.add(acl);
object1.setAcl(aclList);
final Storage.Objects.Insert insertRequest = storage.objects().insert(bucketName, object1);
insertRequest.getMediaHttpUploader().setDirectUploadEnabled(true);
insertRequest.execute();
}
}
我得到 NPE 因为 insertRequest.getMediaHttpUploader() == null
尝试使用 ACL API
,而不是使用objects().insert()
ObjectAccessControl oac = new ObjectAccessControl()
oac.setEntity("allUsers")
oac.setRole("READER");
Insert insert = service.objectAccessControls().insert(bucketName, "sounds/1.0/arabic_test22/1000meters.mp3", oac);
insert.execute();
关于文件夹问题。在云存储中不存在"folder"的概念,只有"bucket"和"object name"。 事实上,您可以看到文件在文件夹内分组(我说的是云存储浏览器),它只是一个图形表示。使用 API 您将始终处理 "bucket" 和 "object name".
了解这一点,Objects: list 提供了一个 prefix
参数,您可以使用该参数过滤名称以它开头的所有对象。如果您认为对象名称的开头是文件夹,此过滤器可以实现您要查找的内容。
来自 API 我引用的文档
In conjunction with the prefix filter, the use of the delimiter parameter allows the list method to operate like a directory listing, despite the object namespace being flat. For example, if delimiter were set to "/", then listing objects from a bucket that contains the objects "a/b", "a/c", "d", "e", "e/f" would return objects "d" and "e", and prefixes "a/" and "e/".