Example usage for com.amazonaws.services.s3.model S3Object getObjectContent

List of usage examples for com.amazonaws.services.s3.model S3Object getObjectContent

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model S3Object getObjectContent.

Prototype

public S3ObjectInputStream getObjectContent() 

Source Link

Document

Gets the input stream containing the contents of this object.

Usage

From source file:jp.sanix.yokusei.java

License:Open Source License

public static void main(String[] args) throws IOException, SQLException, AmazonServiceException, JSONException,
        NullPointerException, ParseException {

    String id = "A0002441";
    String datefrom = "2015/10/01";
    String dateto = "2015/10/12";

    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    format.setTimeZone(TimeZone.getTimeZone("JST"));
    SimpleDateFormat pgformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    pgformat.setTimeZone(TimeZone.getTimeZone("UTC"));

    Calendar cal = Calendar.getInstance();
    Calendar end = Calendar.getInstance();
    String today = toDate(cal);// w  ww . j a va2 s . c  om

    try {
        cal.setTime(format.parse(datefrom));
    } catch (ParseException e) {
    }
    try {
        end.setTime(format.parse(dateto));
        end.add(Calendar.DAY_OF_MONTH, 1);
    } catch (ParseException e) {
    }

    AmazonS3 s3 = new AmazonS3Client();
    s3.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1));

    Connection db = DriverManager.getConnection(PG_CON, PG_USER, PG_PASS);
    Statement st = db.createStatement();
    ResultSet rs = st.executeQuery(
            "SELECT data, pvs_unique_code FROM data WHERE pvs_serial_id='" + id + "' OFFSET 0 LIMIT 1;");
    rs.next();
    String json = rs.getString(1);
    String key = rs.getString(2);
    rs.close();
    db.close();
    Date recent = new Date();

    xlsSheetYokusei xls = new xlsSheetYokusei(json);
    while (cal.before(end)) {
        System.out.println("Getting data of " + toDate(cal));

        /* AWS S3????? */
        String bucketName = "pvdata-storage-production";
        System.out.println("Get s3 data by key='" + bucketName + "/data/" + key + "/" + toDate(cal) + "/'");
        ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName)
                .withPrefix("data/" + key + "/" + toDate(cal) + "/"));

        /* get data from s3 */
        do {
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                String keyname = objectSummary.getKey();
                S3Object object = s3.getObject(new GetObjectRequest(bucketName, keyname));
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(new GZIPInputStream(object.getObjectContent())));
                String line;
                while ((line = reader.readLine()) != null) {
                    try {
                        json = line.substring(line.indexOf("{"));
                        xls.putData(json);
                    } catch (NullPointerException e) {
                    }
                }
                reader.close();
                object.close();
            }
            objectListing = s3.listNextBatchOfObjects(objectListing);
        } while (objectListing.getMarker() != null);

        /* if today, read postgres to get recent data */
        if (toDate(cal).equals(today)) {
            System.out.println("Get recent data from postgres");
            try {
                db = DriverManager.getConnection(PG_CON, PG_USER, PG_PASS);
                st = db.createStatement();
                String sql = "SELECT data FROM data WHERE pvs_unique_code='" + key + "' AND created_at > '"
                        + pgformat.format(recent) + "';";
                System.out.println(sql);
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    json = rs.getString(1);
                    xls.putData(json);
                }
                rs.close();
                db.close();
            } catch (PSQLException e) {
            } catch (ParseException e) {
            }
        }
        System.out.println("Write Buffer");
        xls.writeBuffer();

        cal.add(Calendar.DAY_OF_MONTH, 1);
    }
    File file = new File(
            "C:\\Users\\SANIX_CORERD\\Desktop\\" + id + "-Diamond" + toDate(cal).replace("/", "-") + ".xlsx");
    xls.putFile(new FileOutputStream(file));
    System.out.println("Finished: " + toDate(cal));
}

From source file:model.data.location.FileAccessFactory.java

License:Apache License

/**
 * Gets the input stream for an S3 file store. This will stream the bytes from S3. Null, or exception will be thrown
 * if an error occurs during acquisition.
 * // ww w. j a v a2 s.  co m
 * The S3 Credentials MUST be populated using the setCredentials() method before executing this call, or a
 * Credentials exception is likely to be thrown by S3.
 */
