本文整理汇总了Scala中com.amazonaws.services.s3.AmazonS3Client类的典型用法代码示例。如果您正苦于以下问题:Scala AmazonS3Client类的具体用法?Scala AmazonS3Client怎么用?Scala AmazonS3Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AmazonS3Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Lambda
//设置package包名称以及导入依赖的类
package com.ovoenergy.lambda
import java.io.{Closeable, InputStreamReader}
import java.util.{Map => JMap}
import collection.JavaConverters._
import com.amazonaws.services.lambda.runtime.{Context, RequestHandler}
import com.amazonaws.services.s3.AmazonS3Client
import com.ovoenergy.lambda.client.{KafkaMetricsClient, LibratoClient}
import com.ovoenergy.lambda.domain.{ConsumerGroupMetricResponse, KafkaMetrics, PartitionData}
import com.squareup.okhttp.OkHttpClient
import com.typesafe.config.ConfigFactory
class Lambda extends RequestHandler[JMap[String, Object], Unit] with ConnectionHelpers{
val s3Client = new AmazonS3Client
override def handleRequest(event: JMap[String, Object], context: Context): Unit = {
val environment = context.getFunctionName.split('-').last
println(s"Hello, I'm a Lambda running in the $environment environment")
val config = using(s3Client.getObject("ovo-comms-platform-config", s"comms-burrow-polling-lambda/$environment/application.conf")){ s3Obj =>
using(new InputStreamReader(s3Obj.getObjectContent)){ configReader =>
ConfigFactory.parseReader(configReader)
}
}
val libratoEmail = config.getString("librato.api.email")
val libratoToken = config.getString("librato.api.token")
val libratoUrl = config.getString("librato.api.url")
val kafkaMetricsUrl = config.getString("kafka.metrics.url")
val kafkaConsumerGroups = config.getStringList("kafka.metrics.consumerGroups").asScala
val httpClient = new OkHttpClient()
val metricsClient = new KafkaMetricsClient(kafkaMetricsUrl, "local", kafkaConsumerGroups, httpClient)
val metrics = metricsClient.getMetrics.map(genKafkaMetrics)
using(new LibratoClient(libratoEmail, libratoToken, libratoUrl, environment)){ libratoClient =>
metrics.foreach { metric =>
libratoClient.addMetrics(metric)
}
libratoClient.submitMetrics()
}
}
private def genKafkaMetrics(response :ConsumerGroupMetricResponse): KafkaMetrics = {
val partitionData = response.status.partitions.map { partition =>
PartitionData(partition.partition, partition.end.lag)
}
KafkaMetrics(response.status.group, partitionData)
}
}
示例2: createBucket
//设置package包名称以及导入依赖的类
package akka.persistence.s3
import java.io.InputStream
import com.amazonaws.auth.{ BasicAWSCredentials, DefaultAWSCredentialsProviderChain }
import com.amazonaws.services.s3.{ S3ClientOptions, AmazonS3Client }
import com.amazonaws.services.s3.model._
import scala.concurrent.{ Future, ExecutionContext }
trait S3Client {
val s3ClientConfig: S3ClientConfig
lazy val client: AmazonS3Client = {
val client =
if (s3ClientConfig.awsUseDefaultCredentialsProviderChain)
new AmazonS3Client(new DefaultAWSCredentialsProviderChain).withRegion(s3ClientConfig.region)
else
new AmazonS3Client(new BasicAWSCredentials(s3ClientConfig.awsKey, s3ClientConfig.awsSecret))
s3ClientConfig.endpoint.foreach { endpoint =>
client.withEndpoint(endpoint)
()
}
client.setS3ClientOptions(new S3ClientOptions()
.withPathStyleAccess(s3ClientConfig.options.pathStyleAccess)
.withChunkedEncodingDisabled(s3ClientConfig.options.chunkedEncodingDisabled))
client
}
def createBucket(bucketName: String)(implicit ec: ExecutionContext): Future[Bucket] = Future {
client.createBucket(bucketName)
}
def deleteBucket(bucketName: String)(implicit ec: ExecutionContext): Future[Unit] = Future {
client.deleteBucket(bucketName)
}
def putObject(bucketName: String, key: String, input: InputStream, metadata: ObjectMetadata)(implicit ec: ExecutionContext): Future[PutObjectResult] = Future {
client.putObject(new PutObjectRequest(bucketName, key, input, metadata))
}
def getObject(bucketName: String, key: String)(implicit ec: ExecutionContext): Future[S3Object] = Future {
client.getObject(new GetObjectRequest(bucketName, key))
}
def listObjects(request: ListObjectsRequest)(implicit ec: ExecutionContext): Future[ObjectListing] = Future {
client.listObjects(request)
}
def deleteObject(bucketName: String, key: String)(implicit ec: ExecutionContext): Future[Unit] = Future {
client.deleteObject(bucketName, key)
}
def deleteObjects(request: DeleteObjectsRequest)(implicit ec: ExecutionContext): Future[Unit] = Future {
client.deleteObjects(request)
}
}
示例3: S3FileDetails
//设置package包名称以及导入依赖的类
package aws.s3
import java.io.ByteArrayInputStream
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.{AmazonS3Exception, ListObjectsV2Request, ObjectMetadata}
import org.apache.commons.compress.utils.IOUtils
import scala.collection.JavaConverters._
import play.api.Logger
case class S3FileDetails(contents: Array[Byte], key: String, bucket: String)
class AmazonS3ClientWrapper(client: AmazonS3Client) {
def uploadFile(fileDetails: S3FileDetails): Either[String, String] = {
val stream = new ByteArrayInputStream(fileDetails.contents)
try {
val meta = new ObjectMetadata()
meta.setContentLength(fileDetails.contents.length)
client.putObject(fileDetails.bucket, fileDetails.key, stream, meta)
Logger.info(s"Uploaded file to S3: ${fileDetails.bucket} - ${fileDetails.key}")
Right(client.getResourceUrl(fileDetails.bucket, fileDetails.key))
} catch {
case e: AmazonS3Exception =>
Left(s"Failed to upload to aws.s3 with error: ${e.getMessage} for file: ${fileDetails.key} ")
} finally {
IOUtils.closeQuietly(stream)
}
}
def downloadFile(bucket: String, key: String): Either[String, Array[Byte]] = {
try {
val obj = client.getObject(bucket, key)
val stream = obj.getObjectContent
try {
Right(IOUtils.toByteArray(stream))
} finally {
stream.close()
}
} catch {
case e: AmazonS3Exception =>
// either the object does not exist or something went really wrong
Logger.warn(s"Failed to download aws.s3://$bucket/$key", e)
Left(s"Failed to download s3://$bucket/$key, with status code ${e.getStatusCode}")
}
}
// Returns keys of all the files in specified s3 bucket with the given prefix
def listFiles(bucket: String, prefix: String): Either[String, Seq[String]] = {
try {
val request = new ListObjectsV2Request().withBucketName(bucket).withPrefix(prefix)
val result = client.listObjectsV2(request).getObjectSummaries.asScala.map(_.getKey)
Right(result)
} catch {
case e: AmazonS3Exception =>
Logger.warn(s"Failed to list objects under s3://$bucket/$prefix", e)
Left(s"Failed to retrieve template files from s3://$bucket/$prefix")
}
}
}
示例4: AwsContextProvider
//设置package包名称以及导入依赖的类
package aws
import com.amazonaws.auth.{
AWSCredentialsProviderChain,
AWSStaticCredentialsProvider,
BasicAWSCredentials,
ContainerCredentialsProvider
}
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.s3.AmazonS3Client
import play.api.Logger
object AwsContextProvider {
def genContext(isRunningInCompose: Boolean, region: Regions): (AmazonDynamoDBClient, AmazonS3Client) = {
if (isRunningInCompose) {
Logger.info("Running in compose")
System.setProperty("com.amazonaws.sdk.disableCertChecking", "true")
val awsCreds = new AWSStaticCredentialsProvider(new BasicAWSCredentials("key", "secret"))
val dClient: AmazonDynamoDBClient = new AmazonDynamoDBClient(awsCreds).withRegion(region)
val s3Client: AmazonS3Client = new AmazonS3Client(awsCreds).withRegion(region)
dClient.setEndpoint(sys.env("LOCAL_DYNAMO"))
(dClient, s3Client)
} else {
val awsCreds = new AWSCredentialsProviderChain(
new ContainerCredentialsProvider(),
new ProfileCredentialsProvider()
)
(new AmazonDynamoDBClient(awsCreds).withRegion(region), new AmazonS3Client(awsCreds).withRegion(region))
}
}
}
示例5: TemplatesContext
//设置package包名称以及导入依赖的类
package com.ovoenergy.comms.templates
import cats.Id
import com.amazonaws.auth.AWSCredentialsProvider
import com.amazonaws.services.s3.AmazonS3Client
import com.ovoenergy.comms.model.CommManifest
import com.ovoenergy.comms.templates.cache.CachingStrategy
import com.ovoenergy.comms.templates.model.HandlebarsTemplate
import com.ovoenergy.comms.templates.model.template.processed.CommTemplate
import com.ovoenergy.comms.templates.parsing.Parsing
import com.ovoenergy.comms.templates.parsing.handlebars.HandlebarsParsing
import com.ovoenergy.comms.templates.retriever.{PartialsS3Retriever, TemplatesRetriever, TemplatesS3Retriever}
import com.ovoenergy.comms.templates.s3.AmazonS3ClientWrapper
object TemplatesContext {
def customCachingContext(
credentialsProvider: AWSCredentialsProvider,
cachingStrategy: CachingStrategy[CommManifest, ErrorsOr[CommTemplate[Id]]]): TemplatesContext = {
val s3Client = new AmazonS3ClientWrapper(new AmazonS3Client(credentialsProvider))
TemplatesContext(
templatesRetriever = new TemplatesS3Retriever(s3Client),
parser = new HandlebarsParsing(new PartialsS3Retriever(s3Client)),
cachingStrategy = cachingStrategy
)
}
}
case class TemplatesContext(
templatesRetriever: TemplatesRetriever,
parser: Parsing[HandlebarsTemplate],
cachingStrategy: CachingStrategy[CommManifest, ErrorsOr[CommTemplate[Id]]]
)
示例6: getUTF8TextFileContent
//设置package包名称以及导入依赖的类
package com.ovoenergy.comms.templates.s3
import java.nio.charset.StandardCharsets
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.{AmazonS3Exception, ListObjectsV2Request}
import com.amazonaws.util.IOUtils
import org.slf4j.LoggerFactory
import scala.collection.JavaConverters._
trait S3Client {
def getUTF8TextFileContent(key: String): Option[String]
def listFiles(prefix: String): Seq[String]
}
class AmazonS3ClientWrapper(client: AmazonS3Client) extends S3Client {
private val log = LoggerFactory.getLogger(getClass)
private val TemplatesBucket = "ovo-comms-templates"
override def getUTF8TextFileContent(key: String): Option[String] = {
try {
Option(client.getObject(TemplatesBucket, key)).map(obj => {
val stream = obj.getObjectContent
try {
new String(IOUtils.toByteArray(stream), StandardCharsets.UTF_8)
} finally {
stream.close()
}
})
} catch {
case e: AmazonS3Exception =>
// either the object does not exist or something went really wrong
if (e.getStatusCode != 404)
log.warn(s"Failed to download s3://$TemplatesBucket/$key", e)
None
}
}
override def listFiles(prefix: String): Seq[String] = {
try {
val request = new ListObjectsV2Request().withBucketName(TemplatesBucket).withPrefix(prefix)
client.listObjectsV2(request).getObjectSummaries.asScala.map(_.getKey)
} catch {
case e: AmazonS3Exception =>
log.warn(s"Failed to list objects under s3://$TemplatesBucket/$prefix", e)
Nil
}
}
}
示例7: UploaderTest
//设置package包名称以及导入依赖的类
package com.knoldus.spark.s3
import java.io.File
import com.amazonaws.AmazonClientException
import com.amazonaws.services.s3.model.{PutObjectRequest, PutObjectResult}
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.transfer.{MultipleFileUpload, TransferManager}
import org.mockito.Mockito._
import org.scalatest.FunSuite
import org.specs2.mock.Mockito
class UploaderTest extends FunSuite with Mockito {
private val transferManager = mock[TransferManager]
private val amazonS3Client = mock[AmazonS3Client]
private val multipleFileUpload = mock[MultipleFileUpload]
private val putObjectResult = mock[PutObjectResult]
private val file = new File("src/test/resources/sample.json")
private val filePath = "src/test/resources/sample.json"
private val bucket = "bucket"
private val uploader = new Uploader(transferManager)
test("upload is successful") {
when(transferManager.uploadDirectory(any[String], any[String], any[File], any[Boolean])).thenReturn(multipleFileUpload)
val result = uploader.uploadDirectory(file, filePath, bucket)
assert(result)
}
test("upload is not successful") {
when(transferManager.uploadDirectory(any[String], any[String], any[File], any[Boolean])).thenThrow(new IllegalArgumentException)
val result = uploader.uploadDirectory(file, filePath, bucket)
assert(!result)
}
test("upload partition is successful") {
when(transferManager.getAmazonS3Client).thenReturn(amazonS3Client)
when(amazonS3Client.putObject(any[PutObjectRequest])).thenReturn(putObjectResult)
val result = uploader.uploadPartition(new File("src"), filePath, bucket)
assert(result)
}
test("upload partition is not successful") {
when(transferManager.getAmazonS3Client).thenReturn(amazonS3Client)
when(amazonS3Client.putObject(any[PutObjectRequest])).thenThrow(new AmazonClientException("exception"))
val result = uploader.uploadPartition(new File("src"), filePath, bucket)
assert(!result)
}
}
示例8: autoScalingClient
//设置package包名称以及导入依赖的类
package utils
import com.amazonaws.auth.AWSCredentialsProvider
import com.amazonaws.services.autoscaling.{AmazonAutoScaling, AmazonAutoScalingClient}
import com.amazonaws.services.cloudformation.{AmazonCloudFormation, AmazonCloudFormationClient}
import com.amazonaws.services.elasticloadbalancing.{AmazonElasticLoadBalancing, AmazonElasticLoadBalancingClient}
import com.amazonaws.services.s3.{AmazonS3, AmazonS3Client}
trait AmazonAutoScalingService {
def autoScalingClient(credentials: AWSCredentialsProvider): AmazonAutoScaling = new AmazonAutoScalingClient(credentials)
}
trait AmazonCloudFormationService {
def cloudFormationClient(credentials: AWSCredentialsProvider): AmazonCloudFormation = new AmazonCloudFormationClient(credentials)
}
trait AmazonElasticLoadBalancingService {
def elasticLoadBalancingClient(credentials: AWSCredentialsProvider): AmazonElasticLoadBalancing = new AmazonElasticLoadBalancingClient(credentials)
}
trait AmazonS3Service {
def s3Client(credentials: AWSCredentialsProvider): AmazonS3 = new AmazonS3Client(credentials)
}
示例9: FileUploadService
//设置package包名称以及导入依赖的类
package ylabs.play.common.services
import java.io.InputStream
import java.net.URI
import java.time.Instant
import java.util.{Date, UUID}
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.ObjectMetadata
import com.typesafe.config.ConfigFactory
import ylabs.play.common.models.FileUpload.FileDescription
import scala.concurrent.{ExecutionContext, Future}
object FileUploadService {
val expirationYears:Long = 20
val binExpirationTime: Long = expirationYears*365L*24L*60L*60L*1000L
}
class FileUploadService {
lazy val s3 = new AmazonS3Client
lazy val config = ConfigFactory.load()
lazy val bucketName = config.getString("aws.s3.bucket")
def upload(file: InputStream, description: Option[FileDescription])(implicit ec: ExecutionContext): Future[URI] = {
Future {
if (!s3.doesBucketExist(bucketName)) s3.createBucket(bucketName)
val metadata = new ObjectMetadata
metadata.addUserMetadata("Description", description.getOrElse(FileDescription("None")).value)
val id = UUID.randomUUID().toString
val putObj = s3.putObject(bucketName, id, file, metadata)
val expireDate = Date.from(Instant.ofEpochMilli(new Date().getTime + FileUploadService.binExpirationTime))
s3.generatePresignedUrl(bucketName, id, expireDate).toURI
}
}
}
示例10: S3Service
//设置package包名称以及导入依赖的类
package services
import java.io.InputStream
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.services.s3.{AmazonS3, AmazonS3Client}
import com.amazonaws.services.s3.model._
import play.api.Logger
import scala.util.{Try, Failure, Success}
object S3Service {
val logger: Logger = Logger("S3ServiceLogger")
lazy val s3Client: AmazonS3 = initialize()
def initialize(): AmazonS3Client = {
try {
val s3Client: AmazonS3Client = new AmazonS3Client(new ProfileCredentialsProvider())
logger.info("The process of getting credentials and creating s3Client object completed successfully")
s3Client
} catch {
case e: Exception => {
logger.error("An error occurred while getting credentials and creating s3Client object")
throw e
}
}
}
def storeDataToAmazonS3(key: String, inputStream: InputStream): Try[Unit] = {
try {
val lenght: Int = inputStream.available()
val objectMetadata: ObjectMetadata = new ObjectMetadata()
objectMetadata.setContentLength(lenght)
val request: PutObjectRequest = new PutObjectRequest(ConfigurationService.bucketName, key, inputStream, objectMetadata)
s3Client.putObject(request)
logger.info("Request was transmitted to Amazon S3 successfully")
Success(Unit)
} catch {
case e: Exception =>{
Failure(e)
}
}
}
}
示例11: AmazonS3ClientWrapper
//设置package包名称以及导入依赖的类
package aws.s3
import java.nio.charset.StandardCharsets
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.AmazonS3Exception
import com.amazonaws.util.IOUtils
import play.api.Logger
import repo.EmailBodyRepo.ErrorOr
object AmazonS3ClientWrapper {
case class S3FileDetails(contents: String, key: String, bucket: String)
case class S3Config(bucketName: String, emailbodyHtmlFolder: String, emailbodyTextFolder: String)
type ResourceUrl = String
def uploadFile(client: AmazonS3Client)(fileDetails: S3FileDetails): ErrorOr[ResourceUrl] = {
try {
client.putObject(fileDetails.bucket, fileDetails.key, fileDetails.contents)
Right(client.getResourceUrl(fileDetails.bucket, fileDetails.key))
} catch {
case e: AmazonS3Exception =>
Left(s"Failed to upload to aws.s3 with error: ${e.getMessage} for file: ${fileDetails.key} ")
}
}
def downloadFile(client: AmazonS3Client)(bucket: String, key: String): ErrorOr[String] = {
try {
val obj = client.getObject(bucket, key)
val stream = obj.getObjectContent
try {
Right(new String(IOUtils.toByteArray(stream), StandardCharsets.UTF_8))
} finally {
stream.close()
}
} catch {
case e: AmazonS3Exception =>
// either the object does not exist or something went really wrong
Logger.warn(s"Failed to download aws.s3://$bucket/$key", e)
Left(s"Failed to download s3://$bucket/$key, with status code ${e.getStatusCode}")
}
}
}
示例12: AwsClientProvider
//设置package包名称以及导入依赖的类
package aws
import com.amazonaws.auth.{
AWSCredentialsProviderChain,
AWSStaticCredentialsProvider,
BasicAWSCredentials,
ContainerCredentialsProvider
}
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.s3.AmazonS3Client
import play.api.Logger
object AwsClientProvider {
def genClients(runningInDockerOnDevMachine: Boolean, region: Regions): (AmazonDynamoDBClient, AmazonS3Client) = {
if (runningInDockerOnDevMachine) {
Logger.info("Running in compose")
System.setProperty("com.amazonaws.sdk.disableCertChecking", "true")
val awsCreds = new AWSStaticCredentialsProvider(new BasicAWSCredentials("key", "secret"))
val dClient: AmazonDynamoDBClient = new AmazonDynamoDBClient(awsCreds).withRegion(region)
val s3Client: AmazonS3Client = new AmazonS3Client(awsCreds).withRegion(region)
dClient.setEndpoint(sys.env("LOCAL_DYNAMO"))
(dClient, s3Client)
} else {
val awsCreds = new AWSCredentialsProviderChain(
new ContainerCredentialsProvider(),
new ProfileCredentialsProvider("default")
)
(new AmazonDynamoDBClient(awsCreds).withRegion(region), new AmazonS3Client(awsCreds).withRegion(region))
}
}
}
示例13: AmazonHelpers
//设置package包名称以及导入依赖的类
package com.iheart.lambda
import java.text.SimpleDateFormat
import java.util.Date
import com.amazonaws.services.logs.AWSLogsClient
import com.amazonaws.services.logs.model._
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.GetObjectRequest
import scala.collection.JavaConverters._
import scala.io.Source
object AmazonHelpers {
val s3Client = new AmazonS3Client()
val cwlClient = new AWSLogsClient()
val cwlLogGroup = "/aws/lambda/fastlyLogProcessorSkips"
//val cwlLogStream = "Skips"
def cwlLogStream() = {
val s = new SimpleDateFormat("YYYY-MMDD-HH-mm")
s.format(new Date())
}
def readFileFromS3(bucket: String, key: String) = {
val s3Object = s3Client.getObject(new GetObjectRequest(bucket,key))
Source.fromInputStream(s3Object.getObjectContent).getLines()
}
def getCloudSeqToken(logStream: String) = {
val req = new DescribeLogStreamsRequest(cwlLogGroup).withLogStreamNamePrefix(logStream)
val descResult = cwlClient.describeLogStreams(req)
if (descResult != null && descResult.getLogStreams != null && !descResult.getLogStreams.isEmpty) {
descResult.getLogStreams.asScala.last.getUploadSequenceToken
}
else {
println("Creating log stream " + logStream)
cwlClient.createLogStream(new CreateLogStreamRequest(cwlLogGroup,logStream))
null
}
}
def sendCloudWatchLog(log: String) = {
println("Skipping cloudwatch log: " + log)
val logStream = cwlLogStream()
val token = getCloudSeqToken(logStream)
println("token is : " + token)
val event = new InputLogEvent
event.setTimestamp(new Date().getTime)
event.setMessage(log)
val req = new PutLogEventsRequest(cwlLogGroup,logStream,List(event).asJava)
req.setSequenceToken(token)
cwlClient.putLogEvents(req)
}
}
示例14: S3PlainClient
//设置package包名称以及导入依赖的类
package uk.gov.homeoffice.aws.s3
import java.net.URL
import com.amazonaws.ClientConfiguration
import com.amazonaws.auth.AWSCredentials
import com.amazonaws.services.s3.model.{CryptoConfiguration, EncryptionMaterialsProvider}
import com.amazonaws.services.s3.{AmazonS3, AmazonS3Client, AmazonS3EncryptionClient}
trait S3Client extends AmazonS3
class S3PlainClient(endpoint: URL, credentials: AWSCredentials)(implicit clientConfiguration: ClientConfiguration = new ClientConfiguration) extends AmazonS3Client(credentials, clientConfiguration) with S3Client {
setEndpoint(endpoint.toString)
def clientConfig = clientConfiguration
}
class S3EncryptionClient(endpoint: URL, credentials: AWSCredentials,
encryptionMaterialsProvider: EncryptionMaterialsProvider, cryptoConfiguration: CryptoConfiguration)
(implicit clientConfiguration: ClientConfiguration = new ClientConfiguration)
extends AmazonS3EncryptionClient(credentials, encryptionMaterialsProvider, clientConfiguration, cryptoConfiguration) with S3Client {
setEndpoint(endpoint.toString)
def clientConfig = clientConfiguration
}
示例15: S3ConfigurationSource
//设置package包名称以及导入依赖的类
package com.gu.cm
import com.amazonaws.auth.{AWSCredentialsProvider, DefaultAWSCredentialsProviderChain}
import com.amazonaws.regions.RegionUtils
import com.amazonaws.regions.ServiceAbbreviations.S3
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.GetObjectRequest
import com.typesafe.config.{Config, ConfigFactory}
import scala.util.{Failure, Success, Try}
class S3ConfigurationSource(s3: AmazonS3Client, identity: Identity, bucket: String, version: Option[Int]) extends ConfigurationSource {
override def load: Config = {
val configPath = versionedFilePath(identity, version)
val request = new GetObjectRequest(bucket, configPath)
val config = for {
result <- Try(s3.getObject(request))
item <- Try(scala.io.Source.fromInputStream(result.getObjectContent, "UTF-8").mkString)
} yield {
ConfigFactory.parseString(item)
}
config match {
case Success(theConfig) => theConfig
case Failure(theFailure) => ConfigFactory.empty(s"no s3 config (or failed to load) for bucket=$bucket path=$configPath, exception=[$theFailure]")
}
}
def versionedFilePath(identity: Identity, version:Option[Int]): String = {
val fileVersion = version.map(".v" + _).getOrElse("")
s"config/${identity.region}-${identity.stack}$fileVersion.conf"
}
}
object S3ConfigurationSource {
def apply(identity: Identity, bucket: String, credentials: AWSCredentialsProvider = new DefaultAWSCredentialsProviderChain(), version: Option[Int] = None): S3ConfigurationSource = {
val s3 = {
val client = new AmazonS3Client(credentials)
client.setRegion(RegionUtils.getRegion(identity.region))
client.setEndpoint(RegionUtils.getRegion(identity.region).getServiceEndpoint(S3))
client
}
new S3ConfigurationSource(s3, identity, bucket, version)
}
}