List of usage examples for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials
public BasicAWSCredentials(String accessKey, String secretKey)
From source file:S3ClientSideEncryptionWithSymmetricMasterKey.java
License:Apache License
public static void main(String[] args) throws Exception { SecretKey mySymmetricKey = loadSymmetricAESKey(masterKeyDir, "AES"); EncryptionMaterials encryptionMaterials = new EncryptionMaterials(mySymmetricKey); AWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); AmazonS3EncryptionClient encryptionClient = new AmazonS3EncryptionClient(credentials, new StaticEncryptionMaterialsProvider(encryptionMaterials)); Region usEast1 = Region.getRegion(Regions.US_EAST_1); encryptionClient.setRegion(usEast1); encryptionClient.setEndpoint("https://play.minio.io:9000"); final S3ClientOptions clientOptions = S3ClientOptions.builder().setPathStyleAccess(true).build(); encryptionClient.setS3ClientOptions(clientOptions); // Create the bucket encryptionClient.createBucket(bucketName); // Upload object using the encryption client. byte[] plaintext = "Hello World, S3 Client-side Encryption Using Asymmetric Master Key!".getBytes(); System.out.println("plaintext's length: " + plaintext.length); encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey, new ByteArrayInputStream(plaintext), new ObjectMetadata())); // Download the object. S3Object downloadedObject = encryptionClient.getObject(bucketName, objectKey); byte[] decrypted = IOUtils.toByteArray(downloadedObject.getObjectContent()); // Verify same data. Assert.assertTrue(Arrays.equals(plaintext, decrypted)); //deleteBucketAndAllContents(encryptionClient); }
From source file:AWSClientFactory.java
License:Open Source License
public AWSClientFactory(String proxyHost, String proxyPort, String awsAccessKey, String awsSecretKey, String region) throws InvalidInputException { Validation.checkAWSClientFactoryConfig(proxyHost, proxyPort, awsAccessKey, awsSecretKey); this.proxyHost = proxyHost; this.proxyPort = proxyPort; this.awsAccessKey = awsAccessKey; this.awsSecretKey = awsSecretKey; this.region = region; clientConfig = new ClientConfiguration(); clientConfig.setUserAgentPrefix("CodeBuild-Jenkins-Plugin"); //tags all calls made from Jenkins plugin. clientConfig.setProxyHost(this.proxyHost); if (Validation.parseInt(this.proxyPort) != null) { clientConfig.setProxyPort(Validation.parseInt(proxyPort)); }//from www . ja v a 2 s . c om awsCredentials = new BasicAWSCredentials(this.awsAccessKey, this.awsSecretKey); }
From source file:AmazonDynamoDBPopulator.java
License:Open Source License
/** * The only information needed to create a client are security credentials * consisting of the AWS Access Key ID and Secret Access Key. All other * configuration, such as the service endpoints, are performed * automatically. Client parameters, such as proxies, can be specified in an * optional ClientConfiguration object when constructing a client. * * @see com.amazonaws.auth.BasicAWSCredentials * @see com.amazonaws.auth.PropertiesCredentials * @see com.amazonaws.ClientConfiguration *///w ww .j a v a2s . co m private static void init() throws Exception { BasicAWSCredentials creds = new BasicAWSCredentials(ACCESSKEY, SECRETKEY); dynamoDB = new AmazonDynamoDBClient(creds); dynamoDB.setEndpoint("http://dynamodb.us-east-1.amazonaws.com"); }
From source file:AwsSample.java
License:Open Source License
public static void main(String[] args) throws Exception { BasicAWSCredentials credentials = new BasicAWSCredentials("", ""); /********************************************* * //from ww w . j a v a 2 s. c om * #1 Create Amazon Client object * *********************************************/ System.out.println("#1 Create Amazon Client object"); ec2 = new AmazonEC2Client(credentials); try { /********************************************* * * #2 Describe Availability Zones. * *********************************************/ System.out.println("#2 Describe Availability Zones."); DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones(); System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size() + " Availability Zones."); /********************************************* * * #3 Describe Available Images * *********************************************/ System.out.println("#3 Describe Available Images"); DescribeImagesResult dir = ec2.describeImages(); List<Image> images = dir.getImages(); System.out.println("You have " + images.size() + " Amazon images"); /********************************************* * * #4 Describe Key Pair * *********************************************/ System.out.println("#9 Describe Key Pair"); DescribeKeyPairsResult dkr = ec2.describeKeyPairs(); System.out.println(dkr.toString()); /********************************************* * * #5 Describe Current Instances * *********************************************/ System.out.println("#4 Describe Current Instances"); DescribeInstancesResult describeInstancesRequest = ec2.describeInstances(); List<Reservation> reservations = describeInstancesRequest.getReservations(); Set<Instance> instances = new HashSet<Instance>(); // add all instances to a Set. for (Reservation reservation : reservations) { instances.addAll(reservation.getInstances()); } System.out.println("You have " + instances.size() + " Amazon EC2 instance(s)."); for (Instance ins : instances) { // instance id String instanceId = ins.getInstanceId(); // instance state InstanceState is = ins.getState(); System.out.println(instanceId + " " + is.getName()); } /////////////////////////////////////// String Temp_Group = "Testgroup1"; //name of the group CreateSecurityGroupRequest r1 = new CreateSecurityGroupRequest(Temp_Group, "temporal group"); ec2.createSecurityGroup(r1); AuthorizeSecurityGroupIngressRequest r2 = new AuthorizeSecurityGroupIngressRequest(); r2.setGroupName(Temp_Group); /*************the property of http*****************/ IpPermission permission = new IpPermission(); permission.setIpProtocol("tcp"); permission.setFromPort(80); permission.setToPort(80); List<String> ipRanges = new ArrayList<String>(); ipRanges.add("0.0.0.0/0"); permission.setIpRanges(ipRanges); /*************the property of SSH**********************/ IpPermission permission1 = new IpPermission(); permission1.setIpProtocol("tcp"); permission1.setFromPort(22); permission1.setToPort(22); List<String> ipRanges1 = new ArrayList<String>(); ipRanges1.add("0.0.0.0/22"); permission1.setIpRanges(ipRanges1); /*************the property of https**********************/ IpPermission permission2 = new IpPermission(); permission2.setIpProtocol("tcp"); permission2.setFromPort(443); permission2.setToPort(443); List<String> ipRanges2 = new ArrayList<String>(); ipRanges2.add("0.0.0.0/0"); permission2.setIpRanges(ipRanges2); /*************the property of tcp**********************/ IpPermission permission3 = new IpPermission(); permission3.setIpProtocol("tcp"); permission3.setFromPort(0); permission3.setToPort(65535); List<String> ipRanges3 = new ArrayList<String>(); ipRanges3.add("0.0.0.0/0"); permission3.setIpRanges(ipRanges3); /**********************add rules to the group*********************/ List<IpPermission> permissions = new ArrayList<IpPermission>(); permissions.add(permission); permissions.add(permission1); permissions.add(permission2); permissions.add(permission3); r2.setIpPermissions(permissions); ec2.authorizeSecurityGroupIngress(r2); List<String> groupName = new ArrayList<String>(); groupName.add(Temp_Group);//wait to out our instance into this group /********************************************* * * #6.2 Create a New Key Pair * *********************************************/ CreateKeyPairRequest newKeyRequest = new CreateKeyPairRequest(); newKeyRequest.setKeyName("Test_Key2"); CreateKeyPairResult keyresult = ec2.createKeyPair(newKeyRequest); /************************print the properties of this key*****************/ KeyPair kp = new KeyPair(); kp = keyresult.getKeyPair(); System.out.println("The key we created is = " + kp.getKeyName() + "\nIts fingerprint is=" + kp.getKeyFingerprint() + "\nIts material is= \n" + kp.getKeyMaterial()); String fileName = "C:/Users/Akhil/workspace/Test_Key2.pem"; File distFile = new File(fileName); BufferedReader bufferedReader = new BufferedReader(new StringReader(kp.getKeyMaterial())); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(distFile)); char buf[] = new char[1024]; int len; while ((len = bufferedReader.read(buf)) != -1) { bufferedWriter.write(buf, 0, len); } bufferedWriter.flush(); bufferedReader.close(); bufferedWriter.close(); //String myinstance; /********************************************* * * #6 Create an Instance * *********************************************/ System.out.println("#5 Create an Instance"); String imageId = "ami-76f0061f"; //Basic 32-bit Amazon Linux AMI int minInstanceCount = 1; // create 1 instance int maxInstanceCount = 1; RunInstancesRequest rir = new RunInstancesRequest(imageId, minInstanceCount, maxInstanceCount); rir.setKeyName("Test_Key2"); rir.withSecurityGroups("Testgroup1"); RunInstancesResult result = ec2.runInstances(rir); //get instanceId from the result List<Instance> resultInstance = result.getReservation().getInstances(); String createdInstanceId = null; String myAvailabilityZone = null; for (Instance ins : resultInstance) { createdInstanceId = ins.getInstanceId(); System.out.println("New instance has been created: " + ins.getInstanceId()); //myinstance = ins.getInstanceId(); } Thread.currentThread().sleep(60000); /********************************************* * * * Create a New Volume and attach it * ***********************************************/ List<Instance> resultInstance2 = result.getReservation().getInstances(); createdInstanceId = null; for (Instance ins : resultInstance2) { createdInstanceId = ins.getInstanceId(); System.out.println("New instance has been created: " + ins.getInstanceId());//print the instance ID /********************************************* * * #6.4 Create an Instance * *********************************************/ CreateVolumeRequest newVol = new CreateVolumeRequest(1, "us-east-1a"); CreateVolumeResult volresult = ec2.createVolume(newVol); Volume vol1 = volresult.getVolume(); String volId = vol1.getVolumeId(); Thread.currentThread().sleep(30000); AttachVolumeRequest attachRequest = new AttachVolumeRequest().withInstanceId(createdInstanceId) .withVolumeId(volId); attachRequest.withDevice("/dev/sda5"); ec2.attachVolume(attachRequest); System.out.println("EBS volume has been attached and the volume ID is: " + volId); } /********************************************* * * #7 Create a 'tag' for the new instance. * *********************************************/ System.out.println("#6 Create a 'tag' for the new instance."); List<String> resources = new LinkedList<String>(); List<Tag> tags = new LinkedList<Tag>(); Tag nameTag = new Tag("Akhil", "MyFirstInstance"); resources.add(createdInstanceId); tags.add(nameTag); CreateTagsRequest ctr = new CreateTagsRequest(resources, tags); ec2.createTags(ctr); /********************************************* * * #8 Stop/Start an Instance * *********************************************/ System.out.println("#7 Stop the Instance"); List<String> instanceIds = new LinkedList<String>(); instanceIds.add(createdInstanceId); //stop StopInstancesRequest stopIR = new StopInstancesRequest(instanceIds); ec2.stopInstances(stopIR); //start StartInstancesRequest startIR = new StartInstancesRequest(instanceIds); ec2.startInstances(startIR); System.out.println("#8 Getting DNS, IP."); DescribeInstancesRequest request = new DescribeInstancesRequest(); request.setInstanceIds(instanceIds); DescribeInstancesResult result1 = ec2.describeInstances(request); List<Reservation> reservations1 = result1.getReservations(); List<Instance> instances1; for (Reservation res : reservations1) { instances1 = res.getInstances(); for (Instance ins1 : instances1) { System.out .println("The public DNS is: " + ins1.getPublicDnsName() + "\n" + ins1.getRamdiskId()); System.out.println("The private IP is: " + ins1.getPrivateIpAddress()); System.out.println("The public IP is: " + ins1.getPublicIpAddress()); } /********************************************* * #10 Terminate an Instance * *********************************************/ System.out.println("#8 Terminate the Instance"); TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds); //ec2.terminateInstances(tir); /********************************************* * * #11 shutdown client object * *********************************************/ ec2.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:UploadUrlGenerator.java
License:Open Source License
public static void main(String[] args) throws Exception { Options opts = new Options(); opts.addOption(Option.builder().longOpt(ENDPOINT_OPTION).required().hasArg().argName("url") .desc("Sets the ECS S3 endpoint to use, e.g. https://ecs.company.com:9021").build()); opts.addOption(Option.builder().longOpt(ACCESS_KEY_OPTION).required().hasArg().argName("access-key") .desc("Sets the Access Key (user) to sign the request").build()); opts.addOption(Option.builder().longOpt(SECRET_KEY_OPTION).required().hasArg().argName("secret") .desc("Sets the secret key to sign the request").build()); opts.addOption(Option.builder().longOpt(BUCKET_OPTION).required().hasArg().argName("bucket-name") .desc("The bucket containing the object").build()); opts.addOption(Option.builder().longOpt(KEY_OPTION).required().hasArg().argName("object-key") .desc("The object name (key) to access with the URL").build()); opts.addOption(Option.builder().longOpt(EXPIRES_OPTION).hasArg().argName("minutes") .desc("Minutes from local time to expire the request. 1 day = 1440, 1 week=10080, " + "1 month (30 days)=43200, 1 year=525600. Defaults to 1 hour (60).") .build());// w w w . ja va 2 s . c om opts.addOption(Option.builder().longOpt(VERB_OPTION).hasArg().argName("http-verb").type(HttpMethod.class) .desc("The HTTP verb that will be used with the URL (PUT, GET, etc). Defaults to GET.").build()); opts.addOption(Option.builder().longOpt(CONTENT_TYPE_OPTION).hasArg().argName("mimetype") .desc("Sets the Content-Type header (e.g. image/jpeg) that will be used with the request. " + "Must match exactly. Defaults to application/octet-stream for PUT/POST and " + "null for all others") .build()); DefaultParser dp = new DefaultParser(); CommandLine cmd = null; try { cmd = dp.parse(opts, args); } catch (ParseException e) { System.err.println("Error: " + e.getMessage()); HelpFormatter hf = new HelpFormatter(); hf.printHelp("java -jar UploadUrlGenerator-xxx.jar", opts, true); System.exit(255); } URI endpoint = URI.create(cmd.getOptionValue(ENDPOINT_OPTION)); String accessKey = cmd.getOptionValue(ACCESS_KEY_OPTION); String secretKey = cmd.getOptionValue(SECRET_KEY_OPTION); String bucket = cmd.getOptionValue(BUCKET_OPTION); String key = cmd.getOptionValue(KEY_OPTION); HttpMethod method = HttpMethod.GET; if (cmd.hasOption(VERB_OPTION)) { method = HttpMethod.valueOf(cmd.getOptionValue(VERB_OPTION).toUpperCase()); } int expiresMinutes = 60; if (cmd.hasOption(EXPIRES_OPTION)) { expiresMinutes = Integer.parseInt(cmd.getOptionValue(EXPIRES_OPTION)); } String contentType = null; if (method == HttpMethod.PUT || method == HttpMethod.POST) { contentType = "application/octet-stream"; } if (cmd.hasOption(CONTENT_TYPE_OPTION)) { contentType = cmd.getOptionValue(CONTENT_TYPE_OPTION); } BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); ClientConfiguration cc = new ClientConfiguration(); // Force use of v2 Signer. ECS does not support v4 signatures yet. cc.setSignerOverride("S3SignerType"); AmazonS3Client s3 = new AmazonS3Client(credentials, cc); s3.setEndpoint(endpoint.toString()); S3ClientOptions s3c = new S3ClientOptions(); s3c.setPathStyleAccess(true); s3.setS3ClientOptions(s3c); // Sign the URL Calendar c = Calendar.getInstance(); c.add(Calendar.MINUTE, expiresMinutes); GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key).withExpiration(c.getTime()) .withMethod(method); if (contentType != null) { req = req.withContentType(contentType); } URL u = s3.generatePresignedUrl(req); System.out.printf("URL: %s\n", u.toURI().toASCIIString()); System.out.printf("HTTP Verb: %s\n", method); System.out.printf("Expires: %s\n", c.getTime()); System.out.println("To Upload with curl:"); StringBuilder sb = new StringBuilder(); sb.append("curl "); if (method != HttpMethod.GET) { sb.append("-X "); sb.append(method.toString()); sb.append(" "); } if (contentType != null) { sb.append("-H \"Content-Type: "); sb.append(contentType); sb.append("\" "); } if (method == HttpMethod.POST || method == HttpMethod.PUT) { sb.append("-T <filename> "); } sb.append("\""); sb.append(u.toURI().toASCIIString()); sb.append("\""); System.out.println(sb.toString()); System.exit(0); }
From source file:InstallYarn.java
License:Open Source License
public static void main(String[] args) throws JSchException, IOException, InterruptedException { /*/* w w w . j a v a 2 s . c o m*/ * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (C:\\Users\\CH\\.aws\\credentials). */ AWSCredentials credentials = null; try { credentials = new BasicAWSCredentials("Your Access Key ID", "Your Secret Access Key"); } 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 (C:\\Users\\CH\\.aws\\credentials), and is in valid format.", e); } // Create the AmazonEC2Client object so we can call various APIs. AmazonEC2 ec2 = new AmazonEC2Client(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); ec2.setRegion(usWest2); /* // Create a new security group. try { CreateSecurityGroupRequest securityGroupRequest = new CreateSecurityGroupRequest( "GettingStartedGroup", "Getting Started Security Group"); CreateSecurityGroupResult result = ec2 .createSecurityGroup(securityGroupRequest); System.out.println(String.format("Security group created: [%s]", result.getGroupId())); } catch (AmazonServiceException ase) { // Likely this means that the group is already created, so ignore. System.out.println(ase.getMessage()); } String ipAddr = "0.0.0.0/0"; // Get the IP of the current host, so that we can limit the Security Group // by default to the ip range associated with your subnet. try { InetAddress addr = InetAddress.getLocalHost(); // Get IP Address ipAddr = addr.getHostAddress()+"/10"; } catch (UnknownHostException e) { } // Create a range that you would like to populate. List<String> ipRanges = Collections.singletonList(ipAddr); // Open up port 23 for TCP traffic to the associated IP from above (e.g. ssh traffic). IpPermission ipPermission = new IpPermission() .withIpProtocol("tcp") .withFromPort(new Integer(22)) .withToPort(new Integer(22)) .withIpRanges(ipRanges); List<IpPermission> ipPermissions = Collections.singletonList(ipPermission); try { // Authorize the ports to the used. AuthorizeSecurityGroupIngressRequest ingressRequest = new AuthorizeSecurityGroupIngressRequest( "GettingStartedGroup", ipPermissions); ec2.authorizeSecurityGroupIngress(ingressRequest); System.out.println(String.format("Ingress port authroized: [%s]", ipPermissions.toString())); } catch (AmazonServiceException ase) { // Ignore because this likely means the zone has already been authorized. System.out.println(ase.getMessage()); } */ //CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest() //.withKeyName("CHENHAO"); //CreateKeyPairResult createKeyPairResult = ec2.createKeyPair(createKeyPairRequest); //KeyPair keyPair = new KeyPair(); //keyPair = createKeyPairResult.getKeyPair(); //String privateKey = keyPair.getKeyMaterial(); int cluster_size = Integer .parseInt(JOptionPane.showInputDialog("Enter number of machines you want to set")); String ami_name = JOptionPane.showInputDialog("Enter your ami name"); String key_name = JOptionPane.showInputDialog("Enter your key name"); String s_group_name = JOptionPane.showInputDialog("Enter your security group name"); RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); runInstancesRequest.withImageId(ami_name).withInstanceType("t2.micro").withMinCount(cluster_size) .withMaxCount(cluster_size).withKeyName(key_name).withSecurityGroups(s_group_name); RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest); Thread.sleep(10000); Vector<String> instanceId = new Vector<String>(); for (Instance ins : runInstancesResult.getReservation().getInstances()) instanceId.add(ins.getInstanceId()); DescribeInstancesRequest request = new DescribeInstancesRequest(); request.setInstanceIds(instanceId); DescribeInstancesResult result = ec2.describeInstances(request); List<Reservation> reservations = result.getReservations(); List<Instance> instances_list = new Vector<Instance>(); for (int i = 0; i < reservations.size(); i++) instances_list.addAll(reservations.get(i).getInstances()); System.out.println("Plan cluster size:" + cluster_size + " Real size:" + instances_list.size()); JSch jsch = new JSch(); JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Choose your privatekey"); chooser.setFileHidingEnabled(false); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose " + chooser.getSelectedFile().getAbsolutePath() + "."); jsch.addIdentity(chooser.getSelectedFile().getAbsolutePath()); } Session session; UserInfo ui = new MyUserInfo(); for (int i = 0; i < instances_list.size(); i++) { if (instances_list.get(i).getPublicIpAddress() == null) System.out.println("Error, public ip is null\n"); System.out.println("Connect to:" + instances_list.get(i).getPublicIpAddress() + "\n"); session = jsch.getSession("ubuntu", instances_list.get(i).getPublicIpAddress(), 22); session.setUserInfo(ui); session.connect(); // //if(i==0) //{ // transfer_file_to("/home/ubuntu","C:/Users/CH/Downloads/ch.pem",session); // exec("chmod 400 /home/ubuntu/ch.pem",session); //} //slaves file for (int j = 0; j < instances_list.size(); j++) { if (j != 0) exec("echo " + instances_list.get(j).getPrivateIpAddress() + "\n >> /usr/local/hadoop/etc/hadoop/slaves", session); } //core-site file String command = "sed -i 's#Master#" + instances_list.get(0).getPrivateIpAddress() + "#g' /usr/local/hadoop/etc/hadoop/core-site.xml"; exec(command, session); //hdfs-size file command = "sed -i 's#Master#" + instances_list.get(0).getPrivateIpAddress() + "#g' /usr/local/hadoop/etc/hadoop/core-site.xml"; exec(command, session); command = "sed -i 's#replication#" + Integer.toString(cluster_size - 1) + "#g' /usr/local/hadoop/etc/hadoop/core-site.xml"; exec(command, session); //yarn-size file command = "sed -i 's#Master#" + instances_list.get(0).getPrivateIpAddress() + "#g' /usr/local/hadoop/etc/hadoop/core-site.xml"; exec(command, session); session.disconnect(); } //username and passphrase will be given via UserInfo interface. //slaves file }
From source file:S3ClientSideEncryptionAsymmetricMasterKey.java
License:Apache License
public static void main(String[] args) throws Exception { // 1. Load keys from files byte[] bytes = FileUtils.readFileToByteArray(new File(keyDir + "/private.key")); KeyFactory kf = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes); PrivateKey pk = kf.generatePrivate(ks); bytes = FileUtils.readFileToByteArray(new File(keyDir + "/public.key")); PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes)); KeyPair loadedKeyPair = new KeyPair(publicKey, pk); // 2. Construct an instance of AmazonS3EncryptionClient. EncryptionMaterials encryptionMaterials = new EncryptionMaterials(loadedKeyPair); AWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); AmazonS3EncryptionClient encryptionClient = new AmazonS3EncryptionClient(credentials, new StaticEncryptionMaterialsProvider(encryptionMaterials)); Region usEast1 = Region.getRegion(Regions.US_EAST_1); encryptionClient.setRegion(usEast1); encryptionClient.setEndpoint("https://play.minio.io:9000"); final S3ClientOptions clientOptions = S3ClientOptions.builder().setPathStyleAccess(true).build(); encryptionClient.setS3ClientOptions(clientOptions); // Create the bucket encryptionClient.createBucket(bucketName); // 3. Upload the object. byte[] plaintext = "Hello World, S3 Client-side Encryption Using Asymmetric Master Key!".getBytes(); System.out.println("plaintext's length: " + plaintext.length); encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey, new ByteArrayInputStream(plaintext), new ObjectMetadata())); // 4. Download the object. S3Object downloadedObject = encryptionClient.getObject(bucketName, objectKey); byte[] decrypted = IOUtils.toByteArray(downloadedObject.getObjectContent()); Assert.assertTrue(Arrays.equals(plaintext, decrypted)); System.out.println("decrypted length: " + decrypted.length); //deleteBucketAndAllContents(encryptionClient); }
From source file:receiveSQS.java
License:Open Source License
public static void main(String[] args) throws Exception { /*//from w w w.j a v a2 s. co m * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (). */ BasicAWSCredentials credentials = new BasicAWSCredentials("AKIAI2ZCFS3NVEENXW5A", "tI/GgpSWDF/QrNVhtCRu1G+PX/10A2nJQH+yTOiv"); 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/daniel/.aws/credentials), and is in valid format.", e); } AmazonSQS sqs = new AmazonSQSClient(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); sqs.setRegion(usWest2); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS"); System.out.println("===========================================\n"); try { // List queues System.out.println("Listing all queues in your account.\n"); for (String queueUrl : sqs.listQueues().getQueueUrls()) { System.out.println(" QueueUrl: " + queueUrl); System.out.println(); // Receive messages System.out.println("Receiving messages from MyQueue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl); System.out.println("Message size:"); //***************************************************** //For some reason, we only get one message at a time and it does not loop over. //***************************************************** //ReceiveMessageResponse receiveMessageResponse = amazonSQSClient.ReceiveMessage(receiveMessageRequest); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.println(" Message"); //System.out.println(" MessageId: " + message.getMessageId()); //System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); //System.out.println(" MD5OfBody: " + message.getMD5OfBody()); //System.out.println(" Body: " + message.getBody()); String[] tweetdata = message.getBody().split("\\|\\|"); System.out.println(Arrays.toString(tweetdata)); System.out.println("Deleting a message.\n"); String messageRecieptHandle = messages.get(0).getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(queueUrl, messageRecieptHandle)); } } System.out.println(); /* // Delete a message // Delete a queue System.out.println("Deleting the test queue.\n"); sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl)); */ } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, 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 SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:ai.serotonin.backup.Base.java
License:Mozilla Public License
private void createGlacierClient() { final String accessKey = configRoot.get("glacier").get("accessKey").asText(); final String secretKey = configRoot.get("glacier").get("secretKey").asText(); final String endpoint = configRoot.get("glacier").get("endpoint").asText(); credentials = new BasicAWSCredentials(accessKey, secretKey); client = new AmazonGlacierClient(credentials) // .withEndpoint(endpoint);/*w w w. j ava2s . c o m*/ }
From source file:aot.storage.s3.CustomStorage.java
License:Open Source License
protected static AmazonS3 createS3(String[] ids) { AmazonS3 s3;/* w w w. ja v a 2 s . c om*/ 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; }