Skip to content

Commit

Permalink
Upgrading java SDK versoin, removing usage of deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Meisel committed May 18, 2017
1 parent 69d00c8 commit 26c1bae
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ package dynamodb

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

import org.scalatest.{Suite, BeforeAndAfterAll}

import com.amazonaws.auth.BasicAWSCredentials
import org.scalatest.{BeforeAndAfterAll, Suite}
import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials}
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration
import com.amazonaws.services.dynamodbv2._
import com.amazonaws.services.dynamodbv2.model._

import org.slf4j.Logger;
import org.slf4j.Logger
import org.slf4j.LoggerFactory;


Expand All @@ -39,8 +37,11 @@ trait DynamoDBClient
private val logger: Logger = LoggerFactory.getLogger(self.getClass)

val client = {
val jClient = new AmazonDynamoDBAsyncClient(new BasicAWSCredentials("FAKE_ACCESS_KEY", "FAKE_SECRET_KEY"))
jClient.setEndpoint("http://localhost:8000")
val jClient: AmazonDynamoDBAsyncClient = AmazonDynamoDBAsyncClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("FAKE_ACCESS_KEY", "FAKE_SECRET_KEY")))
.withEndpointConfiguration(new EndpointConfiguration("http://localhost:8000", "us-west-1"))
.build()
.asInstanceOf[AmazonDynamoDBAsyncClient]

new AmazonDynamoDBScalaClient(jClient)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trait S3ClientHelper
c
}

val transferManager = new TransferManager(client.client)
val transferManager = TransferManagerBuilder.standard().withS3Client(client.client).build()

