如何使用java来设置“ACL”到谷歌存储文件夹下的所有文件

编程入门 行业动态 更新时间:2024-10-08 22:51:27
本文介绍了如何使用java来设置“ACL”到谷歌存储文件夹下的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过GCP将文件夹中的所有文件更改为公共共享。

我看到如何通过gsutils执行此操作。

我怎样才能通过java api来做到这一点?

这是我的尝试:

解决方案> 

).insert(),尝试使用ACL API

ObjectAccessControl oac = new ObjectAccessControl() oac.setEntity(allUsers) oac.setRole(READER); 插入insert = service.objectAccessControls()。insert(bucketName,sounds / 1.0 / arabic_test22 / 1000meters.mp3,oac); insert.execute();

关于文件夹的问题。在云存储中,文件夹的概念不存在,它只是桶和对象名称。 您可以看到文件夹内的文件(我正在谈论云存储浏览器)的事实,它只是一个图形表示。使用API​​,您将始终处理存储桶和对象名称。

了解了这一点,对象:列表提供了一个前缀参数,您可以使用它来过滤所有名称以它开头的对象。如果您认为您的对象名称作为文件夹的开头,则此过滤器可以实现您要查找的内容。

从API文档中我引用

结合前缀过滤器,使用分隔符参数可以使list方法像目录列表尽管对象命名空间是平坦的。例如,如果分隔符被设置为/,那么从包含对象a / b,a / c,d,e, e / f将返回对象d和e,并且前缀a /和e /\".

I want to change all files in folder over GCP to be publicly shared.

I see how to do this via gsutils.

How can i do this via java api?

Here is my try:

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(); } }

I get NPE because insertRequest.getMediaHttpUploader() == null

解决方案

Instead of using objects().insert(), try using the ACL API

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();

About the folder matter. In Cloud Storage the concept of "folder" does not exists, it is only "bucket" and "object name". The fact you can see the file grouped inside folders (I'm talking about the Cloud Storage Browser) it is only a graphic representation. With the API you will always handle "bucket" and "object name".

Knowing this, the Objects: list provides a prefix parameter which you can use to filter all the objects where the name starts with it. If you think the start of your object name as the folder, this filter can achieve what you're looking for.

From the documentation of the API I quote

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/".

更多推荐

如何使用java来设置“ACL”到谷歌存储文件夹下的所有文件

本文发布于:2023-11-28 10:57:51,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文件夹   文件   ACL   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!