You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2021/09/30 15:06:32 UTC

[tika] 01/02: add prefix to S3Fetcher

This is an automated email from the ASF dual-hosted git repository.

tallison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git

commit 2f010f877272fd881634b82fb8a00d681211eca5
Author: tballison <ta...@apache.org>
AuthorDate: Thu Sep 30 10:59:12 2021 -0400

    add prefix to S3Fetcher
---
 .../apache/tika/pipes/fetcher/s3/S3Fetcher.java    | 27 ++++++++++++++++++----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/tika-pipes/tika-fetchers/tika-fetcher-s3/src/main/java/org/apache/tika/pipes/fetcher/s3/S3Fetcher.java b/tika-pipes/tika-fetchers/tika-fetcher-s3/src/main/java/org/apache/tika/pipes/fetcher/s3/S3Fetcher.java
index 5f400dd..0e8b190 100644
--- a/tika-pipes/tika-fetchers/tika-fetcher-s3/src/main/java/org/apache/tika/pipes/fetcher/s3/S3Fetcher.java
+++ b/tika-pipes/tika-fetchers/tika-fetcher-s3/src/main/java/org/apache/tika/pipes/fetcher/s3/S3Fetcher.java
@@ -45,6 +45,7 @@ import org.apache.tika.io.TikaInputStream;
 import org.apache.tika.metadata.Metadata;
 import org.apache.tika.pipes.fetcher.AbstractFetcher;
 import org.apache.tika.pipes.fetcher.RangeFetcher;
+import org.apache.tika.utils.StringUtils;
 
 /**
  * Fetches files from s3. Example string: s3://my_bucket/path/to/my_file.pdf
@@ -58,6 +59,7 @@ public class S3Fetcher extends AbstractFetcher implements Initializable, RangeFe
     private String region;
     private String bucket;
     private String profile;
+    private String prefix;
     private String credentialsProvider;
     private boolean extractUserMetadata = true;
     private int maxConnections = ClientConfiguration.DEFAULT_MAX_CONNECTIONS;
@@ -67,10 +69,11 @@ public class S3Fetcher extends AbstractFetcher implements Initializable, RangeFe
     @Override
     public InputStream fetch(String fetchKey, Metadata metadata) throws TikaException, IOException {
 
-        LOGGER.debug("about to fetch fetchkey={} from bucket ({})", fetchKey, bucket);
+        String theFetchKey = StringUtils.isBlank(prefix) ? fetchKey : prefix + fetchKey;
+        LOGGER.debug("about to fetch fetchkey={} from bucket ({})", theFetchKey, bucket);
 
         try {
-            S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucket, fetchKey));
+            S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucket, theFetchKey));
             if (extractUserMetadata) {
                 for (Map.Entry<String, String> e : s3Object.getObjectMetadata().getUserMetadata()
                         .entrySet()) {
@@ -95,13 +98,14 @@ public class S3Fetcher extends AbstractFetcher implements Initializable, RangeFe
     @Override
     public InputStream fetch(String fetchKey, long startRange, long endRange, Metadata metadata)
             throws TikaException, IOException {
+        String theFetchKey = StringUtils.isBlank(prefix) ? fetchKey : prefix + fetchKey;
         //TODO -- figure out how to integrate this
-        LOGGER.debug("about to fetch fetchkey={} (start={} end={}) from bucket ({})", fetchKey,
-                startRange, endRange, bucket);
+        LOGGER.debug("about to fetch fetchkey={} (start={} end={}) from bucket ({})",
+                theFetchKey, startRange, endRange, bucket);
 
         try {
             S3Object s3Object = s3Client.getObject(
-                    new GetObjectRequest(bucket, fetchKey).withRange(startRange, endRange));
+                    new GetObjectRequest(bucket, theFetchKey).withRange(startRange, endRange));
 
             if (extractUserMetadata) {
                 for (Map.Entry<String, String> e : s3Object.getObjectMetadata().getUserMetadata()
@@ -145,6 +149,19 @@ public class S3Fetcher extends AbstractFetcher implements Initializable, RangeFe
     }
 
     /**
+     * prefix to prepend to the fetch key before fetching.
+     * This will automatically add a '/' at the end.
+     * @param prefix
+     */
+    @Field
+    public void setPrefix(String prefix) {
+        //guarantee that the prefix ends with /
+        if (! prefix.endsWith("/")) {
+            prefix += "/";
+        }
+        this.prefix = prefix;
+    }
+    /**
      * Whether or not to extract user metadata from the S3Object
      *
      * @param extractUserMetadata