List of usage examples for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client
@SdkInternalApi AmazonS3Client(AmazonS3ClientParams s3ClientParams)
From source file:GetTestFile.java
License:Apache License
public static void main(String[] args) throws IOException { /*// w ww. ja va 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( GetTestFile.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("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); System.out.println("ContentEncod: " + object.getObjectMetadata().getContentEncoding()); System.out.println("ContentLengt: " + object.getObjectMetadata().getContentLength()); System.out.println("ContentDisp: " + object.getObjectMetadata().getContentDisposition()); displayTextInputStream(object.getObjectContent()); } 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:S3TransferProgressSample.java
License:Open Source License
public static void main(String[] args) throws Exception { /*//from w w w . j a v a 2 s .c om * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (/Users/zunzunwang/.aws/credentials). * * TransferManager manages a pool of threads, so we create a * single instance and share it throughout our application. */ 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/zunzunwang/.aws/credentials), and is in valid format.", e); } AmazonS3 s3 = new AmazonS3Client(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); tx = new TransferManager(s3); bucketName = "s3-upload-sdk-sample-" + credentials.getAWSAccessKeyId().toLowerCase(); new S3TransferProgressSample(); }
From source file:AmazonS3Handler.java
License:Open Source License
public AmazonS3Handler() throws IOException { /*/*from w w w . j a v a 2s.c o m*/ * 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 */ s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider()); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); }
From source file:Encoder.java
License:Open Source License
/** * Amazon cloud credential submitter method (The real shit in the world of programming when you don't have anything to write for a function!). * /*from w ww.j av a 2 s. c o m*/ * @throws Exception */ private static void getAmazonCloud() throws Exception { credentials = new PropertiesCredentials( Encoder.class.getResourceAsStream("settings/" + "AwsCredentials.properties")); ec2 = new AmazonEC2Client(credentials); s3 = new AmazonS3Client(credentials); }
From source file:Uploader.java
License:Open Source License
public static void main(String[] args) throws IOException { if (args.length != 2) { System.out.println("Usage: bucket_name properties_file"); System.out.println("Please specify the bucket name and the properties file to process."); System.out.println(/*from w w w.j a v a 2s .c o m*/ "The properties file contains the key for S3 as the key and absolute file path as the value."); System.exit(0); } /* * 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(Uploader.class.getResourceAsStream("AwsCredentials.properties"))); String bucketName = args[0]; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); Properties files = new Properties(); InputStream in = new FileInputStream(new File(args[1])); files.load(in); in.close(); Set keys = files.keySet(); for (Object key : keys) { uploadFile(s3, bucketName, (String) key, new File(files.getProperty((String) key))); } }
From source file:AbstractAmazonKinesisFirehoseDelivery.java
License:Open Source License
/** * Method to initialize the clients using the specified AWSCredentials. * * @param Exception//w w w . j a va 2 s . c o m */ protected static void initClients() throws Exception { /* * The ProfileCredentialsProvider will return your [default] credential * profile by reading from the credentials file located at * (~/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().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 (~/.aws/credentials), and is in valid format.", e); } // S3 client s3Client = new AmazonS3Client(credentials); Region s3Region = RegionUtils.getRegion(s3RegionName); s3Client.setRegion(s3Region); // Firehose client firehoseClient = new AmazonKinesisFirehoseClient(credentials); firehoseClient.setRegion(RegionUtils.getRegion(firehoseRegion)); // IAM client iamClient = new AmazonIdentityManagementClient(credentials); iamClient.setRegion(RegionUtils.getRegion(iamRegion)); }
From source file:Assignment1.java
License:Open Source License
public static void main(String[] args) throws Exception { AWSCredentials credentials = new PropertiesCredentials( Assignment1.class.getResourceAsStream("AwsCredentials.properties")); /********************************************* * #1 Create Amazon Client object// w ww . j ava 2s .c o m **********************************************/ ec2 = new AmazonEC2Client(credentials); // We assume that we've already created an instance. Use the id of the instance. //String instanceId = "i-4e6c2a3d"; //put your own instance id to test this code. try { CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest(); createSecurityGroupRequest.withGroupName("mini").withDescription("My Java Security Group"); CreateSecurityGroupResult createSecurityGroupResult = ec2 .createSecurityGroup(createSecurityGroupRequest); IpPermission ipPermission = new IpPermission(); ipPermission.withIpRanges("0.0.0.0/0", "150.150.150.150/32").withIpProtocol("tcp").withFromPort(22) .withToPort(22); AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest(); authorizeSecurityGroupIngressRequest.withGroupName("mini").withIpPermissions(ipPermission); ec2.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest); CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest(); createKeyPairRequest.withKeyName("E3instance_key"); CreateKeyPairResult createKeyPairResult = ec2.createKeyPair(createKeyPairRequest); KeyPair keyPair = new KeyPair(); keyPair = createKeyPairResult.getKeyPair(); String privateKey = keyPair.getKeyMaterial(); System.out.print(privateKey); /********************************************* * * #1.1 Describe Key Pair * *********************************************/ System.out.println("\n#1.1 Describe Key Pair"); DescribeKeyPairsResult dkr = ec2.describeKeyPairs(); System.out.println(dkr.toString()); /********************************************* * * #1.2 Create an Instance * *********************************************/ RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); runInstancesRequest.withImageId("ami-ab844dc2").withInstanceType("t1.micro").withMinCount(2) .withMaxCount(2).withKeyName("E3instance_key").withSecurityGroups("mini"); RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest); System.out.println("\n#1.2 Create an Instance"); List<Instance> resultInstance = runInstancesResult.getReservation().getInstances(); String createdInstanceId = null; for (Instance ins : resultInstance) { createdInstanceId = ins.getInstanceId(); System.out.println("New instance has been created: " + ins.getInstanceId()); } String myinstanceZone = resultInstance.get(0).getPlacement().getAvailabilityZone(); String myinstanceZone1 = resultInstance.get(1).getPlacement().getAvailabilityZone(); String myinstanceID = resultInstance.get(0).getInstanceId(); String myinstanceID1 = resultInstance.get(1).getInstanceId(); Thread.sleep(1000 * 1 * 60); /********************************************* * #2.1 Create a volume *********************************************/ //create a volume CreateVolumeRequest cvr = new CreateVolumeRequest(); CreateVolumeRequest cvr1 = new CreateVolumeRequest(); cvr.setAvailabilityZone(myinstanceZone); cvr1.setAvailabilityZone(myinstanceZone1); cvr.setSize(10); //size = 10 gigabytes cvr1.setSize(10); CreateVolumeResult volumeResult = ec2.createVolume(cvr); CreateVolumeResult volumeResult1 = ec2.createVolume(cvr1); String createdVolumeId = volumeResult.getVolume().getVolumeId(); String createdVolumeId1 = volumeResult1.getVolume().getVolumeId(); String[] volumeID = new String[2]; volumeID[0] = createdVolumeId; volumeID[1] = createdVolumeId1; System.out.println("\n#2.1 Create a volume for each instance"); Thread.sleep(1000 * 1 * 60); /********************************************* * #2.2 Attach the volume to the instance *********************************************/ AttachVolumeRequest avr = new AttachVolumeRequest(); AttachVolumeRequest avr1 = new AttachVolumeRequest(); avr.setInstanceId(myinstanceID); avr1.setInstanceId(myinstanceID1); avr.setVolumeId(createdVolumeId); avr1.setVolumeId(createdVolumeId1); avr.setDevice("/dev/sda2"); avr1.setDevice("/dev/sda2"); //avr.setVolumeId(createdVolumeId); //avr.setInstanceId(createdInstanceId); //avr.setDevice("/dev/sdf"); ec2.attachVolume(avr); ec2.attachVolume(avr1); System.out.println("\n#2.2 Attach the volume"); System.out.println("EBS volume has been attached and the volume ID is: " + createdVolumeId); System.out.println("EBS volume has been attached and the volume ID is: " + createdVolumeId1); Thread.sleep(1000 * 2 * 60); /*********************************** * #2.3 Monitoring (CloudWatch) *********************************/ //create CloudWatch client AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(credentials); //create request message GetMetricStatisticsRequest statRequest = new GetMetricStatisticsRequest(); //set up request message statRequest.setNamespace("AWS/EC2"); //namespace statRequest.setPeriod(60); //period of data ArrayList<String> stats = new ArrayList<String>(); //Use one of these strings: Average, Maximum, Minimum, SampleCount, Sum stats.add("Average"); stats.add("Sum"); statRequest.setStatistics(stats); //Use one of these strings: CPUUtilization, NetworkIn, NetworkOut, DiskReadBytes, DiskWriteBytes, DiskReadOperations statRequest.setMetricName("CPUUtilization"); // set time GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); calendar.add(GregorianCalendar.SECOND, -1 * calendar.get(GregorianCalendar.SECOND)); // 1 second ago Date endTime = calendar.getTime(); calendar.add(GregorianCalendar.MINUTE, -10); // 10 minutes ago Date startTime = calendar.getTime(); statRequest.setStartTime(startTime); statRequest.setEndTime(endTime); //specify an instance ArrayList<Dimension> dimensions = new ArrayList<Dimension>(); String monitorInstanceId = null; int i = 0; String[] idleInstance = new String[2]; for (Instance ins : resultInstance) { monitorInstanceId = ins.getInstanceId(); dimensions.add(new Dimension().withName("InstanceId").withValue(monitorInstanceId)); statRequest.setDimensions(dimensions); Thread.sleep(1000 * 3 * 60); //get statistics GetMetricStatisticsResult statResult = cloudWatch.getMetricStatistics(statRequest); //display System.out.println(statResult.toString()); List<Datapoint> dataList = statResult.getDatapoints(); Double averageCPU = null; Date timeStamp = null; for (Datapoint data : dataList) { averageCPU = data.getAverage(); timeStamp = data.getTimestamp(); System.out.println("Average CPU utlilization for last 1 minutes: " + averageCPU); //System.out.println("Total CPU utlilization for last 1 minutes: "+data.getSum()); //Calendar vmTime=GregorianCalendar.getInstance(); //vmTime.setTime(timeStamp); //vmTime.get(Calendar.HOUR_OF_DAY); if (averageCPU < 50 && i < 2) { idleInstance[i] = monitorInstanceId; i++; } } } System.out.println("\n" + i + " instance(s) idling."); /********************************************* * #2.4 Detach the volume from the instance *********************************************/ DetachVolumeRequest dvr = new DetachVolumeRequest(); DetachVolumeRequest dvr1 = new DetachVolumeRequest(); dvr.setVolumeId(createdVolumeId); dvr1.setVolumeId(createdVolumeId1); dvr.setInstanceId(myinstanceID); dvr1.setInstanceId(myinstanceID1); dvr.setDevice("/dev/sda2"); dvr1.setDevice("/dev/sda2"); ec2.detachVolume(dvr); ec2.detachVolume(dvr1); System.out.println("\n#2.4 Detach the volume"); Thread.sleep(1000 * 1 * 60); /********************************************* * #2.5 Create new AMI for idle instance *********************************************/ String[] idleAMIID = new String[2]; int j = 0; for (j = 0; j < idleInstance.length; j++) { CreateImageRequest Im = new CreateImageRequest(idleInstance[j], "image" + j); //CreateImageRequest Im1=new CreateImageRequest(myinstanceID1, "image1"); Im.setInstanceId(idleInstance[j]); //Im1.setInstanceId(myinstanceID1); CreateImageResult myAMI = ec2.createImage(Im); idleAMIID[j] = myAMI.getImageId(); //CreateImageResult myAMI1= ec2.createImage(Im1); System.out.println("\n#2.5 Create new AMI"); } Thread.sleep(1000 * 1 * 60); /********************************************* * * # Terminate an Instance * *********************************************/ //System.out.println("#8 Terminate the Instance"); // TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds); //ec2.terminateInstances(tir); /********************************************* * #2.6 Create new VMs *********************************************/ RunInstancesRequest runNewInstancesRequest = new RunInstancesRequest(); int m; String[] newCreatedInstanceId = new String[2]; for (m = 0; m < j; m++)//j is the number of AMI created { runNewInstancesRequest.withImageId(idleAMIID[m]).withInstanceType("t1.micro").withMinCount(1) .withMaxCount(1).withKeyName("E3instance_key").withSecurityGroups("mini"); RunInstancesResult runNewInstancesResult = ec2.runInstances(runNewInstancesRequest); List<Instance> newResultInstance = runNewInstancesResult.getReservation().getInstances(); String newInstanceId = null; for (Instance ins : newResultInstance) { newInstanceId = ins.getInstanceId(); } newCreatedInstanceId[m] = newInstanceId; System.out.println("Using AMI, a new instance has been created: " + newCreatedInstanceId[m]); } Thread.sleep(1000 * 1 * 60); //System.out.println("\n#2.6 Create "+ m + " instance using AMI"); /********************************************* * #2.7 Attach the volume to the new instance *********************************************/ int n; for (n = 0; n < idleInstance.length; n++) { AttachVolumeRequest new_avr = new AttachVolumeRequest(); //AttachVolumeRequest new_avr1 = new AttachVolumeRequest(); new_avr.setInstanceId(newCreatedInstanceId[n]); //avr1.setInstanceId(myinstanceID1); new_avr.setVolumeId(volumeID[n]); //avr1.setVolumeId(createdVolumeId1); new_avr.setDevice("/dev/sda2"); //avr1.setDevice("/dev/sda2"); //avr.setVolumeId(createdVolumeId); //avr.setInstanceId(createdInstanceId); //avr.setDevice("/dev/sdf"); ec2.attachVolume(new_avr); //ec2.attachVolume(avr1); System.out.println("\n#2.7 Re-attach the volume"); System.out.println("EBS volume has been attached and the volume ID is: " + volumeID[n]); //System.out.println("EBS volume has been attached and the volume ID is: "+createdVolumeId1); Thread.sleep(1000 * 1 * 60); } /************************************************ * #3 S3 bucket and object ***************************************************/ s3 = new AmazonS3Client(credentials); //create bucket String bucketName = "lucinda.duan"; s3.createBucket(bucketName); //set key String key = "object-name.txt"; //set value File file = File.createTempFile("temp", ".txt"); file.deleteOnExit(); Writer writer = new OutputStreamWriter(new FileOutputStream(file)); writer.write("This is a sample sentence.\r\nYes!"); writer.close(); //put object - bucket, key, value(file) s3.putObject(new PutObjectRequest(bucketName, key, file)); //get object S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent())); String data = null; while ((data = reader.readLine()) != null) { System.out.println(data); } /********************************************* * #4 shutdown client object *********************************************/ // ec2.shutdown(); // s3.shutdown(); } catch (AmazonServiceException ase) { System.out.println("Caught Exception: " + ase.getMessage()); System.out.println("Reponse Status Code: " + ase.getStatusCode()); System.out.println("Error Code: " + ase.getErrorCode()); System.out.println("Request ID: " + ase.getRequestId()); } }
From source file:aot.storage.s3.CustomStorage.java
License:Open Source License
protected static AmazonS3 createS3(String[] ids) { AmazonS3 s3;/*from w w w . java 2s .co m*/ if ((ids.length >= 1) && !ids[1].trim().isEmpty()) { String[] creds = ids[1].split(":"); s3 = new AmazonS3Client(new BasicAWSCredentials(creds[0], creds[1])); } else { s3 = new AmazonS3Client(); } if ((ids.length >= 2) && !ids[2].trim().isEmpty()) { s3.setRegion(Region.getRegion(Regions.fromName(ids[2]))); } if ((ids.length >= 3) && !ids[3].trim().isEmpty()) { s3.setEndpoint(ids[3]); } return s3; }
From source file:apphub.storage.s3.CustomStorage.java
License:Open Source License
protected static AmazonS3 createS3(URL url) { AmazonS3 s3;/*from w w w . ja v a2s . c om*/ String userInfo = url.getUserInfo(); if (userInfo != null) { String[] creds = userInfo.split(":"); if (creds.length == 2) { s3 = new AmazonS3Client(new BasicAWSCredentials(creds[0], creds[1])); } else { throw new CreateStorageException(url.toString(), "Credentials for S3 storage must be in form of KEY:SECRET"); } } else { s3 = new AmazonS3Client(); } Map<String, String> queryParameters = UrlUtil.getQueryParameters(url); String region = queryParameters.get("region"); if (region != null) { s3.setRegion(Region.getRegion(Regions.fromName(region))); } String endpoint = queryParameters.get("endpoint"); if (endpoint != null) { s3.setEndpoint(endpoint); } return s3; }
From source file:arcade.database.S3Connections.java
License:Open Source License
/** * Constructor that connects to S3Instance *//* www . j av a 2 s. c o m*/ public S3Connections() { myS3Instance = new AmazonS3Client( new ClasspathPropertiesFileCredentialsProvider("AwsCredentials.properties")); Region usWest2 = Region.getRegion(Regions.US_WEST_2); myS3Instance.setRegion(usWest2); }