Created
August 8, 2019 12:04
-
-
Save MitchDresdner/b4e002e73dfe9ceb6574b8f3ba943811 to your computer and use it in GitHub Desktop.
Basic S3 and SQS operations in JAVA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.awss3sample.acme.awss3sample; | |
import com.amazonaws.ClientConfiguration; | |
import com.amazonaws.auth.profile.ProfileCredentialsProvider; | |
import com.amazonaws.regions.Regions; | |
import com.amazonaws.services.s3.AmazonS3; | |
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |
import com.amazonaws.services.s3.model.Bucket; | |
import com.amazonaws.services.sqs.AmazonSQS; | |
import com.amazonaws.services.sqs.AmazonSQSClientBuilder; | |
import com.amazonaws.services.sqs.model.*; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import java.util.List; | |
@SpringBootApplication | |
public class Awss3sampleApplication implements CommandLineRunner { | |
private static Logger LOG = LoggerFactory.getLogger(Awss3sampleApplication.class); | |
public static void main(String[] args) { | |
LOG.info("STARTING THE APPLICATION"); | |
SpringApplication.run(Awss3sampleApplication.class, args); | |
LOG.info("APPLICATION FINISHED"); | |
} | |
@Override | |
public void run(String... args) { | |
LOG.info("EXECUTING : command line runner"); | |
// Create a client builder using a proxy to AWS | |
AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard(); | |
ClientConfiguration cc = new ClientConfiguration(); | |
cc.setProxyHost("proxy.acme.com"); | |
cc.setProxyPort(80); | |
builder.setClientConfiguration(cc); | |
// Connect to S3 and list buckets | |
AmazonS3 s3 = builder.withRegion(Regions.US_EAST_1).build(); | |
List<Bucket> bucketList = s3.listBuckets(); | |
LOG.info("Your Amazon S3 buckets are:"); | |
for (Bucket b : bucketList) { | |
LOG.info("* " + b.getName()); | |
} | |
AmazonSQSClientBuilder sqsBuilder = AmazonSQSClientBuilder.standard(); | |
// Assumes other than default profile called sqs | |
ProfileCredentialsProvider creds = new ProfileCredentialsProvider("sqs"); | |
LOG.info("Using ID " + creds.getCredentials().getAWSAccessKeyId()); | |
// Create an SQS connection and list queues | |
sqsBuilder.setClientConfiguration(cc); | |
AmazonSQS sqs = sqsBuilder | |
.withRegion(Regions.US_EAST_1) | |
.withCredentials(creds) | |
.build(); | |
// Create an initial Queue | |
// CreateQueueRequest create_request = new CreateQueueRequest("acme-test") | |
// .addAttributesEntry("DelaySeconds", "60") | |
// .addAttributesEntry("MessageRetentionPeriod", "86400"); | |
// | |
// try { | |
// sqs.createQueue(create_request); | |
// } catch (AmazonSQSException e) { | |
// if (!e.getErrorCode().equals("QueueAlreadyExists")) { | |
// throw e; | |
// } | |
// } | |
// Search through queues for one prefixed with acme | |
String name_prefix = "acme"; | |
ListQueuesResult lq_result = sqs.listQueues(new ListQueuesRequest(name_prefix)); | |
LOG.info("Queue URLs with prefix: " + name_prefix); | |
for (String url : lq_result.getQueueUrls()) { | |
LOG.info(url); | |
} | |
SendMessageRequest send_msg_request = new SendMessageRequest() | |
.withQueueUrl("https://sqs.us-east-1.amazonaws.com/<Queue URI>/acme-test") | |
.withMessageBody("who moved my cheese") | |
.withDelaySeconds(5); | |
sqs.sendMessage(send_msg_request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment