Skip to content

Commit 4d5a86c

Browse files
authored
Merge pull request #1228 from mziccard/storage-acls
Add support for create/get/update/delete/list ACLs for blob and bucket
2 parents c929ee1 + 5376eff commit 4d5a86c

File tree

21 files changed

+2315
-54
lines changed

21 files changed

+2315
-54
lines changed

google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/FakeStorageRpc.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.google.cloud.storage.contrib.nio;
1818

1919
import com.google.api.services.storage.model.Bucket;
20+
import com.google.api.services.storage.model.BucketAccessControl;
21+
import com.google.api.services.storage.model.ObjectAccessControl;
2022
import com.google.api.services.storage.model.StorageObject;
2123
import com.google.cloud.storage.Storage;
2224
import com.google.cloud.storage.StorageException;
@@ -336,6 +338,81 @@ public RewriteResponse continueRewrite(RewriteResponse previousResponse) throws
336338
throw new UnsupportedOperationException();
337339
}
338340

341+
@Override
342+
public BucketAccessControl getAcl(String bucket, String entity) {
343+
throw new UnsupportedOperationException();
344+
}
345+
346+
@Override
347+
public boolean deleteAcl(String bucket, String entity) {
348+
throw new UnsupportedOperationException();
349+
}
350+
351+
@Override
352+
public BucketAccessControl createAcl(BucketAccessControl acl) {
353+
throw new UnsupportedOperationException();
354+
}
355+
356+
@Override
357+
public BucketAccessControl patchAcl(BucketAccessControl acl) {
358+
throw new UnsupportedOperationException();
359+
}
360+
361+
@Override
362+
public List<BucketAccessControl> listAcls(String bucket) {
363+
throw new UnsupportedOperationException();
364+
}
365+
366+
@Override
367+
public ObjectAccessControl getDefaultAcl(String bucket, String entity) {
368+
throw new UnsupportedOperationException();
369+
}
370+
371+
@Override
372+
public boolean deleteDefaultAcl(String bucket, String entity) {
373+
throw new UnsupportedOperationException();
374+
}
375+
376+
@Override
377+
public ObjectAccessControl createDefaultAcl(ObjectAccessControl acl) {
378+
throw new UnsupportedOperationException();
379+
}
380+
381+
@Override
382+
public ObjectAccessControl patchDefaultAcl(ObjectAccessControl acl) {
383+
throw new UnsupportedOperationException();
384+
}
385+
386+
@Override
387+
public List<ObjectAccessControl> listDefaultAcls(String bucket) {
388+
throw new UnsupportedOperationException();
389+
}
390+
391+
@Override
392+
public ObjectAccessControl getAcl(String bucket, String object, Long generation, String entity) {
393+
throw new UnsupportedOperationException();
394+
}
395+
396+
@Override
397+
public boolean deleteAcl(String bucket, String object, Long generation, String entity) {
398+
throw new UnsupportedOperationException();
399+
}
400+
401+
@Override
402+
public ObjectAccessControl createAcl(ObjectAccessControl acl) {
403+
throw new UnsupportedOperationException();
404+
}
405+
406+
@Override
407+
public ObjectAccessControl patchAcl(ObjectAccessControl acl) {
408+
throw new UnsupportedOperationException();
409+
}
410+
411+
@Override
412+
public List<ObjectAccessControl> listAcls(String bucket, String object, Long generation) {
413+
throw new UnsupportedOperationException();
414+
}
415+
339416
private String fullname(StorageObject so) {
340417
return (so.getBucket() + "/" + so.getName());
341418
}

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/StorageExample.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -582,24 +582,18 @@ public void run(Storage storage, Tuple<BlobId, Acl> params) {
582582
System.out.printf("Bucket %s does not exist%n", blobId.bucket());
583583
return;
584584
}
585-
bucket.toBuilder().acl(addAcl(bucket.acl(), acl)).build().update();
585+
acl = bucket.createAcl(acl);
586586
System.out.printf("Added ACL %s to bucket %s%n", acl, blobId.bucket());
587587
} else {
588588
Blob blob = storage.get(blobId);
589589
if (blob == null) {
590590
System.out.printf("Blob %s does not exist%n", blobId);
591591
return;
592592
}
593-
blob.toBuilder().acl(addAcl(blob.acl(), acl)).build().update();
593+
acl = blob.createAcl(acl);
594594
System.out.printf("Added ACL %s to blob %s%n", acl, blobId);
595595
}
596596
}
597-
598-
private static List<Acl> addAcl(List<Acl> acls, Acl newAcl) {
599-
List<Acl> newAcls = new LinkedList<>(acls);
600-
newAcls.add(newAcl);
601-
return newAcls;
602-
}
603597
}
604598

