List of usage examples for com.amazonaws.services.s3.model S3Object close
@Override public void close() throws IOException
From source file:com.amediamanager.config.S3ConfigurationProvider.java
License:Apache License
@Override public void loadProperties() { this.properties = null; // Load properties if there is a bucket and key if (bucket != null && key != null) { AWSCredentialsProvider creds = new AWSCredentialsProviderChain(new InstanceProfileCredentialsProvider(), new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider()); AmazonS3 s3Client = new AmazonS3Client(creds); try {//from w w w. j a v a2s.com S3Object object = s3Client.getObject(this.bucket, this.key); if (object != null) { this.properties = new Properties(); try { this.properties.load(object.getObjectContent()); } catch (IOException e) { this.properties = null; LOG.warn("Found configuration file in S3 but failed to load properties (s3://{}/{})", new Object[] { this.bucket, this.key, e }); } finally { try { object.close(); } catch (IOException e) { // Don't care } } } } catch (AmazonS3Exception ase) { LOG.error("Error loading config from s3://{}/{}", new Object[] { this.bucket, this.key, ase }); } } }
From source file:com.ge.predix.solsvc.blobstore.bootstrap.BlobstoreClientImpl.java
License:Apache License
/** * Gets the list of available Blobs for the binded bucket from the * BlobStore./*from w w w. j a v a2 s. c om*/ * * @return List of DataFile Blobs */ @Override public List<DataFile> getBlob() { S3Object obj = null; try { List<DataFile> objs = new ArrayList<DataFile>(); // Get the List from BlobStore ObjectListing objectList = this.s3Client.listObjects(this.blobstoreConfig.getBucketName()); for (S3ObjectSummary objectSummary : objectList.getObjectSummaries()) { obj = this.s3Client.getObject( new GetObjectRequest(this.blobstoreConfig.getBucketName(), objectSummary.getKey())); DataFile data = new DataFile(); data.setFile(IOUtils.toByteArray(obj.getObjectContent())); objs.add(data); } return objs; } catch (IOException e) { throw new RuntimeException(e); } finally { if (obj != null) { try { obj.close(); } catch (IOException e) { throw new RuntimeException( "unable to close object object=" + obj + " throwing original exception", //$NON-NLS-1$ //$NON-NLS-2$ e); } } } }
From source file:com.miovision.oss.awsbillingtools.s3.loader.S3BillingRecordLoader.java
License:Open Source License
public Stream<T> load(S3BillingRecordFile billingRecordFile) throws IOException { S3Object s3Object = amazonS3.getObject(billingRecordFile.getBucketName(), billingRecordFile.getKey()); try {/*from www .j a v a2 s .co m*/ InputStream inputStream = s3Object.getObjectContent(); if (billingRecordFile.isZip()) { ZipInputStream zipInputStream = new ZipInputStream(inputStream); zipInputStream.getNextEntry(); inputStream = zipInputStream; } return billingRecordParser.parse(new InputStreamReader(inputStream, Charset.defaultCharset())); } catch (Exception e) { s3Object.close(); throw e; } }
From source file:com.rathravane.clerk.impl.s3.S3IamDb.java
License:Apache License
/** * Load an object to a stream./*w w w . j a v a 2 s . c om*/ * @param key * @param os * @returns true if found, false if not found * @throws IamSvcException * @throws IamBadRequestException */ private boolean loadTo(String key, OutputStream os) throws IamSvcException { S3Object object = null; try { object = fDb.getObject(new GetObjectRequest(fBucketId, key)); final InputStream is = object.getObjectContent(); // s3 objects must be closed or will leak an HTTP connection rrStreamTools.copyStream(is, os); return true; } catch (AmazonServiceException x) { if (404 == x.getStatusCode()) return false; throw new IamSvcException(x); } catch (AmazonClientException x) { throw new IamSvcException(x); } catch (IOException x) { throw new IamSvcException(x); } finally { if (object != null) { try { object.close(); } catch (IOException e) { throw new IamSvcException(e); } } } }
From source file:com.sangupta.urn.service.impl.AmazonS3UrnStorageServiceImpl.java
License:Apache License
@Override protected UrnObject get(String objectKey) { S3Object object = this.client.getObject(this.bucketName, objectKey); if (object == null) { return null; }// w w w. j a v a 2 s . c o m try { InputStream stream = object.getObjectContent(); byte[] bytes = IOUtils.toByteArray(stream); UrnObject urnObject = new UrnObject(objectKey, bytes); // TODO: read and populate metadata ObjectMetadata metadata = object.getObjectMetadata(); if (metadata != null) { if (metadata.getHttpExpiresDate() != null) { urnObject.expiry = metadata.getHttpExpiresDate().getTime(); } urnObject.mime = metadata.getContentType(); urnObject.stored = metadata.getLastModified().getTime(); // TODO:parse the value to extract the filename if available urnObject.name = metadata.getContentDisposition(); } // return the object return urnObject; } catch (IOException e) { // happens when we cannot read data from S3 LOGGER.debug("Exception reading data from S3 for object key: " + objectKey, e); return null; } finally { if (object != null) { try { object.close(); } catch (IOException e) { LOGGER.warn("Unable to close S3 object during/after reading the object"); } } } }
From source file:com.smoketurner.pipeline.application.core.AmazonS3Downloader.java
License:Apache License
/** * Retrieves a file from S3/* ww w . j a v a 2 s .c o m*/ * * @param record * S3 event notification record to download * @return S3 object * @throws AmazonS3ConstraintException * if the etag constraints weren't met * @throws AmazonS3ZeroSizeException * if the file size of the object is zero */ public S3Object fetch(@Nonnull final S3EventNotificationRecord record) throws AmazonS3ConstraintException, AmazonS3ZeroSizeException { final AmazonS3Object object = converter.convert(Objects.requireNonNull(record)); final GetObjectRequest request = new GetObjectRequest(object.getBucketName(), object.getKey()); object.getVersionId().ifPresent(request::setVersionId); object.getETag().ifPresent(etag -> request.setMatchingETagConstraints(Collections.singletonList(etag))); LOGGER.debug("Fetching key: {}/{}", object.getBucketName(), object.getKey()); final S3Object download; try { download = s3.getObject(request); } catch (AmazonServiceException e) { LOGGER.error("Service error while fetching object from S3", e); throw e; } catch (AmazonClientException e) { LOGGER.error("Client error while fetching object from S3", e); throw e; } if (download == null) { LOGGER.error("eTag from object did not match for key: {}/{}", object.getBucketName(), object.getKey()); throw new AmazonS3ConstraintException(object.getKey()); } final long contentLength = download.getObjectMetadata().getContentLength(); if (contentLength < 1) { try { download.close(); } catch (IOException e) { LOGGER.error(String.format("Failed to close S3 stream for key: %s/%s", download.getBucketName(), download.getKey()), e); } LOGGER.debug("Object size is zero for key: {}/{}", download.getBucketName(), download.getKey()); throw new AmazonS3ZeroSizeException(object.getKey()); } LOGGER.debug("Streaming key ({} bytes): {}/{}", contentLength, download.getBucketName(), download.getKey()); return download; }
From source file:jp.sanix.analysis.java
License:Open Source License
public static void main(String[] args) throws IOException, SQLException, AmazonServiceException, JSONException, NullPointerException, ParseException { if (args.length < 1) { System.out.println("Please specify at least an arguments that is pvs_serial_id."); System.out.println("analysis A9990004 // get today's data"); System.out.println("analysis A9990004 2015/05/30 // get from 2015/05/30 to today's data"); System.out.println("analysis A9990004 2015/05/30 2015/06/30"); System.exit(-1);/*www. j a v a2s . co m*/ } String id = args[0]; SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); format.setTimeZone(TimeZone.getTimeZone("JST")); SimpleDateFormat normalformat = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss'Z'"); normalformat.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); System.out.println(id); try { cal.setTime(format.parse(args[1])); } catch (ParseException e) { } catch (ArrayIndexOutOfBoundsException e) { } try { end.setTime(format.parse(args[2])); } catch (ParseException e) { } catch (ArrayIndexOutOfBoundsException e) { } end.add(Calendar.DAY_OF_MONTH, 1); 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(); while (cal.before(end)) { xlsSheet xls = new xlsSheet(json); System.out.println("Getting data of " + toDate(cal)); /* AWS S3????? */ String bucketName = "pvdata-storage-production"; // String bucketName = "pvdata-storage-staging"; 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()))); while (reader.ready()) { String line = reader.readLine(); try { json = line.substring(line.indexOf("{")); recent = parseDate(line.substring(0, 25)); 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(); File file = File.createTempFile("temp", ".xlsx"); file.deleteOnExit(); xls.putFile(new FileOutputStream(file)); System.out.println("Put S3"); s3.putObject(new PutObjectRequest("sanix-data-analysis", FOLDER + id + "-" + toDate(cal).replace("/", "-") + ".xlsx", file)); System.out.println("Finished: " + toDate(cal)); cal.add(Calendar.DAY_OF_MONTH, 1); } File file = File.createTempFile("temp", ".html"); file.deleteOnExit(); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName("sanix-data-analysis").withPrefix(FOLDER)); bw.write("<html><head></head><body><ul>\n"); do { for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { String keyname = objectSummary.getKey(); if (Pattern.compile("\\.xlsx$|\\.html$").matcher(keyname).find()) { bw.write("<li><a href=\"https://s3-ap-northeast-1.amazonaws.com/sanix-data-analysis/" + keyname + "\">" + keyname.replaceAll("^[^\\/]*\\/", "") + "</a></li>\n"); } } objectListing = s3.listNextBatchOfObjects(objectListing); } while (objectListing.getMarker() != null); bw.write("</ul></body></html>\n"); bw.flush(); bw.close(); s3.putObject(new PutObjectRequest("sanix-data-analysis", FOLDER + "index.html", file)); }
From source file:jp.sanix.analysisLocal.java
License:Open Source License
public static void main(String[] args) throws IOException, SQLException, AmazonServiceException, JSONException, NullPointerException, ParseException { BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(new File("canceled.csv")), "SJIS")); br.readLine();//from w w w . jav a2s .c om String l; while ((l = br.readLine()) != null) { String[] col = l.split(","); String id = col[0]; String datefrom = col[1]; String dateto = col[1]; 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); 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(); while (cal.before(end)) { xlsSheet xls = new xlsSheet(json); 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()))); while (reader.ready()) { String line = reader.readLine(); try { json = line.substring(line.indexOf("{")); recent = parseDate(line.substring(0, 25)); 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(); File file = new File( "C:\\Users\\SANIX_CORERD\\Desktop\\" + id + "_" + toDate(cal).replace("/", "-") + ".xlsx"); xls.putFile(new FileOutputStream(file)); System.out.println("Finished: " + toDate(cal)); cal.add(Calendar.DAY_OF_MONTH, 1); } } br.close(); }
From source file:jp.sanix.analysisStaging.java
License:Open Source License
public static void main(String[] args) throws IOException, SQLException, AmazonServiceException, JSONException, NullPointerException, ParseException { // if (args.length < 1) { // System.out.println("Please specify at least an arguments that is pvs_serial_id."); // System.out.println("analysis A9990004 // get today's data"); // System.out.println("analysis A9990004 2015/05/30 // get from 2015/05/30 to today's data"); // System.out.println("analysis A9990004 2015/05/30 2015/06/30"); // System.exit(-1); // }//from w ww . j av a 2s .c o m // String id = args[0]; String uniq = "763ff280b16c61f524ae31b33401bcd6"; SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); format.setTimeZone(TimeZone.getTimeZone("JST")); SimpleDateFormat normalformat = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss'Z'"); normalformat.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(); cal.setTime(format.parse("2015/11/23")); end.setTime(format.parse("2015/11/23")); System.out.println(uniq); try { cal.setTime(format.parse(args[1])); } catch (ParseException e) { } catch (ArrayIndexOutOfBoundsException e) { } try { end.setTime(format.parse(args[2])); } catch (ParseException e) { } catch (ArrayIndexOutOfBoundsException e) { } end.add(Calendar.DAY_OF_MONTH, 1); AmazonS3 s3 = new AmazonS3Client(); s3.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1)); String bucketName = "pvdata-storage-staging"; 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/" + uniq + "/" + toDate(cal) + "/'"); ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName) .withPrefix("data/" + uniq + "/" + toDate(cal) + "/")); /* get data from s3 */ xlsSheet xls = null; do { Boolean first = true; 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()))); while (reader.ready()) { String line = reader.readLine(); try { String json = line.substring(line.indexOf("{")); if (first) { xls = new xlsSheet(json); first = false; } else { xls.putData(json); } } catch (NullPointerException e) { } } reader.close(); object.close(); } objectListing = s3.listNextBatchOfObjects(objectListing); } while (objectListing.getMarker() != null); System.out.println("Write Buffer"); xls.writeBuffer(); File file = File.createTempFile("temp", ".xlsx"); file.deleteOnExit(); xls.putFile(new FileOutputStream(file)); System.out.println("Put S3"); s3.putObject(new PutObjectRequest("sanix-data-analysis", FOLDER + uniq + "-" + toDate(cal).replace("/", "-") + ".xlsx", file)); System.out.println("Finished: " + toDate(cal)); cal.add(Calendar.DAY_OF_MONTH, 1); } }
From source file:jp.sanix.mukaino.java
License:Open Source License
public static void main(String[] args) throws IOException, SQLException, AmazonServiceException, JSONException, NullPointerException, ParseException { AmazonS3 s3 = new AmazonS3Client(); s3.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1)); String[] ids = { "A0000089", "A0002570" }; Connection db;//from w w w . j av a 2 s. c o m if (System.getProperty("user.name").toString().equals("ec2-user")) { db = DriverManager.getConnection(PG_CON, PG_USER, PG_PASS); } else { db = DriverManager.getConnection(PG_CON_LOCAL, PG_USER, PG_PASS); } for (String id : ids) { pvSensor.getInstance().getById(id); Statement st = db.createStatement(); ResultSet rs = st.executeQuery("SELECT data, pvs_unique_code FROM data WHERE pvs_serial_id='" + id + "' ORDER BY created_at DESC OFFSET 0 LIMIT 1;"); rs.next(); String json = rs.getString(1); String key = rs.getString(2); rs.close(); st.close(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, -1); // 1?? Calendar end = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, -1); // 1??? do { String mDate = String.format("%04d-%02d-%02d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH)); xlsSheetMukaino xls = new xlsSheetMukaino(json); /* AWS S3????? */ String bucketName = "pvdata-storage-production"; System.out.print("Get s3 data by key='" + bucketName + String.format("data/%s/%04d/%02d/%02d/", key, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH))); ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName) .withPrefix("data/" + key + "/" + mDate.replace("-", "/"))); /* 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()))); while (reader.ready()) { String line = reader.readLine(); try { json = line.substring(line.indexOf("{")); xls.putData(json); } catch (NullPointerException e) { } } reader.close(); object.close(); System.out.print("."); } objectListing = s3.listNextBatchOfObjects(objectListing); } while (objectListing.getMarker() != null); System.out.println(); xls.writeBuffer(); File file = File.createTempFile("temp", ".xlsx"); file.deleteOnExit(); xls.putFile(new FileOutputStream(file)); s3.putObject(new PutObjectRequest("sanix-data-analysis", "Rw6WHQLqGcvs3wpWwALh80IHAiZqWYzw/" + id + "-" + mDate + ".xlsx", file)); System.out.println("Finished: " + mDate); cal.add(Calendar.DAY_OF_MONTH, 1); } while (cal.before(end)); } db.close(); File file = File.createTempFile("temp", ".html"); file.deleteOnExit(); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName("sanix-data-analysis") .withPrefix("Rw6WHQLqGcvs3wpWwALh80IHAiZqWYzw/")); bw.write("<html><head></head><body><ul>\n"); do { for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { String keyname = objectSummary.getKey(); if (Pattern.compile("\\.xlsx$|\\.html$").matcher(keyname).find()) { bw.write("<li><a href=\"https://s3-ap-northeast-1.amazonaws.com/sanix-data-analysis/" + keyname + "\">" + keyname.replaceAll("^[^\\/]*\\/", "") + "</a></li>\n"); } } objectListing = s3.listNextBatchOfObjects(objectListing); } while (objectListing.getMarker() != null); bw.write("</ul></body></html>\n"); bw.flush(); bw.close(); s3.putObject( new PutObjectRequest("sanix-data-analysis", "Rw6WHQLqGcvs3wpWwALh80IHAiZqWYzw/index.html", file)); }