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 pathS3Path.java
More file actions
627 lines (507 loc) · 18.4 KB
/
S3Path.java
File metadata and controls
627 lines (507 loc) · 18.4 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package com.upplication.s3fs;
import static com.google.common.collect.Iterables.concat;
import static java.lang.String.format;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Iterator;
import java.util.List;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.upplication.s3fs.attribute.S3BasicFileAttributes;
public class S3Path implements Path {
public static final String PATH_SEPARATOR = "/";
/**
* S3FileStore which represents the Bucket this path resides in.
*/
private final S3FileStore fileStore;
/**
* URI not encoded
* Is the key for AmazonS3
*/
private String uri;
/**
* actual filesystem
*/
private S3FileSystem fileSystem;
/**
* S3BasicFileAttributes cache
*/
private S3BasicFileAttributes fileAttributes;
/**
* Build an S3Path from path segments. '/' are stripped from each segment.
*
* @param fileSystem S3FileSystem
* @param first should be start with a '/' and is the bucket name
* @param more directories and files
*/
public S3Path(S3FileSystem fileSystem, String first, String... more) {
Preconditions.checkArgument(first != null, "first path must be not null");
Preconditions.checkArgument(!first.startsWith("//"), "first path doesnt start with '//'. Miss bucket");
// see tests com.upplication.s3fs.Path.EndsWithTest#endsWithRelativeBlankAbsolute()
// Preconditions.checkArgument(!first.isEmpty(), "first path must be not empty");
boolean hasBucket = first.startsWith("/");
List<String> pathsURI = Lists
.newArrayList(Splitter.on(PATH_SEPARATOR)
.omitEmptyStrings()
.split(first));
if (hasBucket) { // absolute path
Preconditions.checkArgument(pathsURI.size() >= 1, "path must start with bucket name");
Preconditions.checkArgument(!pathsURI.get(0).isEmpty(), "bucket name must be not empty");
String bucket = pathsURI.get(0);
this.fileStore = new S3FileStore(fileSystem, bucket);
// the filestore is not part of the uri
pathsURI.remove(0);
}
else {
// relative uri
this.fileStore = null;
}
StringBuilder uriBuilder = new StringBuilder();
if (hasBucket) {
uriBuilder.append(PATH_SEPARATOR);
}
for (String path : pathsURI) {
uriBuilder.append(path + PATH_SEPARATOR);
}
if (more != null) {
for (String path : more) {
uriBuilder.append(path + PATH_SEPARATOR);
}
}
this.uri = normalizeURI(uriBuilder.toString());
// remove last PATH_SEPARATOR
if (!first.isEmpty() &&
// only first param and not ended with PATH_SEPARATOR
((!first.endsWith(PATH_SEPARATOR) && (more == null || more.length == 0))
// we have more param and not ended with PATH_SEPARATOR
|| more != null && more.length > 0 && !more[more.length-1].endsWith(PATH_SEPARATOR))) {
this.uri = this.uri.substring(0, this.uri.length() - 1);
}
this.fileSystem = fileSystem;
}
/**
* Remove duplicated slash
*/
private String normalizeURI(String uri) {
return uri.replace("//", "/");
}
public S3FileStore getFileStore() {
return fileStore;
}
/**
* key for amazon without final slash.
* <b>note:</b> the final slash need to be added to save a directory (Amazon s3 spec)
*
* @return the key for AmazonS3Client
*/
public String getKey() {
String key = this.uri;
if (key.startsWith("/")) {
key = key.substring(1, key.length());
}
return key;
}
@Override
public S3FileSystem getFileSystem() {
return this.fileSystem;
}
@Override
public boolean isAbsolute() {
return fileStore != null;
}
@Override
public Path getRoot() {
if (isAbsolute()) {
return new S3Path(fileSystem, PATH_SEPARATOR + fileStore.name() + PATH_SEPARATOR);
}
return null;
}
@Override
public Path getFileName() {
List<String> paths = uriToList();
if (paths.isEmpty()) {
// get FileName of root directory is null
return null;
}
String filename = paths.get(paths.size()-1);
return new S3Path(fileSystem, filename);
}
@Override
public Path getParent() {
// bucket is not present in the parts
if (uri.isEmpty()) {
return null;
}
String newUri = this.uri;
if (this.uri.endsWith("/")) {
newUri = this.uri.substring(0, this.uri.length()-1);
}
int lastPathSeparatorPosition = newUri.lastIndexOf(PATH_SEPARATOR);
if (lastPathSeparatorPosition == -1) {
return null;
}
newUri = uri.substring(0, lastPathSeparatorPosition + 1);
if (newUri.isEmpty())
return null;
String filestore = isAbsolute() ? PATH_SEPARATOR + fileStore.name() + PATH_SEPARATOR : "";
return new S3Path(fileSystem, filestore + newUri);
}
@Override
public int getNameCount() {
return uriToList().size();
}
@Override
public Path getName(int index) {
List<String> paths = uriToList();
if (index < 0 || index >= paths.size()) {
throw new IllegalArgumentException("index out of range");
}
String path = paths.get(index);
StringBuilder pathsBuilder = new StringBuilder();
if (isAbsolute() && index == 0) {
pathsBuilder.append(PATH_SEPARATOR + fileStore.name() + PATH_SEPARATOR);
}
pathsBuilder.append(path);
if (index < paths.size() - 1) {
pathsBuilder.append(PATH_SEPARATOR);
}
// if is the last path, check if end with path separator
if (index == paths.size() - 1 && this.uri.endsWith(PATH_SEPARATOR)) {
pathsBuilder.append(PATH_SEPARATOR);
}
return new S3Path(fileSystem, pathsBuilder.toString());
}
private List<String> uriToList() {
return Splitter.on(PATH_SEPARATOR).omitEmptyStrings().splitToList(this.uri);
}
/**
* The bucket name not count
*/
@Override
public Path subpath(int beginIndex, int endIndex) {
List<String> paths = uriToList();
if (beginIndex < 0 || endIndex > paths.size()) {
throw new IllegalArgumentException("index out of range");
}
List<String> pathSubList = paths.subList(beginIndex, endIndex);
StringBuilder pathsStringBuilder = new StringBuilder();
// build path string
if (this.isAbsolute() && beginIndex == 0) {
pathsStringBuilder.append(PATH_SEPARATOR + fileStore.name() + PATH_SEPARATOR);
}
for (String path : pathSubList) {
pathsStringBuilder.append(path).append(PATH_SEPARATOR);
}
String pathsResult = pathsStringBuilder.toString();
// if the uri doesnt have last PATH_SEPARATOR we must remove it.
if (endIndex == paths.size() && !this.uri.endsWith(PATH_SEPARATOR)) {
pathsResult = pathsResult.substring(0, pathsResult.length() - 1);
}
return new S3Path(fileSystem, pathsResult);
}
@Override
public boolean startsWith(Path other) {
if (other.getNameCount() > this.getNameCount()) {
return false;
}
if (!(other instanceof S3Path)) {
return false;
}
if (this.isAbsolute() && !other.isAbsolute()) {
return false;
}
S3Path path = (S3Path) other;
if (this.isAbsolute() && other.isAbsolute() &&
!this.fileStore.name().equals(path.fileStore.name())) {
return false;
}
if (path.uri.isEmpty() && !this.uri.isEmpty()) {
return false;
}
List<String> pathsOther = path.uriToList();
List<String> paths = this.uriToList();
for (int i = 0; i < pathsOther.size(); i++) {
if (!pathsOther.get(i).equals(paths.get(i))) {
return false;
}
}
return true;
}
@Override
public boolean startsWith(String path) {
S3Path other = new S3Path(this.fileSystem, path);
return this.startsWith(other);
}
@Override
public boolean endsWith(Path other) {
if (other.getNameCount() > this.getNameCount()) {
return false;
}
// empty
if (other.getNameCount() == 0 && this.getNameCount() != 0) {
return false;
}
if (!(other instanceof S3Path)) {
return false;
}
S3Path path = (S3Path) other;
if ((path.getFileStore() != null && !path.getFileStore().equals(this.getFileStore())) || (path.getFileStore() != null && this.getFileStore() == null)) {
return false;
}
// check subkeys
List<String> pathsOther = path.uriToList();
List<String> paths = this.uriToList();
int i = pathsOther.size() - 1;
int j = paths.size() - 1;
for (; i >= 0 && j >= 0; ) {
if (!pathsOther.get(i).equals(paths.get(j))) {
return false;
}
i--;
j--;
}
return true;
}
@Override
public boolean endsWith(String other) {
return this.endsWith(new S3Path(this.fileSystem, other));
}
@Override
public Path normalize() {
return this;
}
@Override
public Path resolve(Path other) {
if (other.isAbsolute()) {
Preconditions.checkArgument(other instanceof S3Path, "other must be an instance of %s", S3Path.class.getName());
return other;
}
S3Path otherS3Path = (S3Path) other;
StringBuilder pathBuilder = new StringBuilder();
if (this.isAbsolute()) {
pathBuilder.append(PATH_SEPARATOR + this.fileStore.name() + PATH_SEPARATOR);
}
pathBuilder.append(this.uri);
if (!otherS3Path.uri.isEmpty())
pathBuilder.append(PATH_SEPARATOR + otherS3Path.uri);
return new S3Path(this.fileSystem, pathBuilder.toString());
}
@Override
public Path resolve(String other) {
return resolve(new S3Path(this.getFileSystem(), other));
}
@Override
public Path resolveSibling(Path other) {
Preconditions.checkArgument(other instanceof S3Path, "other must be an instance of %s", S3Path.class.getName());
S3Path s3Path = (S3Path) other;
Path parent = getParent();
if (parent == null || s3Path.isAbsolute()) {
return s3Path;
}
List<String> othersPaths = s3Path.uriToList();
if (othersPaths.isEmpty()) { // other is relative and empty
return parent;
}
List<String> paths = this.uriToList();
StringBuilder pathBuilder = new StringBuilder();
String lastPath = othersPaths.get(othersPaths.size() - 1);
if (isAbsolute()) {
pathBuilder.append(PATH_SEPARATOR + fileStore.name() + PATH_SEPARATOR);
}
for (String path : concat(paths.subList(0, paths.size() - 1), othersPaths)) {
pathBuilder.append(path);
if (!lastPath.equals(path) || s3Path.uri.endsWith(PATH_SEPARATOR)) {
pathBuilder.append(PATH_SEPARATOR);
}
}
return new S3Path(fileSystem, pathBuilder.toString());
}
@Override
public Path resolveSibling(String other) {
return resolveSibling(new S3Path(this.getFileSystem(), other));
}
@Override
public Path relativize(Path other) {
Preconditions.checkArgument(other instanceof S3Path, "other must be an instance of %s", S3Path.class.getName());
S3Path s3Path = (S3Path) other;
if (this.equals(other)) {
return new S3Path(this.getFileSystem(), "");
}
Preconditions.checkArgument(isAbsolute(), "Path is already relative: %s", this);
Preconditions.checkArgument(s3Path.isAbsolute(), "Cannot relativize against a relative path: %s", s3Path);
Preconditions.checkArgument(fileStore.equals(s3Path.getFileStore()), "Cannot relativize paths with different buckets: '%s', '%s'", this, other);
// Preconditions.checkArgument(parts.size() <= s3Path.parts.size(), "Cannot relativize against a parent path: '%s', '%s'", this, other);
String uriPath = decode(URI.create(encode(this.uri)).relativize(URI.create(encode(s3Path.uri))));
return new S3Path(fileSystem, uriPath);
}
/**
* Examples:
*
* Relative:
* --------
* NO use fileSystem and not used fileStore.
* - path/file
*
* Absolute:
* --------
* Use the fileSystem to get the host and the filestore to get the first path (in the future the filestore can be attached to the host)
* http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
* - s3://AMAZONACCESSKEY@s3.amazonaws.com/bucket/path/file
* - s3://AMAZONACCESSKEY@bucket.s3.amazonaws.com/path/file
* - s3://s3-aws-region.amazonaws.com/bucket/path/file
*
* @return URI never null
*/
@Override
public URI toUri() {
String uri = encode(this.uri);
// absolute
if (this.isAbsolute()) {
StringBuilder builder = new StringBuilder();
builder.append(fileSystem.getKey());
builder.append(PATH_SEPARATOR + fileStore.name() + PATH_SEPARATOR);
builder.append(uri);
return URI.create("s3://" + normalizeURI(builder.toString()));
}
else {
return URI.create(uri);
}
}
/**
* Get the url for the s3Path.
*
* The url represents a Uniform Resource
* Locator, a pointer to a "resource" on the World
* Wide Web.
*
* All S3Path has a URL if is absolute
*
* @see com.amazonaws.services.s3.AmazonS3#getUrl(String, String)
* @see S3Path#toUri() for unique resource identifier
* @return URL or null if is not absoulte
*/
public URL toURL() {
if (!this.isAbsolute())
return null;
return this.getFileSystem().getClient().getUrl(this.fileStore.name(), this.getKey());
}
@Override
public Path toAbsolutePath() {
if (isAbsolute()) {
return this;
}
throw new IllegalStateException(format("Relative path cannot be made absolute: %s", this));
}
@Override
public Path toRealPath(LinkOption... options) throws IOException {
return toAbsolutePath();
}
@Override
public File toFile() {
throw new UnsupportedOperationException();
}
@Override
public WatchKey register(WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public Iterator<Path> iterator() {
ImmutableList.Builder<Path> builder = ImmutableList.builder();
if (isAbsolute()) {
builder.add(new S3Path(fileSystem, PATH_SEPARATOR + fileStore.name() + PATH_SEPARATOR));
}
List<String> paths = uriToList();
if (uriToList().isEmpty())
return builder.build().iterator();
String lastPath = paths.get(paths.size() - 1);
for (String path : uriToList()) {
String pathFinal = path + PATH_SEPARATOR;
if (path.equals(lastPath) && !lastPath.endsWith(PATH_SEPARATOR)) {
pathFinal = pathFinal.substring(0, pathFinal.length() - 1);
}
builder.add(new S3Path(fileSystem, pathFinal));
}
return builder.build().iterator();
}
@Override
public int compareTo(Path other) {
return toString().compareTo(other.toString());
}
@Override
public String toString() {
return toUri().toString();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
S3Path path = (S3Path) o;
if (fileStore != null ? !fileStore.equals(path.fileStore) : path.fileStore != null)
return false;
if (!uri.equals(path.uri))
return false;
return true;
}
@Override
public int hashCode() {
int result = fileStore != null ? fileStore.name().hashCode() : 0;
result = 31 * result + uri.hashCode();
return result;
}
/**
* Encode special URI characters for path.
* @param uri String the uri path
* @return String
*/
private String encode(String uri) {
try {
// URL encode all characters, but then convert known allowed characters back
return URLEncoder.encode(uri, "UTF-8")
.replace("%3A", ":")
.replace("%2F", "/")
.replace("+", "%20");
} catch (UnsupportedEncodingException e) {
// This should never happen unless there is something
// fundamentally broken with the running JVM.
throw new RuntimeException(e);
}
}
/**
* Decode uri special characters
*
* @param uri URI mandatory
* @return String decoded
*/
private String decode(URI uri) {
try {
return URLDecoder.decode(uri.toString(), "UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Error decoding key: " + this.uri, e);
}
}
public S3BasicFileAttributes getFileAttributes() {
return fileAttributes;
}
public void setFileAttributes(S3BasicFileAttributes fileAttributes) {
this.fileAttributes = fileAttributes;
}
}