4
4
import java .nio .file .FileStore ;
5
5
import java .nio .file .attribute .FileAttributeView ;
6
6
import java .nio .file .attribute .FileStoreAttributeView ;
7
+ import java .util .Date ;
7
8
8
9
import com .amazonaws .services .s3 .AmazonS3 ;
10
+ import com .amazonaws .services .s3 .model .AccessControlList ;
11
+ import com .amazonaws .services .s3 .model .AmazonS3Exception ;
9
12
import com .amazonaws .services .s3 .model .Bucket ;
10
13
import com .amazonaws .services .s3 .model .Owner ;
11
- import com .google .common .collect .ImmutableList ;
12
14
13
15
public class S3FileStore extends FileStore implements Comparable <S3FileStore > {
14
16
@@ -65,9 +67,18 @@ public boolean supportsFileAttributeView(String attributeViewName) {
65
67
public <V extends FileStoreAttributeView > V getFileStoreAttributeView (Class <V > type ) {
66
68
if (type != S3FileStoreAttributeView .class )
67
69
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 ());
71
82
}
72
83
73
84
@ Override
@@ -83,6 +94,10 @@ public Bucket getBucket() {
83
94
return getBucket (name );
84
95
}
85
96
97
+ public boolean doesBucketExist (String bucketName ) {
98
+ return getClient ().doesBucketExistV2 (bucketName );
99
+ }
100
+
86
101
private Bucket getBucket (String bucketName ) {
87
102
for (Bucket buck : getClient ().listBuckets ())
88
103
if (buck .getName ().equals (bucketName ))
@@ -99,10 +114,14 @@ private AmazonS3 getClient() {
99
114
}
100
115
101
116
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 ();
106
125
}
107
126
108
127
@ Override
0 commit comments