/
S3ObjectListService.java
217 lines (203 loc) · 10.3 KB
/
S3ObjectListService.java
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
package ch.cyberduck.core.s3;
/*
* Copyright (c) 2002-2013 David Kocher. All rights reserved.
* http://cyberduck.ch/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
*/
import ch.cyberduck.core.AttributedList;
import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.ListProgressListener;
import ch.cyberduck.core.ListService;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathAttributes;
import ch.cyberduck.core.PathContainerService;
import ch.cyberduck.core.PathNormalizer;
import ch.cyberduck.core.Preferences;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.features.Versioning;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jets3t.service.ServiceException;
import org.jets3t.service.StorageObjectsChunk;
import org.jets3t.service.VersionOrDeleteMarkersChunk;
import org.jets3t.service.model.BaseVersionOrDeleteMarker;
import org.jets3t.service.model.S3Version;
import org.jets3t.service.model.StorageObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @version $Id$
*/
public class S3ObjectListService implements ListService {
private static final Logger log = Logger.getLogger(S3Session.class);
private S3Session session;
private S3AttributesFeature attributes;
private PathContainerService containerService
= new PathContainerService();
private Map<Path, VersioningConfiguration> versioning
= new HashMap<Path, VersioningConfiguration>();
public S3ObjectListService(final S3Session session) {
this.session = session;
this.attributes = new S3AttributesFeature(session);
}
@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
try {
// Keys can be listed by prefix. By choosing a common prefix
// for the names of related keys and marking these keys with
// a special character that delimits hierarchy, you can use the list
// operation to select and browse keys hierarchically
String prefix = StringUtils.EMPTY;
if(!containerService.isContainer(directory)) {
// Restricts the response to only contain results that begin with the
// specified prefix. If you omit this optional argument, the value
// of Prefix for your query will be the empty string.
// In other words, the results will be not be restricted by prefix.
prefix = containerService.getKey(directory);
if(!prefix.endsWith(String.valueOf(Path.DELIMITER))) {
prefix += Path.DELIMITER;
}
}
// If this optional, Unicode string parameter is included with your request,
// then keys that contain the same string between the prefix and the first
// occurrence of the delimiter will be rolled up into a single result
// element in the CommonPrefixes collection. These rolled-up keys are
// not returned elsewhere in the response.
final AttributedList<Path> children = new AttributedList<Path>();
final Path container = containerService.getContainer(directory);
children.addAll(this.listObjects(container,
directory, prefix, String.valueOf(Path.DELIMITER), listener));
if(Preferences.instance().getBoolean("s3.revisions.enable")) {
if(session.getFeature(Versioning.class) != null
&& session.getFeature(Versioning.class).withCache(versioning).getConfiguration(container).isEnabled()) {
String priorLastKey = null;
String priorLastVersionId = null;
do {
final VersionOrDeleteMarkersChunk chunk = session.getClient().listVersionedObjectsChunked(
container.getName(), prefix, String.valueOf(Path.DELIMITER),
Preferences.instance().getInteger("s3.listing.chunksize"),
priorLastKey, priorLastVersionId, true);
children.addAll(this.listVersions(container, directory,
Arrays.asList(chunk.getItems())));
priorLastKey = chunk.getNextKeyMarker();
priorLastVersionId = chunk.getNextVersionIdMarker();
listener.chunk(children);
}
while(priorLastKey != null);
}
}
return children;
}
catch(ServiceException e) {
throw new ServiceExceptionMappingService().map("Listing directory failed", e, directory);
}
catch(IOException e) {
throw new DefaultIOExceptionMappingService().map(e, directory);
}
}
private AttributedList<Path> listObjects(final Path bucket, final Path parent,
final String prefix, final String delimiter,
final ListProgressListener listener)
throws IOException, ServiceException, BackgroundException {
final AttributedList<Path> children = new AttributedList<Path>();
// Null if listing is complete
String priorLastKey = null;
do {
// Read directory listing in chunks. List results are always returned
// in lexicographic (alphabetical) order.
final StorageObjectsChunk chunk = session.getClient().listObjectsChunked(
bucket.getName(), prefix, delimiter,
Preferences.instance().getInteger("s3.listing.chunksize"), priorLastKey);
final StorageObject[] objects = chunk.getObjects();
for(StorageObject object : objects) {
final String key = PathNormalizer.normalize(object.getKey());
if(new Path(bucket, key, EnumSet.of(Path.Type.directory)).equals(parent)) {
continue;
}
final PathAttributes attr = attributes.find(object);
final Path p = new Path(parent, PathNormalizer.name(key),
attr.isPlaceholder() ? EnumSet.of(Path.Type.directory) : EnumSet.of(Path.Type.file), attr);
// Copy bucket location
p.attributes().setRegion(bucket.attributes().getRegion());
children.add(p);
}
final String[] prefixes = chunk.getCommonPrefixes();
for(String common : prefixes) {
if(common.equals(String.valueOf(Path.DELIMITER))) {
log.warn(String.format("Skipping prefix %s", common));
continue;
}
final String key = PathNormalizer.normalize(common);
if(new Path(bucket, key, EnumSet.of(Path.Type.directory)).equals(parent)) {
continue;
}
final Path p = new Path(parent, PathNormalizer.name(key), EnumSet.of(Path.Type.directory));
if(children.contains(p.getReference())) {
// There is already a placeholder object
continue;
}
p.attributes().setRegion(bucket.attributes().getRegion());
p.attributes().setPlaceholder(false);
children.add(p);
}
priorLastKey = chunk.getPriorLastKey();
listener.chunk(children);
}
while(priorLastKey != null);
return children;
}
private List<Path> listVersions(final Path bucket, final Path parent, final List<BaseVersionOrDeleteMarker> versionOrDeleteMarkers)
throws IOException, ServiceException {
// Amazon S3 returns object versions in the order in which they were
// stored, with the most recently stored returned first.
Collections.sort(versionOrDeleteMarkers, new Comparator<BaseVersionOrDeleteMarker>() {
@Override
public int compare(BaseVersionOrDeleteMarker o1, BaseVersionOrDeleteMarker o2) {
return o1.getLastModified().compareTo(o2.getLastModified());
}
});
final List<Path> versions = new ArrayList<Path>();
int i = 0;
for(BaseVersionOrDeleteMarker marker : versionOrDeleteMarkers) {
if((marker.isDeleteMarker() && marker.isLatest()) || !marker.isLatest()) {
// Latest version already in default listing
final String key = PathNormalizer.normalize(marker.getKey());
if(new Path(bucket, key, EnumSet.of(Path.Type.directory)).equals(parent)) {
continue;
}
final Path p = new Path(parent, PathNormalizer.name(key), EnumSet.of(Path.Type.file));
// Versioning is enabled if non null.
p.attributes().setVersionId(marker.getVersionId());
p.attributes().setRevision(++i);
p.attributes().setDuplicate(true);
p.attributes().setModificationDate(marker.getLastModified().getTime());
p.attributes().setRegion(bucket.attributes().getRegion());
if(marker instanceof S3Version) {
p.attributes().setSize(((S3Version) marker).getSize());
p.attributes().setETag(((S3Version) marker).getEtag());
p.attributes().setStorageClass(((S3Version) marker).getStorageClass());
}
versions.add(p);
}
}
return versions;
}
}