List of usage examples for com.amazonaws.services.s3.model CannedAccessControlList PublicRead
CannedAccessControlList PublicRead
To view the source code for com.amazonaws.services.s3.model CannedAccessControlList PublicRead.
Click Source Link
From source file:S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { // s3// w w w. j av a2 s. c o m String imageBucketName = "ringtone_image"; String ringBucketName = "ringtone_ring"; AmazonS3 s3 = new AmazonS3Client( new PropertiesCredentials(S3Sample.class.getResourceAsStream("AwsCredentials.properties"))); String bucketName; System.out.println("Start Sending Files To Amazon S3"); // directory and files String pathName = "/home/liutao/workspace/python/1-fetch/download_Holiday/"; File dir = new File(pathName); File list[] = dir.listFiles(); String fileName; // sort by record index Arrays.sort(list, new Comparator<File>() { public int compare(File f1, File f2) { String s1 = f1.getName(); String s2 = f2.getName(); int idx1 = s1.indexOf(".xml"); int idx2 = s2.indexOf(".xml"); if (idx1 != -1 && idx2 != -1) { int num1 = Integer.parseInt(s1.substring(s1.indexOf('d') + 1, idx1)); int num2 = Integer.parseInt(s2.substring(s2.indexOf('d') + 1, idx2)); return num1 - num2; } else if (idx1 == -1) return -1; else return 1; } }); // xml parser DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); InputStream in; // String uuid; String key; URL url; int i = 0, j; try { DocumentBuilder docBuilder = factory.newDocumentBuilder(); FileWriter log = new FileWriter(pathName + "log"); while (i < list.length) { if (list[i].getName().endsWith(".xml")) break; i++; } // start process xml file for (; i < list.length; i++) { System.out.println(list[i].getName()); log.write(list[i].getName() + ","); try { in = new FileInputStream(pathName + list[i].getName()); Document doc = docBuilder.parse(in); org.w3c.dom.Element root = doc.getDocumentElement(); //System.out.println(root.getNodeName()); NodeList childen = root.getChildNodes(); org.w3c.dom.Node curNode, node; uuid = UUID.randomUUID().toString(); for (j = 0; j < childen.getLength(); j++) { curNode = childen.item(j); if (curNode.getNodeType() == Node.ELEMENT_NODE) if (curNode.getNodeName().equals("Ring") || curNode.getNodeName().equals("Image")) { //System.out.println(curNode.getFirstChild().getNodeValue()); fileName = curNode.getFirstChild().getNodeValue(); File file = new File(pathName + fileName); if (!file.exists()) { log.write(fileName + " not Found!\n"); break; } key = uuid + fileName; //bucketName = curNode.getNodeName().equals("Ring")?ringBucketName:imageBucketName; bucketName = "ringtone_test_2010"; try { s3.putObject(new PutObjectRequest(bucketName, key, file)); // s3.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead); // ?? // url = s3.generatePresignedUrl(bucketName, key, expireDate); // System.out.println(url); } catch (AmazonServiceException ase) { log.write("AmazonServiceException\n"); } catch (AmazonClientException ace) { log.write("AmazonClientException\n"); } } } if (j == childen.getLength()) {// if success, record it log.write("uuid:" + uuid + "\n"); } } catch (FileNotFoundException e) { log.write("FileNotFound\n"); } catch (SAXException e) { log.write("xml parse error\n"); } catch (IOException e) { log.write("IOexception\n"); } } log.flush(); log.close(); } catch (ParserConfigurationException e) { e.printStackTrace(); } System.out.println("Sending Finished!"); }
From source file:PutTestFile.java
License:Apache License
public static void main(String[] args) throws IOException { /*/*from w w w. jav a 2 s .c o m*/ * 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 PropertiesCredentials( PutTestFile.class.getResourceAsStream("AwsCredentials.properties"))); String bucketName = "dtccTest"; String key = "largerfile.txt"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); System.out.println("Uploading a new object to S3 from a file\n"); PutObjectRequest request = new PutObjectRequest(bucketName, key, createSampleFile()); request.setCannedAcl(CannedAccessControlList.PublicRead); s3.putObject(request); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, whichmeans your request made it " + "to Amazon S3, but was rejected with an errorresponse 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, whichmeans the client encountered " + "a serious internal problem while trying tocommunicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:PutFile.java
License:Apache License
public static void main(String[] args) throws IOException { /*/* w ww .j av a 2 s .c o m*/ * 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 PropertiesCredentials( PutFile.class.getResourceAsStream("AwsCredentials.properties"))); String bucketName = "dtccTest"; System.out.println("==========================================="); System.out.println(" Amazon S3 Test"); System.out.println("===========================================\n"); try { /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); String fileName = "S3HWTest.zip"; File file = new File(fileName); System.out.println("Uploading a new object to S3 from a file " + fileName + "\n"); System.out.println("file.length() " + file.length() + "\n"); long start = System.currentTimeMillis(); PutObjectRequest request = new PutObjectRequest(bucketName, fileName, file); request.setCannedAcl(CannedAccessControlList.PublicRead); s3.putObject(request); System.out.println("Uploading done in " + (System.currentTimeMillis() - start) + " ms.\n"); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, whichmeans your request made it " + "to Amazon S3, but was rejected with an errorresponse 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, whichmeans the client encountered " + "a serious internal problem while trying tocommunicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:AmazonS3Handler.java
License:Open Source License
public void putObject(File file, String key) throws IOException { try {//w w w . j av a 2 s .c o m // System.out.println("Creating bucket " + bucketName + "\n"); // s3.createBucket(bucketName); // System.out.println("Listing buckets"); // for (Bucket bucket : s3.listBuckets()) { // System.out.println(" - " + bucket.getName()); // } System.out.println(); System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject( new PutObjectRequest(bucketName, key, file).withCannedAcl(CannedAccessControlList.PublicRead)); } 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:awslabs.lab21.SolutionCode.java
License:Open Source License
@Override public void makeObjectPublic(AmazonS3 s3Client, String bucketName, String key) { // Use the setObjectAcl method of the s3Client object to set the ACL for the specified // object to CannedAccessControlList.PublicRead. s3Client.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead); }
From source file:biz.k11i.S3GyazoController.java
License:Open Source License
@RequestMapping(value = "/upload.cgi", method = RequestMethod.POST) @ResponseBody//from ww w . java2 s. c om String upload(@RequestParam("imagedata") MultipartFile imagedata) throws IOException { if (imagedata.isEmpty()) { String message = "????????"; logger.warn(message); throw new BadRequestException(message); } byte[] bytes = imagedata.getBytes(); String hash = generateHash(bytes); String filename = String.format("%s.png", hash); try (InputStream input = imagedata.getInputStream()) { ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType("image/png"); objectMetadata.setContentLength(bytes.length); PutObjectRequest req = new PutObjectRequest(bucket, filename, input, objectMetadata) .withCannedAcl(CannedAccessControlList.PublicRead); amazonS3Client.putObject(req); } String result = urlPrefix + filename; logger.info("New image uploaded {}", result); return result; }
From source file:br.puc_rio.ele.lvc.interimage.common.udf.ROIStorage.java
License:Apache License
/** * Method invoked on every tuple during foreach evaluation. * @param input tuple<br>/*from w w w . j a v a2 s . c o m*/ * first column is assumed to have the geometry<br> * second column is assumed to have the class name<br> * third column is assumed to have the output path * @exception java.io.IOException * @return true if successful, false otherwise */ @Override public Boolean exec(Tuple input) throws IOException { if (input == null || input.size() < 3) return null; try { Object objGeometry = input.get(0); Geometry geometry = _geometryParser.parseGeometry(objGeometry); String className = DataType.toString(input.get(1)); String path = DataType.toString(input.get(2)); AWSCredentials credentials = new BasicAWSCredentials(_accessKey, _secretKey); AmazonS3 conn = new AmazonS3Client(credentials); conn.setEndpoint("https://s3.amazonaws.com"); /*File temp = File.createTempFile(className, ".wkt"); // Delete temp file when program exits. temp.deleteOnExit(); BufferedWriter out = new BufferedWriter(new FileWriter(temp)); out.write(new WKTWriter().write(geometry)); out.close();*/ /* File temp = File.createTempFile(className, ".wkt.snappy"); temp.deleteOnExit();*/ String geom = new WKTWriter().write(geometry); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream snappyOut = new SnappyOutputStream(out); snappyOut.write(geom.getBytes()); snappyOut.close(); /*PutObjectRequest putObjectRequest = new PutObjectRequest(_bucket, path + className + ".wkt.snappy", temp); putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all*/ PutObjectRequest putObjectRequest = new PutObjectRequest(_bucket, path + className + ".wkts", new ByteArrayInputStream(out.toByteArray()), new ObjectMetadata()); putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all TransferManager tx = new TransferManager(credentials); tx.upload(putObjectRequest); return true; } catch (Exception e) { throw new IOException("Caught exception processing input row ", e); } }
From source file:br.puc_rio.ele.lvc.interimage.core.datamanager.AWSSource.java
License:Apache License
public void put(String from, String to, Resource resource) { try {//from w w w . ja v a 2s . c om File file = new File(from); PutObjectRequest putObjectRequest = new PutObjectRequest(_bucket, to, file); if (resource instanceof SplittableResource) { SplittableResource rsrc = (SplittableResource)resource; if (rsrc.getType() == SplittableResource.IMAGE) { putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all } } else if (resource instanceof DefaultResource) { DefaultResource rsrc = (DefaultResource)resource; if (rsrc.getType() == DefaultResource.TILE) { putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all } else if (rsrc.getType() == DefaultResource.FUZZY_SET) { putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all } else if (rsrc.getType() == DefaultResource.SHAPE) { putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all } } Upload upload = _manager.upload(putObjectRequest); upload.waitForCompletion(); System.out.println("AWSSource: Uploaded file - " + to); } catch (Exception e) { System.err.println("Source put failed: " + e.getMessage()); } }
From source file:br.puc_rio.ele.lvc.interimage.core.datamanager.AWSSource.java
License:Apache License
public void put(String from, String to, Resource resource) { try {// ww w . j a v a2 s. c om File file = new File(from); PutObjectRequest putObjectRequest = new PutObjectRequest(_bucket, to, file); if (resource instanceof SplittableResource) { SplittableResource rsrc = (SplittableResource) resource; if (rsrc.getType() == SplittableResource.IMAGE) { putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all } } else if (resource instanceof DefaultResource) { DefaultResource rsrc = (DefaultResource) resource; if (rsrc.getType() == DefaultResource.TILE) { putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all } else if (rsrc.getType() == DefaultResource.FUZZY_SET) { putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all } else if (rsrc.getType() == DefaultResource.SHAPE) { putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all } } Upload upload = _manager.upload(putObjectRequest); upload.waitForCompletion(); System.out.println("AWSSource: Uploaded file - " + to); } catch (Exception e) { System.err.println("Source put failed: " + e.getMessage()); } }
From source file:ch.admin.isb.hermes5.persistence.s3.S3RemoteAdapter.java
License:Apache License
@Override @Asynchronous//w w w . j av a 2 s. c o m @Logged public Future<Void> addFile(InputStream file, long size, String path) { try { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(size); metadata.setContentType(mimeTypeUtil.getMimeType(path)); s3.putObject(new PutObjectRequest(bucketName.getStringValue(), path, file, metadata) .withCannedAcl(CannedAccessControlList.PublicRead)); return new AsyncResult<Void>(null); } catch (Exception e) { throw new RuntimeException(e); } finally { if (file != null) { try { file.close(); } catch (IOException e) { } } } }