List of usage examples for com.amazonaws.services.s3 AmazonS3 setObjectAcl
public void setObjectAcl(String bucketName, String key, CannedAccessControlList acl) throws SdkClientException, AmazonServiceException;
Sets the CannedAccessControlList for the specified object in Amazon S3 using one of the pre-configured CannedAccessControlLists
.
From source file:S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { // s3//from w w w . j av a 2 s .c om 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:Uploader.java
License:Open Source License
private static void uploadFile(AmazonS3 s3, String bucketName, String key, File file) { if (!file.exists()) { System.out.println("File does not exist: " + file.getAbsolutePath()); return;//from w w w . ja v a 2 s . co m } /* * 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. */ try { System.out.println(file.getAbsolutePath() + " ---> " + key + "\n"); s3.putObject(new PutObjectRequest(bucketName, key, file)); // Change permissions. Grant all users the read permission. AccessControlList acl = s3.getObjectAcl(bucketName, key); Permission permission = Permission.Read; Grantee grantee = GroupGrantee.AllUsers; acl.grantPermission(grantee, permission); s3.setObjectAcl(bucketName, key, acl); } 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:aws.example.s3.SetAcl.java
License:Open Source License
public static void setObjectAcl(String bucket_name, String object_key, String email, String access) { System.out.format("Setting %s access for %s\n", access, email); System.out.println("for object: " + object_key); System.out.println(" in bucket: " + bucket_name); final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try {/*ww w . j a va 2 s . c om*/ // get the current ACL AccessControlList acl = s3.getObjectAcl(bucket_name, object_key); // set access for the grantee EmailAddressGrantee grantee = new EmailAddressGrantee(email); Permission permission = Permission.valueOf(access); acl.grantPermission(grantee, permission); s3.setObjectAcl(bucket_name, object_key, acl); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } }
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:cloudExplorer.Acl.java
License:Open Source License
void setACLpublic(String object, String access_key, String secret_key, String endpoint, String bucket) { try {//from w w w. ja v a 2s. c o m AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration().withSignerOverride("S3SignerType")); s3Client.setEndpoint(endpoint); s3Client.setObjectAcl(bucket, object, CannedAccessControlList.PublicRead); } catch (Exception setACLpublic) { mainFrame.jTextArea1.append("\nException occurred in ACL"); } }
From source file:cloudExplorer.Acl.java
License:Open Source License
void setACLprivate(String object, String access_key, String secret_key, String endpoint, String bucket) { try {/*ww w.j av a 2s .c o m*/ AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration().withSignerOverride("S3SignerType")); s3Client.setEndpoint(endpoint); s3Client.setObjectAcl(bucket, object, CannedAccessControlList.Private); } catch (Exception setACLprivate) { mainFrame.jTextArea1.append("\nException occurred in setACLprivate"); } }
From source file:iit.edu.supadyay.s3.S3upload.java
public static boolean upload(String bucketName, String uploadFileName, String keyName) throws IOException, InterruptedException { //access = "AKIAJ2YSLRUZR5B3F5HQ"; //secret = "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"; //AWSCredentials credentials = new BasicAWSCredentials(access, secret); //AmazonS3 s3client = new AmazonS3Client(getCredentials()); AmazonS3 s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider()); try {/*from w w w.j av a2 s.com*/ System.out.println("Uploading a new object to S3 from a file\n"); File file = new File(uploadFileName); System.out.println("I am before here\n"); s3client.createBucket(bucketName); System.out.println("I am here\n"); s3client.putObject(new PutObjectRequest(bucketName, keyName, file)); s3client.setObjectAcl(bucketName, keyName, CannedAccessControlList.PublicReadWrite); } 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()); return false; } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); return false; } return true; }
From source file:org.alanwilliamson.amazon.s3.SetAcl.java
License:Open Source License
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException { AmazonKey amazonKey = getAmazonKey(_session, argStruct); AmazonS3 s3Client = getAmazonS3(amazonKey); String bucket = getNamedStringParam(argStruct, "bucket", null); String key = getNamedStringParam(argStruct, "key", null); if (key != null && key.charAt(0) == '/') key = key.substring(1);//from ww w. java2 s . com CannedAccessControlList acl = amazonKey.getAmazonCannedAcl(getNamedStringParam(argStruct, "acl", null)); try { s3Client.setObjectAcl(bucket, key, acl); } catch (Exception e) { throwException(_session, "AmazonS3: " + e.getMessage()); } return cfBooleanData.TRUE; }
From source file:org.reswitchboard.utils.s3.access.App.java
License:Open Source License
public static void main(String[] args) { try {// w w w. j av a 2 s. c o m if (args.length == 0 || StringUtils.isNullOrEmpty(args[0])) throw new IllegalArgumentException("Bucket name can not be empty"); String bucketName = args[0]; String prefix = null; if (args.length > 1) prefix = args[1]; AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName); 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); for (int nAttempt = 1;; ++nAttempt) { try { AccessControlList acl = s3client.getObjectAcl(bucketName, key); List<Grant> grants = acl.getGrantsAsList(); for (Grant grant : grants) { // System.out.println( " Grant: " + grant.toString()); if (grant.getGrantee().equals(GroupGrantee.AllUsers)) { System.out.println(" Revoking public access"); acl.revokeAllPermissions(GroupGrantee.AllUsers); s3client.setObjectAcl(bucketName, key, acl); break; } } break; } catch (Exception e) { System.out.println("Error: " + e.toString()); if (nAttempt >= 10) { throw new Exception("Maximum number of invalid attempts has been reeched"); } // double back-off delay Thread.sleep((long) (Math.pow(2, nAttempt) * 50)); } } } listObjectsRequest.setMarker(objectListing.getNextMarker()); } while (objectListing.isTruncated()); } catch (Exception e) { e.printStackTrace(); } }
From source file:rg.ent.S3SampleB.java
License:Open Source License
public static void copyFileBtweenBuckets() { AWSCredentials credentials = null;// w w w .j a va 2 s. c om try { credentials = new ProfileCredentialsProvider("default").getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (/Users/john/.aws/credentials), and is in valid format.", e); } String file_name = "640_262098533.JPG"; String source_bucket = "royaltygroupimages"; String dest_bucket = "royaltygroupupload"; AmazonS3 s3 = new AmazonS3Client(credentials); CopyObjectRequest cor = new CopyObjectRequest(source_bucket, file_name, dest_bucket, file_name); s3.copyObject(cor); s3.setObjectAcl(dest_bucket, file_name, CannedAccessControlList.PublicRead); //S3Object[] filteredObjects = s3Service.listObjects("sourceBucket", "appData/", null); //for(S3Object object: filteredObjects ){ // s3Service.copyObject("sourceBucket", "newAppData/" + object.getKey().substring(object.getKey().indexOf("/"), "destBucket", object, false); //} }