List of usage examples for com.amazonaws.services.s3 AmazonS3 getObject
public S3Object getObject(GetObjectRequest getObjectRequest) throws SdkClientException, AmazonServiceException;
Gets the object stored in Amazon S3 under the specified bucket and key.
From source file:org.jdamico.s3.components.S3Component.java
License:Apache License
public boolean isValidFile(AppProperties appProperties, String keyName) throws TopLevelException { AmazonS3 s3client = getS3Client(appProperties); boolean isValidFile = true; try {/* w w w.ja va2 s .com*/ S3Object object = s3client.getObject(new GetObjectRequest(appProperties.getBucketnName(), keyName)); ObjectMetadata objectMetadata = object.getObjectMetadata(); } catch (AmazonS3Exception s3e) { if (s3e.getStatusCode() == 404) { isValidFile = false; } else { throw new TopLevelException(appProperties, s3e); } } return isValidFile; }
From source file:org.p365.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*from ww w. j a v a 2 s . c om*/ * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. * * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider()); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "mynewbuket"; String key = "Myobj/sd.jpg"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); if (!s3.doesBucketExist(bucketName)) { s3.createBucket(bucketName); } /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); String pathname = "D:\\Program Files\\apache-tomcat-7.0.42\\webapps\\WorkerForP365\\src\\AAA_1465.jpg"; File file = new File(pathname); s3.putObject( new PutObjectRequest(bucketName, key, file).withCannedAcl(CannedAccessControlList.PublicRead)); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ //System.out.println("Deleting an object\n"); //s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ //System.out.println("Deleting bucket " + bucketName + "\n"); //s3.deleteBucket(bucketName); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:org.rdswitchboard.importers.browser.s3.App.java
License:Open Source License
public static void main(String[] args) { try {/* w ww . j av a2 s . co m*/ if (args.length == 0 || StringUtils.isNullOrEmpty(args[0])) throw new Exception("Please provide properties file"); String propertiesFile = args[0]; Properties properties = new Properties(); try (InputStream in = new FileInputStream(propertiesFile)) { properties.load(in); } String source = properties.getProperty("data.source.id"); if (StringUtils.isNullOrEmpty(source)) throw new IllegalArgumentException("Source can not be empty"); System.out.println("Source: " + source); String baseUrl = properties.getProperty("base.url"); if (StringUtils.isNullOrEmpty(baseUrl)) throw new IllegalArgumentException("Base URL can not be empty"); System.out.println("Base URL: " + baseUrl); String sessionId = properties.getProperty("session.id"); if (StringUtils.isNullOrEmpty(sessionId)) throw new IllegalArgumentException("Session Id can not be empty"); System.out.println("Session Id: " + sessionId); String accessKey = properties.getProperty("aws.access.key"); String secretKey = properties.getProperty("aws.secret.key"); String bucket = properties.getProperty("s3.bucket"); if (StringUtils.isNullOrEmpty(bucket)) throw new IllegalArgumentException("AWS S3 Bucket can not be empty"); System.out.println("S3 Bucket: " + bucket); String prefix = properties.getProperty("s3.prefix"); if (StringUtils.isNullOrEmpty(prefix)) throw new IllegalArgumentException("AWS S3 Prefix can not be empty"); System.out.println("S3 Prefix: " + prefix); String crosswalk = properties.getProperty("crosswalk"); Templates template = null; if (!StringUtils.isNullOrEmpty(crosswalk)) { System.out.println("Crosswalk: " + crosswalk); template = TransformerFactory.newInstance() .newTemplates(new StreamSource(new FileInputStream(crosswalk))); } ObjectMapper mapper = new ObjectMapper(); Client client = Client.create(); Cookie cookie = new Cookie("PHPSESSID", properties.getProperty("session")); AmazonS3 s3client; if (!StringUtils.isNullOrEmpty(accessKey) && !StringUtils.isNullOrEmpty(secretKey)) { System.out.println( "Connecting to AWS via Access and Secret Keys. This is not safe practice, consider to use IAM Role instead."); AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); s3client = new AmazonS3Client(awsCredentials); } else { System.out.println("Connecting to AWS via Instance Profile Credentials"); s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider()); } //String file = "rda/rif/class:collection/54800.xml"; ListObjectsRequest listObjectsRequest; ObjectListing objectListing; String file = prefix + "/latest.txt"; S3Object object = s3client.getObject(new GetObjectRequest(bucket, file)); String latest; try (InputStream txt = object.getObjectContent()) { latest = prefix + "/" + IOUtils.toString(txt, StandardCharsets.UTF_8).trim() + "/"; } System.out.println("S3 Repository: " + latest); listObjectsRequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(latest); do { objectListing = s3client.listObjects(listObjectsRequest); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { file = objectSummary.getKey(); System.out.println("Processing file: " + file); object = s3client.getObject(new GetObjectRequest(bucket, file)); String xml = null; if (null != template) { Source reader = new StreamSource(object.getObjectContent()); StringWriter writer = new StringWriter(); Transformer transformer = template.newTransformer(); transformer.transform(reader, new StreamResult(writer)); xml = writer.toString(); } else { InputStream is = object.getObjectContent(); xml = IOUtils.toString(is, ENCODING); } URL url = new URL(baseUrl + "/registry/import/import_s3/"); StringBuilder sb = new StringBuilder(); addParam(sb, "id", source); addParam(sb, "xml", xml); //System.out.println(sb.toString()); WebResource webResource = client.resource(url.toString()); ClientResponse response = webResource .header("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0") .accept(MediaType.APPLICATION_JSON, "*/*").acceptLanguage("en-US", "en") .type(MediaType.APPLICATION_FORM_URLENCODED).cookie(cookie) .post(ClientResponse.class, sb.toString()); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } String output = response.getEntity(String.class); Result result = mapper.readValue(output, Result.class); if (!result.getStatus().equals("OK")) { System.err.println(result.getMessage()); break; } else System.out.println(result.getMessage()); } listObjectsRequest.setMarker(objectListing.getNextMarker()); } while (objectListing.isTruncated()); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.rdswitchboard.tests.crosswalk.App.java
License:Open Source License
public static void main(String[] args) { try {//from ww w.j av a2s . c om String propertiesFile = PROPERTIES_FILE; if (args.length != 0 && !StringUtils.isNullOrEmpty(args[0])) propertiesFile = args[0]; Properties properties = new Properties(); try (InputStream in = new FileInputStream(propertiesFile)) { properties.load(in); } String accessKey = properties.getProperty("aws.access.key"); String secretKey = properties.getProperty("aws.secret.key"); String bucket = properties.getProperty("s3.bucket"); if (StringUtils.isNullOrEmpty(bucket)) throw new IllegalArgumentException("AWS S3 Bucket can not be empty"); System.out.println("S3 Bucket: " + bucket); String key = properties.getProperty("s3.key"); if (StringUtils.isNullOrEmpty(key)) throw new IllegalArgumentException("AWS S3 Key can not be empty"); System.out.println("S3 Key: " + key); String crosswalk = properties.getProperty("crosswalk"); if (StringUtils.isNullOrEmpty(crosswalk)) throw new IllegalArgumentException("Crosswalk can not be empty"); System.out.println("Crosswalk: " + crosswalk); String outFileName = properties.getProperty("out", OUT_FILE_NAME); System.out.println("Out: " + outFileName); AmazonS3 s3client; if (!StringUtils.isNullOrEmpty(accessKey) && !StringUtils.isNullOrEmpty(secretKey)) { System.out.println( "Connecting to AWS via Access and Secret Keys. This is not safe practice, consider to use IAM Role instead."); AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); s3client = new AmazonS3Client(awsCredentials); } else { System.out.println("Connecting to AWS via Instance Profile Credentials"); s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider()); } S3Object object = s3client.getObject(new GetObjectRequest(bucket, key)); Templates template = TransformerFactory.newInstance() .newTemplates(new StreamSource(new FileInputStream(crosswalk))); StreamSource reader = new StreamSource(object.getObjectContent()); StreamResult result = (StringUtils.isNullOrEmpty(outFileName) || outFileName.equals("stdout")) ? new StreamResult(System.out) : new StreamResult(new FileOutputStream(outFileName)); Transformer transformer = template.newTransformer(); transformer.transform(reader, result); /* DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); TransformerFactory tFactory = TransformerFactory.newInstance(); XPath xPath = XPathFactory.newInstance().newXPath(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document document = builder.parse(object.getObjectContent()); Transformer transformer1 = tFactory.newTemplates( new StreamSource(new FileInputStream(crosswalk))).newTransformer(); Transformer transformer2 = tFactory.newTransformer(); NodeList metadata = (NodeList)xPath.evaluate("/OAI-PMH/ListRecords/record/metadata", document.getDocumentElement(), XPathConstants.NODESET); for (int i = 0; i < metadata.getLength(); ++i) { System.out.println("Converting node: " + i); Element e = (Element) metadata.item(i); Node mets = e.getElementsByTagName("mets").item(0); Node rifcs = document.createElement("registryObjects"); DOMSource xmlSource = new DOMSource(mets); DOMResult xmlResult = new DOMResult(rifcs); transformer1.transform(xmlSource, xmlResult); e.removeChild(mets); e.appendChild(xmlResult.getNode()); // e.replaceChild(rifcs, xmlResult.getNode()); } StreamResult result = (StringUtils.isNullOrEmpty(outFileName) || outFileName.equals("stdout")) ? new StreamResult(System.out) : new StreamResult(new FileOutputStream(outFileName)); transformer2.transform(new DOMSource(document), result); */ } catch (Exception e) { e.printStackTrace(); } }
From source file:org.rdswitchboard.utils.s3.find.App.java
License:Open Source License
public static void main(String[] args) { try {//from w w w . ja v a 2 s . c om if (args.length != 2) throw new IllegalArgumentException("Bucket name and search string can not be empty"); String buckey = args[0]; String search = args[1]; String prefix = null; int pos = buckey.indexOf('/'); if (pos > 0) { prefix = buckey.substring(pos + 1); buckey = buckey.substring(0, pos); } AmazonS3 s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider()); // AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(buckey); if (!StringUtils.isNullOrEmpty(prefix)) listObjectsRequest.setPrefix(prefix); ObjectListing objectListing; do { objectListing = s3client.listObjects(listObjectsRequest); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { String key = objectSummary.getKey(); System.out.println(" - " + key); S3Object object = s3client.getObject(new GetObjectRequest(buckey, key)); String str = IOUtils.toString(object.getObjectContent()); if (str.contains(search)) { System.out.println("Found!"); FileUtils.writeStringToFile(new File("s3/" + key), str); } } listObjectsRequest.setMarker(objectListing.getNextMarker()); } while (objectListing.isTruncated()); } catch (Exception e) { e.printStackTrace(); } }
From source file:oulib.aws.s3.S3TiffProcessor.java
/** * * @param bookInfo : contains the information of the source bucket name, target bucket name, and the name of the book * @param context : lambda function runtime context * @return ://w ww .jav a 2s . com * */ @Override public String handleRequest(S3BookInfo bookInfo, Context context) { AmazonS3 s3client = new AmazonS3Client(); Region usEast = Region.getRegion(Regions.US_EAST_1); s3client.setRegion(usEast); try { String sourceBucketName = bookInfo.getBucketSourceName(); String targetBucketName = bookInfo.getBucketTargetName(); String bookName = bookInfo.getBookName(); // Every book has a folder in the target bucket: Map targetBucketKeyMap = S3Util.getBucketObjectKeyMap(targetBucketName, bookName, s3client); if (!S3Util.folderExitsts(bookName, targetBucketKeyMap)) { S3Util.createFolder(targetBucketName, bookName, s3client); } final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(sourceBucketName) .withPrefix(bookName + "/data/"); ListObjectsV2Result result; do { result = s3client.listObjectsV2(req); for (S3ObjectSummary objectSummary : result.getObjectSummaries()) { String key = objectSummary.getKey(); if (key.endsWith(".tif") && !targetBucketKeyMap.containsKey(key + ".tif")) { S3Object object = s3client.getObject(new GetObjectRequest(sourceBucketName, key)); System.out.println("Start to generate smaller tif image for the object " + key); S3Util.generateSmallTiffWithTargetSize(s3client, object, targetBucketName, bookInfo.getCompressionSize()); // S3Util.copyS3ObjectTiffMetadata(s3client, object, s3client.getObject(new GetObjectRequest(targetBucketName, key)), targetBucketName, key+".tif"); System.out.println("Finished to generate smaller tif image for the object " + key + ".tif"); // break; } } System.out.println("Next Continuation Token : " + result.getNextContinuationToken()); req.setContinuationToken(result.getNextContinuationToken()); } while (result.isTruncated() == true); } catch (AmazonServiceException ase) { System.out.println( "Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println( "Caught an AmazonClientException, which means the client encountered an internal error while trying to communicate with S3, \nsuch as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } return null; }
From source file:oulib.aws.s3.S3Util.java
public static PutObjectResult generateSmallTiff(AmazonS3 s3client, String sourceBucketName, String sourceKey, String targetBucketName, String targetKey, double compressionRate) { S3Object s3Object = s3client.getObject(new GetObjectRequest(sourceBucketName, sourceKey)); return generateSmallTiff(s3client, s3Object, targetBucketName, targetKey, compressionRate); }
From source file:oulib.aws.s3.S3Util.java
/** * Pull out Tiff metadata from input S3 object and inject into the * content of target S3 Object;<br> * Generate the new output S3 object that has the metadata from input object. * //from w w w . ja va 2 s .c o m * @param s3client : S3 client * @param sourceBucketName : Input bucket name * @param targetBucketName : Output bucket name * @param sourceKey : Input object key * @param targetKey : Output object key * * @return PutObjectResult */ public static PutObjectResult copyS3ObjectTiffMetadata(AmazonS3 s3client, String sourceBucketName, String targetBucketName, String sourceKey, String targetKey) { S3Object obj1 = s3client.getObject(new GetObjectRequest(sourceBucketName, sourceKey)); S3Object obj2 = s3client.getObject(new GetObjectRequest(targetBucketName, targetKey)); return copyS3ObjectTiffMetadata(s3client, obj1, obj2); }
From source file:oulib.aws.s3.S3Util.java
public static void generateTifDerivativesByS3Bucket(AmazonS3 s3client, S3BookInfo bookInfo) { String sourceBucketName = bookInfo.getBucketSourceName(); String targetBucketName = bookInfo.getBucketTargetName(); String bookName = bookInfo.getBookName(); try {/* w ww. ja v a2s .co m*/ // Every book has a folder in the target bucket: Map targetBucketKeyMap = S3Util.getBucketObjectKeyMap(targetBucketName, bookName, s3client); if (!S3Util.folderExitsts(bookName, targetBucketKeyMap)) { S3Util.createFolder(targetBucketName, bookName, s3client); } final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(sourceBucketName) .withPrefix(bookName + "/data/"); ListObjectsV2Result result; do { result = s3client.listObjectsV2(req); for (S3ObjectSummary objectSummary : result.getObjectSummaries()) { String key = objectSummary.getKey(); if (key.contains(".tif") && (key.contains("047") || key.contains("049") || key.contains("054")) && !targetBucketKeyMap.containsKey(key + ".tif")) { S3Object object = s3client.getObject(new GetObjectRequest(sourceBucketName, key)); System.out.println("Start to generate smaller tif image for the object " + key + "\n"); S3Util.generateSmallTiffWithTargetSize(s3client, object, targetBucketName, bookInfo.getCompressionSize()); // S3Util.copyS3ObjectTiffMetadata(s3client, object, s3client.getObject(new GetObjectRequest(targetBucketName, key)), targetBucketName, key+".tif"); System.out.println("Finished to generate smaller tif image for the object " + key + "\n"); // break; } } System.out.println("Next Continuation Token : " + result.getNextContinuationToken() + "\n"); req.setContinuationToken(result.getNextContinuationToken()); } while (result.isTruncated() == true); } catch (AmazonServiceException ase) { System.out.println( "Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.\n"); System.out.println("Error Message: " + ase.getMessage() + "\n"); System.out.println("HTTP Status Code: " + ase.getStatusCode() + "\n"); System.out.println("AWS Error Code: " + ase.getErrorCode() + "\n"); System.out.println("Error Type: " + ase.getErrorType() + "\n"); System.out.println("Request ID: " + ase.getRequestId() + "\n"); } catch (AmazonClientException ace) { System.out.println( "Caught an AmazonClientException, which means the client encountered an internal error while trying to communicate with S3, \nsuch as not being able to access the network.\n"); System.out.println("Error Message: " + ace.getMessage() + "\n"); } }
From source file:pl.pawlik.cymes.controllers.FormController.java
@RequestMapping(value = "/plik/{nazwa_pliku}", method = RequestMethod.GET) public void getFile(@PathVariable("nazwa_pliku") String nazwaPliku, HttpServletResponse response) { try {//from ww w .java 2s. c o m AmazonS3 s3client = new AmazonS3Client( new BasicAWSCredentials("AKIAJJ4WVVITDZF4SU4A", "oTNfuCbjK/Q/BnBEdQCAs6ogIpoRmbvROAjtdtXV")); S3Object object = s3client.getObject( new GetObjectRequest("pawliktest", "/uploads/upload_2d6303ef-a714-4fa6-9137-fc8b22412730")); InputStream is = object.getObjectContent(); response.setContentType("image/jpeg"); org.apache.commons.io.IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (Exception ex) { ex.printStackTrace(); } }