@JsonIgnore
public InputStream getS3File(FileLocation fileLocation, String accessKey, String privateKey,
        String s3EncryptKey) {
    // Get the file from S3. Connect to S3 Bucket. Only apply credentials if they are present.
    final AmazonS3Client s3Client;
    S3FileStore fileStore = (S3FileStore) fileLocation;
    if (accessKey.isEmpty() || privateKey.isEmpty()) {
        s3Client = new AmazonS3Client();
    } else {
        // If an encryption key was provided, use the encrypted client
        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, privateKey);
        if (s3EncryptKey != null) {
            KMSEncryptionMaterialsProvider materialProvider = new KMSEncryptionMaterialsProvider(s3EncryptKey);
            s3Client = new AmazonS3EncryptionClient(credentials, materialProvider,
                    new CryptoConfiguration().withKmsRegion(Regions.US_EAST_1))
                            .withRegion(Region.getRegion(Regions.US_EAST_1));
        } else {
            s3Client = new AmazonS3Client(credentials);
        }
    }
    S3Object s3Object = s3Client.getObject(fileStore.getBucketName(), fileStore.getFileName());
    return s3Object.getObjectContent();
}

From source file:net.geoprism.data.aws.AmazonEndpoint.java

License:Open Source License

@Override
public void copyFiles(File directory, List<String> keys, boolean preserveDirectories) {
    try {/*from   w w w .  java2 s.c o m*/
        List<File> files = new LinkedList<File>();

        AmazonS3 client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());

        for (String key : keys) {
            GetObjectRequest request = new GetObjectRequest("geodashboarddata", key);

            S3Object object = client.getObject(request);

            InputStream istream = object.getObjectContent();

            try {
                String targetPath = this.getTargetPath(preserveDirectories, key);

                File file = new File(directory, targetPath);

                FileUtils.copyInputStreamToFile(istream, file);

                files.add(file);
            } finally {
                // Process the objectData stream.
                istream.close();
            }
        }
    } catch (IOException e) {
        throw new ProgrammingErrorException(e);
    }
}

From source file:net.henryhu.roxlab2.NotePadProvider.java

License:Apache License

private ContentValues s3query(String id) {
    if (bucket == null)
        bucket = s3.createBucket(bucketName);

    Log.w("s3query()", "query id: " + id);

    ObjectListing ol = s3.listObjects(bucketName, id + "_");
    for (Object o : ol.getObjectSummaries()) {
        S3ObjectSummary sum = (S3ObjectSummary) o;
        S3Object obj = s3.getObject(bucketName, sum.getKey());

        ObjectMetadata om = obj.getObjectMetadata();

        byte[] buf = new byte[(int) om.getContentLength()];
        try {//from  w ww .  j  a  v  a2 s.com
            InputStream contents = obj.getObjectContent();
            contents.read(buf);
            contents.close();
            String sbuf = new String(buf, "UTF-8");
            Map<String, String> entries = ParseInfo.getEntries(sbuf);
            ContentValues vals = new ContentValues();
            for (String key : entries.keySet()) {
                vals.put(key, entries.get(key));
            }
            Log.w("s3query()", "query succ");
            return vals;
        } catch (Exception e) {
        }
    }
    Log.w("s3query()", "query fail");
    return null;
}

From source file:net.lizhaoweb.aws.api.service.impl.SNSNotificationHandlerForCloudSearch.java

License:Open Source License

private String getAccessUrl(S3Object s3Object) {
    if (s3Object == null) {
        return null;
    }/*from   w  ww .j  a  v a2 s.  c om*/
    S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
    if (s3ObjectInputStream == null) {
        return null;
    }
    HttpRequestBase httpRequestBase = s3ObjectInputStream.getHttpRequest();
    if (httpRequestBase == null) {
        return null;
    }
    URI uri = httpRequestBase.getURI();
    if (uri == null) {
        return null;
    }
    String accessUrl = uri.toString();
    return accessUrl;
}

From source file:net.oletalk.hellospringboot.dao.S3Dao.java

