Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
S3FilesResource - refactored
public interface S3Facade {
List<S3File> listObjects(String bucketName);
}
public class S3FacadeImpl implements S3Facade {
AmazonS3Client amazonS3Client;
@Override
public List<S3File> listObjects(String bucketName) {
List<S3File> result = new ArrayList<S3File>();
List<S3ObjectSummary> files = this.amazonS3Client.listObjects(bucketName).getObjectSummaries();
for (S3ObjectSummary file : files) {
result.add(new S3File(file.getKey(), file.getKey())); // later we can use st. else for the display name
}
return result;
}
}
public class S3File {
public final String displayName;
public final String path;
public S3File(String displayName, String path) {
this.displayName = displayName;
this.path = path;
}
}
public class S3FilesResource {
S3Facade amazonS3Client = new S3FacadeImpl();
// ...
@Path("/files")
public String listS3Files() {
StringBuilder html = new StringBuilder("<html><body>");
List<S3File> files = fetchS3Files();
for (S3File file : files) {
html.append("<a href='/content?fileName=").append(file.path).append("'>").append(file.displayName)
.append("</a><br>");
}
return html.append("</body></html>").toString();
}
List<S3File> fetchS3Files() {
List<S3File> files = this.amazonS3Client.listObjects("myBucket");
List<S3File> result = new ArrayList<S3File>(files.size());
for (S3File file : files) {
if (!file.path.endsWith("/")) {
result.add(file);
}
}
return result;
}
@Path("/content")
public String getContent(@QueryParam("fileName") String fileName) {
throw new UnsupportedOperationException("Not implemented yet");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.