44import java .nio .file .FileStore ;
55import java .nio .file .attribute .FileAttributeView ;
66import java .nio .file .attribute .FileStoreAttributeView ;
7+ import java .util .Date ;
78
89import com .amazonaws .services .s3 .AmazonS3 ;
10+ import com .amazonaws .services .s3 .model .AccessControlList ;
11+ import com .amazonaws .services .s3 .model .AmazonS3Exception ;
912import com .amazonaws .services .s3 .model .Bucket ;
1013import com .amazonaws .services .s3 .model .Owner ;
11- import com .google .common .collect .ImmutableList ;
1214
1315public class S3FileStore extends FileStore implements Comparable <S3FileStore > {
1416
@@ -65,9 +67,18 @@ public boolean supportsFileAttributeView(String attributeViewName) {
6567 public <V extends FileStoreAttributeView > V getFileStoreAttributeView (Class <V > type ) {
6668 if (type != S3FileStoreAttributeView .class )
6769 throw new IllegalArgumentException ("FileStoreAttributeView of type '" + type .getName () + "' is not supported." );
68- Bucket buck = getBucket ();
69- Owner owner = buck .getOwner ();
70- return (V ) new S3FileStoreAttributeView (buck .getCreationDate (), buck .getName (), owner .getId (), owner .getDisplayName ());
70+ Date creationDate = null ;
71+ String bucketName = name ;
72+ try {
73+ Bucket bucket = getBucket ();
74+ creationDate = bucket .getCreationDate ();
75+ bucketName = bucket .getName ();
76+ } catch (@ SuppressWarnings ("unused" ) AmazonS3Exception e ) {
77+ // This is probably caused by not being authorized to call ListAll Buckets.
78+ // Lets return what we can get our hands on instead of crashing at this point.
79+ }
80+ Owner owner = getOwner ();
81+ return (V ) new S3FileStoreAttributeView (creationDate , bucketName , owner .getId (), owner .getDisplayName ());
7182 }
7283
7384 @ Override
@@ -83,6 +94,10 @@ public Bucket getBucket() {
8394 return getBucket (name );
8495 }
8596
97+ public boolean doesBucketExist (String bucketName ) {
98+ return getClient ().doesBucketExistV2 (bucketName );
99+ }
100+
86101 private Bucket getBucket (String bucketName ) {
87102 for (Bucket buck : getClient ().listBuckets ())
88103 if (buck .getName ().equals (bucketName ))
@@ -99,10 +114,14 @@ private AmazonS3 getClient() {
99114 }
100115
101116 public Owner getOwner () {
102- Bucket buck = getBucket ();
103- if (buck != null )
104- return buck .getOwner ();
105- return fileSystem .getClient ().getS3AccountOwner ();
117+ try {
118+ AccessControlList acl = getClient ().getBucketAcl (name );
119+ return acl .getOwner ();
120+ } catch (@ SuppressWarnings ("unused" ) AmazonS3Exception e ) {
121+ // Client might not be authorized to request this info?
122+ // User S3AccountOwner as fallback.
123+ }
124+ return fileSystem .getClient ().getS3AccountOwner ();
106125 }
107126
108127 @ Override
0 commit comments