AWS Create new files from s3 object using JAVA error

I have a form file and I need to read the form file from my java code. I used the code below to read the form file.

public class App {
    public static void main(String[] args) {
        File file = new File("C:\\Test\\sample.shp");
        Map<String, Object> map = new HashMap<>();//
        try {
            map.put("url", URLs.fileToUrl(file));
            DataStore dataStore = DataStoreFinder.getDataStore(map);
            String typeName = dataStore.getTypeNames()[0];
            SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
            SimpleFeatureCollection collection = source.getFeatures();

            try (FeatureIterator<SimpleFeature> features = collection.features()) {
                while (features.hasNext()) {
                    SimpleFeature feature = features.next();
                    SimpleFeatureType schema = feature.getFeatureType();
                    Class<?> geomType = schema.getGeometryDescriptor().getType().getBinding();

                    String type = "";
                    if (Polygon.class.isAssignableFrom(geomType) || MultiPolygon.class.isAssignableFrom(geomType)) {

                        MultiPolygon geom = (MultiPolygon) feature.getDefaultGeometry();
                        type = "Polygon";
                        if (geom.getNumGeometries() > 1) {
                            type = "MultiPolygon";
                        }
                    } else if (LineString.class.isAssignableFrom(geomType)
                            || MultiLineString.class.isAssignableFrom(geomType)) {
                    } else {

                    }
                    System.out.println(feature.getDefaultGeometryProperty().getValue().toString());

                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }
}

I got the desired result. But my requirement is to write aws lambda function to read the form file. To do this 1. I created the jamba java project of the s3 event project. I wrote the same code inside handleRequest. I loaded the java lambda project as a lanbda function and added one trigger. When I load the .shp file as s3 bucket, the lmbda function will be called automatically. But I get an error like below

java.lang.RuntimeException: java.io.FileNotFoundException: /sample.shp (There is no such file or directory)

sample.shp s3-. . S3 ?

. ,

  S3Object object = s3.getObject(new GetObjectRequest(bucket, key)); 
  InputStream objectData = object.getObjectContent();
  map.put("url", objectData );

File file = new File("C:\\Test\\sample.shp"); 
 map.put("url", URLs.fileToUrl(file));

:-( ,

java.lang.NullPointerException

DataStore dataStore = DataStoreFinder.getDataStore(objectData);

DataStore dataStore = DataStoreFinder.getDataStore(map);

java.lang.ClassCastException: com.amazonaws.services.s3.model.S3ObjectInputStream java.util.Map

, DataStore. ..: - (

-, ? , - ...

+4
1

DataStoreFinder.getDataStore geotools , , / "url". , "url", URL- , "file://host/path/my.shp".

Java . , URL- .

geotools URL- http/https (. geotools ), ://URL. , S3 Lambda, ://URL-, . Java-:

// get the shape file from S3 to local filesystem
File localshp = new File("/tmp/download.shp");
s3.getObject(new GetObjectRequest(bucket, key), localshp);

// now store file:// URL in the map
map.put("url", localshp.getURI().getURL().toString());

geotools URL- ( ://URL-), URL- S3 URL- .

, :

// get current time and add one hour
java.util.Date expiration = new java.util.Date();
long msec = expiration.getTime();
msec += 1000 * 60 * 60;
expiration.setTime(msec);

// request pre-signed URL that will allow bearer to GET the object
GeneratePresignedUrlRequest gpur = new GeneratePresignedUrlRequest(bucket, key);
gpur.setMethod(HttpMethod.GET);
gpur.setExpiration(expiration);

// get URL that will expire in one hour
URL url = s3.generatePresignedUrl(gpur);
+2

Source: https://habr.com/ru/post/1692045/


All Articles