val bucketNames: Seq[String]

Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sbt._
object Dependencies {

object V {
val awsJavaSDK = "1.11.18"
val awsJavaSDK = "1.11.129"

val jodaTime = "2.9.4"
val jodaConvert = "1.8.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ import java.util.{concurrent => juc}
import java.util.concurrent.{atomic => juca}
import java.util.concurrent.{locks => jucl}

import com.amazonaws.{AmazonClientException, AmazonServiceException, ClientConfiguration}
import com.amazonaws.auth.{AWSCredentials, AWSCredentialsProvider}
import com.amazonaws.internal.StaticCredentialsProvider
import com.amazonaws.{AmazonClientException, ClientConfiguration, SdkBaseException}
import com.amazonaws.auth.{AWSCredentials, AWSCredentialsProvider, AWSStaticCredentialsProvider}
import com.amazonaws.retry.RetryUtils
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder
import com.amazonaws.services.dynamodbv2.model.{BatchWriteItemRequest, ProvisionedThroughputExceededException, ReturnConsumedCapacity, WriteRequest}

import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -60,7 +58,7 @@ object ConcurrentBatchWriter {
* @param writeConcurrency write concurrency and also the max connections
* @return a ClientConfiguration that can be further config
*/
def defaultClientConfig(writeConcurrency: Int) =
def defaultClientConfig(writeConcurrency: Int): ClientConfiguration =
new ClientConfiguration()
.withMaxErrorRetry(0)
.withMaxConnections(writeConcurrency)
Expand Down Expand Up @@ -101,7 +99,7 @@ class ConcurrentBatchWriter(
* The number of concurrent writers
*/
def this(tableName: String, credentials: AWSCredentials, writeConcurrency: Int) {
this(tableName, new StaticCredentialsProvider(credentials), writeConcurrency, ConcurrentBatchWriter.defaultClientConfig(writeConcurrency))
this(tableName, new AWSStaticCredentialsProvider(credentials), writeConcurrency, ConcurrentBatchWriter.defaultClientConfig(writeConcurrency))
}

// convenience type synonym
Expand All @@ -115,7 +113,10 @@ class ConcurrentBatchWriter(

// a synchronous DynamoDB client that performs no internal retry-logic
// and has a pool of connections equal to the number of concurrent writers
private val dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider, clientConfiguration.withMaxConnections(writeConcurrency))
private val dynamoDBClient = AmazonDynamoDBClientBuilder.standard()
.withCredentials(credentialsProvider)
.withClientConfiguration(clientConfiguration.withMaxConnections(writeConcurrency))
.build()

/*
* turn an fixed capacity, array blocking queue into a buffer
Expand Down Expand Up @@ -333,7 +334,7 @@ class ConcurrentBatchWriter(
try {
writeWithBackoffRetry(batch)
} catch {
case e: AmazonServiceException if RetryUtils.isRequestEntityTooLargeException(e) =>
case e: SdkBaseException if RetryUtils.isRequestEntityTooLargeException(e) =>
// if request exceeded the 1Mb request limit
val requests = batch.get(tableName)
val size = requests.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ package dynamodb
import java.{util => ju}
import java.util.Random

import com.amazonaws.{AmazonServiceException, ClientConfiguration}
import com.amazonaws.auth.{AWSCredentials, AWSCredentialsProvider}
import com.amazonaws.internal.StaticCredentialsProvider
import com.amazonaws.{ClientConfiguration, SdkBaseException}
import com.amazonaws.auth.{AWSCredentials, AWSCredentialsProvider, AWSStaticCredentialsProvider}
import com.amazonaws.retry.RetryUtils
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder
import com.amazonaws.services.dynamodbv2.model.{BatchWriteItemRequest, ProvisionedThroughputExceededException, ReturnConsumedCapacity, WriteRequest}

import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand All @@ -52,17 +50,20 @@ class SingleThreadedBatchWriter(
* client supply ClientConfiguration
*/
def this(tableName: String, credentials: AWSCredentials, clientConfiguration: ClientConfiguration) {
this(tableName, new StaticCredentialsProvider(credentials), clientConfiguration)
this(tableName, new AWSStaticCredentialsProvider(credentials), clientConfiguration)
}

private type Batch = ju.Map[String, ju.List[WriteRequest]]

private val logger: Logger = LoggerFactory.getLogger(this.getClass)

private val client = new AmazonDynamoDBClient(credentialsProvider, clientConfiguration)
private val client = AmazonDynamoDBClientBuilder.standard()
.withCredentials(credentialsProvider)
.withClientConfiguration(clientConfiguration)
.build()

def this(tableName: String, credentials: AWSCredentials) {
this(tableName, new StaticCredentialsProvider(credentials))
this(tableName, new AWSStaticCredentialsProvider(credentials))
}

// implement exponential backoff using Thread.sleep
Expand Down Expand Up @@ -173,7 +174,7 @@ class SingleThreadedBatchWriter(
try {
writeWithBackoffRetry(batch)
} catch {
case e: AmazonServiceException if RetryUtils.isRequestEntityTooLargeException(e) =>
case e: SdkBaseException if RetryUtils.isRequestEntityTooLargeException(e) =>
// if request exceeded the 1Mb request limit
val requests = batch.get(tableName)
val size = requests.size
Expand Down
24 changes: 12 additions & 12 deletions src/main/scala/com/github/dwhjames/awswrap/s3/s3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,21 @@
package com.github.dwhjames.awswrap
package s3

import java.io.{InputStream, File}
import java.io.{File, InputStream}
import java.net.URL

import scala.collection.JavaConverters._
import scala.concurrent.{Future, Promise}
import scala.util.Try

import java.util.concurrent.{TimeUnit, Executors, ExecutorService, ThreadFactory}
import java.util.concurrent.{ExecutorService, Executors, ThreadFactory, TimeUnit}
import java.util.concurrent.atomic.AtomicLong

import com.amazonaws.ClientConfiguration
import com.amazonaws.auth.{AWSCredentials, AWSCredentialsProvider, DefaultAWSCredentialsProviderChain}
import com.amazonaws.event.{ProgressListener, ProgressEvent, ProgressEventType}
import com.amazonaws.internal.StaticCredentialsProvider
import com.amazonaws.auth.{AWSCredentials, AWSCredentialsProvider, AWSStaticCredentialsProvider, DefaultAWSCredentialsProviderChain}
import com.amazonaws.event.{ProgressEvent, ProgressEventType, ProgressListener}
import com.amazonaws.services.s3._
import com.amazonaws.services.s3.model._
import com.amazonaws.services.s3.transfer.Transfer

import org.slf4j.{Logger, LoggerFactory}

private[s3] class S3ThreadFactory extends ThreadFactory {
Expand Down Expand Up @@ -82,7 +79,10 @@ class AmazonS3ScalaClient(
*
* @see [[http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html AmazonS3Client]]
*/
val client = new AmazonS3Client(awsCredentialsProvider, clientConfiguration)
val client: AmazonS3 = AmazonS3ClientBuilder.standard()
.withCredentials(awsCredentialsProvider)
.withClientConfiguration(clientConfiguration)
.build()

/**
* make a client from a credentials provider, a config, and a default executor service.
Expand Down Expand Up @@ -129,7 +129,7 @@ class AmazonS3ScalaClient(
* an executor service for synchronous calls to the underlying AmazonS3Client.
*/
def this(awsCredentials: AWSCredentials, clientConfiguration: ClientConfiguration, executorService: ExecutorService) {
this(new StaticCredentialsProvider(awsCredentials), clientConfiguration, executorService)
this(new AWSStaticCredentialsProvider(awsCredentials), clientConfiguration, executorService)
}

/**
Expand All @@ -151,7 +151,7 @@ class AmazonS3ScalaClient(
* AWS credentials.
*/
def this(awsCredentials: AWSCredentials) {
this(new StaticCredentialsProvider(awsCredentials))
this(new AWSStaticCredentialsProvider(awsCredentials))
}

/**
Expand Down Expand Up @@ -327,7 +327,7 @@ class AmazonS3ScalaClient(
def deleteObjects(
deleteObjectsRequest: DeleteObjectsRequest
): Future[Seq[DeleteObjectsResult.DeletedObject]] =
wrapMethod((req: DeleteObjectsRequest) => client.deleteObjects(req).getDeletedObjects.asScala.toSeq, deleteObjectsRequest)
wrapMethod((req: DeleteObjectsRequest) => client.deleteObjects(req).getDeletedObjects.asScala, deleteObjectsRequest)

/**
* @see [[http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html#deleteVersion(com.amazonaws.services.s3.model.DeleteVersionRequest) AWS Java SDK]]
Expand Down Expand Up @@ -436,7 +436,7 @@ class AmazonS3ScalaClient(
def listBuckets(
listBucketsRequest: ListBucketsRequest
): Future[Seq[Bucket]] =
wrapMethod((req: ListBucketsRequest) => client.listBuckets(req).asScala.toSeq, listBucketsRequest)
wrapMethod((req: ListBucketsRequest) => client.listBuckets(req).asScala, listBucketsRequest)

/**
* @see [[http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html#listBuckets(com.amazonaws.services.s3.model.ListBucketsRequest) AWS Java SDK]]
Expand Down

0 comments on commit 26c1bae

Please sign in to comment.