This repository was archived by the owner on Feb 26, 2025. It is now read-only.
forked from martint/s3fs
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathS3FileSystem.java
More file actions
189 lines (160 loc) · 5.05 KB
/
S3FileSystem.java
File metadata and controls
189 lines (160 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.upplication.s3fs;
import static com.upplication.s3fs.S3Path.PATH_SEPARATOR;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.WatchService;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.Set;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.Bucket;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
/**
* S3FileSystem with a concrete client configured and ready to use.
*
* @see AmazonS3 configured by {@link AmazonS3Factory}
*/
public class S3FileSystem extends FileSystem implements Comparable<S3FileSystem> {
private final S3FileSystemProvider provider;
private final String key;
private final AmazonS3 client;
private final String endpoint;
private final String kmsKeyId;
private int cache;
public S3FileSystem(S3FileSystemProvider provider, String key, AmazonS3 client, String endpoint) {
this(provider, key, client, endpoint, null);
}
public S3FileSystem(S3FileSystemProvider provider, String key, AmazonS3 client, String endpoint, String kmsKeyId) {
this.provider = provider;
this.key = key;
this.client = client;
this.endpoint = endpoint;
this.cache = 60000; // 1 minute cache for the s3Path
this.kmsKeyId = kmsKeyId;
}
@Override
public S3FileSystemProvider provider() {
return provider;
}
public String getKey() {
return key;
}
@Override
public void close() throws IOException {
this.provider.close(this);
}
@Override
public boolean isOpen() {
return this.provider.isOpen(this);
}
@Override
public boolean isReadOnly() {
return false;
}
@Override
public String getSeparator() {
return S3Path.PATH_SEPARATOR;
}
@Override
public Iterable<Path> getRootDirectories() {
ImmutableList.Builder<Path> builder = ImmutableList.builder();
for (FileStore fileStore : getFileStores()) {
builder.add(((S3FileStore) fileStore).getRootDirectory());
}
return builder.build();
}
@Override
public Iterable<FileStore> getFileStores() {
ImmutableList.Builder<FileStore> builder = ImmutableList.builder();
for (Bucket bucket : client.listBuckets()) {
builder.add(new S3FileStore(this, bucket.getName()));
}
return builder.build();
}
@Override
public Set<String> supportedFileAttributeViews() {
return ImmutableSet.of("basic", "posix");
}
@Override
public S3Path getPath(String first, String... more) {
if (more.length == 0) {
return new S3Path(this, first);
}
return new S3Path(this, first, more);
}
@Override
public PathMatcher getPathMatcher(String syntaxAndPattern) {
throw new UnsupportedOperationException();
}
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
throw new UnsupportedOperationException();
}
@Override
public WatchService newWatchService() throws IOException {
throw new UnsupportedOperationException();
}
public AmazonS3 getClient() {
return client;
}
/**
* get the endpoint associated with this fileSystem.
*
* @return string
* @see <a href="http://docs.aws.amazon.com/general/latest/gr/rande.html">http://docs.aws.amazon.com/general/latest/gr/rande.html</a>
*/
public String getEndpoint() {
return endpoint;
}
public String[] key2Parts(String keyParts) {
String[] parts = keyParts.split(PATH_SEPARATOR);
String[] split = new String[parts.length];
int i = 0;
for (String part : parts) {
split[i++] = part;
}
return split;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((endpoint == null) ? 0 : endpoint.hashCode());
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof S3FileSystem))
return false;
S3FileSystem other = (S3FileSystem) obj;
if (endpoint == null) {
if (other.endpoint != null)
return false;
} else if (!endpoint.equals(other.endpoint))
return false;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return true;
}
@Override
public int compareTo(S3FileSystem o) {
return key.compareTo(o.getKey());
}
public int getCache() {
return cache;
}
public String getKmsKeyId() {
return kmsKeyId;
}
}