public void getObjectData(String bucketName, String key, OutputStream out) throws S3Exception {
    AmazonS3 s3client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
    LOG.info("fetching requested object " + key);
    try {//  ww  w. j  a v  a2s .co  m
        S3Object object = s3client.getObject(new GetObjectRequest(bucketName, key));
        LOG.info("fetched, serving up");
        try {
            IOUtils.copy(object.getObjectContent(), out);
        } catch (IOException ioe) {
            LOG.error("Problem writing message: " + ioe.getMessage());
        }

    } catch (AmazonS3Exception e) {
        LOG.error("Problem fetching from S3: ", e);
        String errorMsg = "Error fetching document";
        try {
            out.write(errorMsg.getBytes(Charset.defaultCharset()));
            throw new S3Exception("Error fetching document");
        } catch (IOException ioe) {
            LOG.error("Problem writing message: " + ioe.getMessage());
        }
    }
}

From source file:net.smartcosmos.plugin.service.aws.storage.AwsS3StorageService.java

License:Apache License

@Override
public InputStream retrieve(IFile file) throws IOException {
    Preconditions.checkArgument((file != null), "file must not be null");

    AmazonS3 s3 = new AmazonS3Client(credentials, new ClientConfiguration().withProtocol(Protocol.HTTPS));

    GetObjectRequest getObjectRequest = new GetObjectRequest(getBucketName(), file.getFileName());
    S3Object storedObject = s3.getObject(getObjectRequest);

    return storedObject.getObjectContent();
}

From source file:net.solarnetwork.node.backup.s3.S3BackupResource.java

License:Open Source License

@Override
public InputStream getInputStream() throws IOException {
    S3Object obj = client.getObject(metadata.getObjectKey());
    return obj.getObjectContent();
}

From source file:net.solarnetwork.node.setup.s3.S3SetupManager.java

License:Open Source License

/**
 * Download and install all setup objects in a given configuration.
 * //from   ww  w. j ava 2s  .  c  o m
 * @param config
 *        the configuration to apply
 * @return the set of absolute paths of all installed files (or an empty set
 *         if nothing installed)
 * @throws IOException
 *         if an IO error occurs
 */
private Set<Path> applySetupObjects(S3SetupConfiguration config) throws IOException {
    Set<Path> installed = new LinkedHashSet<>();
    for (String dataObjKey : config.getObjects()) {
        if (!TARBALL_PAT.matcher(dataObjKey).find()) {
            log.warn("S3 setup resource {} not a supported type; skipping");
            continue;
        }
        S3Object obj = s3Client.getObject(dataObjKey);
        if (obj == null) {
            log.warn("Data object {} not found, cannot apply setup", dataObjKey);
            continue;
        }

        File workDir = new File(workDirectory);
        if (!workDir.exists()) {
            if (!workDir.mkdirs()) {
                log.warn("Unable to create work dir {}", workDir);
            }
        }

        // download the data object to the work dir
        String dataObjFilename = DigestUtils.sha1Hex(dataObjKey);
        File dataObjFile = new File(workDir, dataObjFilename);
        try (InputStream in = obj.getObjectContent();
                OutputStream out = new BufferedOutputStream(new FileOutputStream(dataObjFile))) {
            log.info("Downloading S3 setup resource {} -> {}", dataObjKey, dataObjFile);
            FileCopyUtils.copy(obj.getObjectContent(), out);

            // extract tarball
            List<Path> extractedPaths = extractTarball(dataObjFile);
            installed.addAll(extractedPaths);
        } finally {
            if (dataObjFile.exists()) {
                dataObjFile.delete();
            }
        }
    }
    if (!installed.isEmpty()) {
        log.info("Installed files from objects {}: {}", Arrays.asList(config.getObjects()), installed);
    }
    return installed;
}

From source file:nl.nn.adapterframework.filesystem.AmazonS3FileSystem.java

License:Apache License

@Override
public InputStream readFile(S3Object f) throws FileSystemException, IOException {
    try {// w  ww. ja  v a 2  s.co m
        final S3Object file = s3Client.getObject(bucketName, f.getKey());
        final S3ObjectInputStream is = file.getObjectContent();

        return is;
    } catch (AmazonServiceException e) {
        throw new FileSystemException(e);
    }
}