605599
/**

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import com.google.cloud.ReadChannel;
2929
import com.google.cloud.ServiceAccountSigner;
3030
import com.google.cloud.WriteChannel;
31+
import com.google.cloud.storage.Acl;
32+
import com.google.cloud.storage.Acl.User;
3133
import com.google.cloud.storage.Blob;
3234
import com.google.cloud.storage.Blob.BlobSourceOption;
3335
import com.google.cloud.storage.BlobId;
@@ -40,6 +42,7 @@
4042
import java.net.URL;
4143
import java.nio.ByteBuffer;
4244
import java.util.HashMap;
45+
import java.util.List;
4346
import java.util.Map;
4447
import java.util.concurrent.TimeUnit;
4548

@@ -228,4 +231,67 @@ public URL signUrlWithSigner(String keyPath) throws IOException {
228231
// [END signUrlWithSigner]
229232
return signedUrl;
230233
}
234+
235+
/**
236+
* Example of getting the ACL entry for an entity.
237+
*/
238+
// [TARGET getAcl(Entity)]
239+
public Acl getAcl() {
240+
// [START getAcl]
241+
Acl acl = blob.getAcl(User.ofAllAuthenticatedUsers());
242+
// [END getAcl]
243+
return acl;
244+
}
245+
246+
/**
247+
* Example of deleting the ACL entry for an entity.
248+
*/
249+
// [TARGET deleteAcl(Entity)]
250+
public boolean deleteAcl() {
251+
// [START deleteAcl]
252+
boolean deleted = blob.deleteAcl(User.ofAllAuthenticatedUsers());
253+
if (deleted) {
254+
// the acl entry was deleted
255+
} else {
256+
// the acl entry was not found
257+
}
258+
// [END deleteAcl]
259+
return deleted;
260+
}
261+
262+
/**
263+
* Example of creating a new ACL entry.
264+
*/
265+
// [TARGET createAcl(Acl)]
266+
public Acl createAcl() {
267+
// [START createAcl]
268+
Acl acl = blob.createAcl(Acl.of(User.ofAllAuthenticatedUsers(), Acl.Role.READER));
269+
// [END createAcl]
270+
return acl;
271+
}
272+
273+
/**
274+
* Example of updating a new ACL entry.
275+
*/
276+
// [TARGET updateAcl(Acl)]
277+
public Acl updateAcl() {
278+
// [START updateAcl]
279+
Acl acl = blob.updateAcl(Acl.of(User.ofAllAuthenticatedUsers(), Acl.Role.OWNER));
280+
// [END updateAcl]
281+
return acl;
282+
}
283+
284+
/**
285+
* Example of listing the ACL entries.
286+
*/
287+
// [TARGET listAcls()]
288+
public List<Acl> listAcls() {
289+
// [START listAcls]
290+
List<Acl> acls = blob.listAcls();
291+
for (Acl acl : acls) {
292+
// do something with ACL entry
293+
}
294+
// [END listAcls]
295+
return acls;
296+
}
231297
}

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/BucketSnippets.java

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import static java.nio.charset.StandardCharsets.UTF_8;
2626

2727
import com.google.cloud.Page;
28+
import com.google.cloud.storage.Acl;
29+
import com.google.cloud.storage.Acl.User;
2830
import com.google.cloud.storage.Blob;
2931
import com.google.cloud.storage.Bucket;
3032
import com.google.cloud.storage.Bucket.BucketSourceOption;
@@ -124,8 +126,8 @@ public Page<Blob> listBlobs() {
124126
}
125127

126128
/**
127-
* Example of getting a blob in the bucket, only if its metageneration matches a value, otherwise
128-
* a {@link StorageException} is thrown.
129+
* Example of getting a blob in the bucket, only if its metageneration matches a value,
130+
* otherwise a {@link StorageException} is thrown.
129131
*/
130132
// [TARGET get(String, BlobGetOption...)]
131133
// [VARIABLE "my_blob_name"]
@@ -226,4 +228,130 @@ public Blob createBlobFromInputStreamWithContentType(String blobName) {
226228
// [END createBlobFromInputStreamWithContentType]
227229
return blob;
228230
}
231+
232+
/**
233+
* Example of getting the ACL entry for an entity.
234+
*/
235+
// [TARGET getAcl(Entity)]
236+
public Acl getAcl() {
237+
// [START getAcl]
238+
Acl acl = bucket.getAcl(User.ofAllAuthenticatedUsers());
239+
// [END getAcl]
240+
return acl;
241+
}
242+
243+
/**
244+
* Example of deleting the ACL entry for an entity.
245+
*/
246+
// [TARGET deleteAcl(Entity)]
247+
public boolean deleteAcl() {
248+
// [START deleteAcl]
249+
boolean deleted = bucket.deleteAcl(User.ofAllAuthenticatedUsers());
250+
if (deleted) {
251+
// the acl entry was deleted
252+
} else {
253+
// the acl entry was not found
254+
}
255+
// [END deleteAcl]
256+
return deleted;
257+
}
258+
259+
/**
260+
* Example of creating a new ACL entry.
261+
*/
262+
// [TARGET createAcl(Acl)]
263+
public Acl createAcl() {
264+
// [START createAcl]
265+
Acl acl = bucket.createAcl(Acl.of(User.ofAllAuthenticatedUsers(), Acl.Role.READER));
266+
// [END createAcl]
267+
return acl;
268+
}
269+
270+
/**
271+
* Example of updating a new ACL entry.
272+
*/
273+
// [TARGET updateAcl(Acl)]
274+
public Acl updateAcl() {
275+
// [START updateAcl]
276+
Acl acl = bucket.updateAcl(Acl.of(User.ofAllAuthenticatedUsers(), Acl.Role.OWNER));
277+
// [END updateAcl]
278+
return acl;
279+
}
280+
281+
/**
282+
* Example of listing the ACL entries.
283+
*/
284+
// [TARGET listAcls()]
285+
public List<Acl> listAcls() {
286+
// [START listAcls]
287+
List<Acl> acls = bucket.listAcls();
288+
for (Acl acl : acls) {
289+
// do something with ACL entry
290+
}
291+
// [END listAcls]
292+
return acls;
293+
}
294+
295+
/**
296+
* Example of getting the default ACL entry for an entity.
297+
*/
298+
// [TARGET getDefaultAcl(Entity)]
299+
public Acl getDefaultAcl() {
300+
// [START getDefaultAcl]
301+
Acl acl = bucket.getDefaultAcl(User.ofAllAuthenticatedUsers());
302+
// [END getDefaultAcl]
303+
return acl;
304+
}
305+
306+
/**
307+
* Example of deleting the default ACL entry for an entity.
308+
*/
309+
// [TARGET deleteDefaultAcl(Entity)]
310+
public boolean deleteDefaultAcl() {
311+
// [START deleteDefaultAcl]
312+
boolean deleted = bucket.deleteDefaultAcl(User.ofAllAuthenticatedUsers());
313+
if (deleted) {
314+
// the acl entry was deleted
315+
} else {
316+
// the acl entry was not found
317+
}
318+
// [END deleteDefaultAcl]
319+
return deleted;
320+
}
321+
322+
/**
323+
* Example of creating a new default ACL entry.
324+
*/
325+
// [TARGET createDefaultAcl(Acl)]
326+
public Acl createDefaultAcl() {
327+
// [START createDefaultAcl]
328+
Acl acl = bucket.createDefaultAcl(Acl.of(User.ofAllAuthenticatedUsers(), Acl.Role.READER));
329+
// [END createDefaultAcl]
330+
return acl;
331+
}
332+
333+
/**
334+
* Example of updating a new default ACL entry.
335+
*/
336+
// [TARGET updateDefaultAcl(Acl)]
337+
public Acl updateDefaultAcl() {
338+
// [START updateDefaultAcl]
339+
Acl acl = bucket.updateDefaultAcl(Acl.of(User.ofAllAuthenticatedUsers(), Acl.Role.OWNER));
340+
// [END updateDefaultAcl]
341+
return acl;
342+
}
343+
344+
/**
345+
* Example of listing the default ACL entries.
346+
*/
347+
// [TARGET listDefaultAcls()]
348+
public List<Acl> listDefaultAcls() {
349+
// [START listDefaultAcls]
350+
List<Acl> acls = bucket.listDefaultAcls();
351+
for (Acl acl : acls) {
352+
// do something with ACL entry
353+
}
354+
// [END listDefaultAcls]
355+
return acls;
356+
}
229357
}

0 commit comments

Comments
 (0)