• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala AmazonS3Client类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中com.amazonaws.services.s3.AmazonS3Client的典型用法代码示例。如果您正苦于以下问题:Scala AmazonS3Client类的具体用法?Scala AmazonS3Client怎么用?Scala AmazonS3Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了AmazonS3Client类的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
  }
} 
开发者ID:ovotech,项目名称:comms-burrow-polling-lambda,代码行数:52,代码来源:Lambda.scala


示例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)
  }
} 
开发者ID:TanUkkii007,项目名称:akka-persistence-s3,代码行数:59,代码来源:S3Client.scala


示例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")
    }
  }
} 
开发者ID:ovotech,项目名称:comms-template-manager,代码行数:62,代码来源:AmazonS3ClientWrapper.scala


示例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))
    }
  }
} 
开发者ID:ovotech,项目名称:comms-template-manager,代码行数:35,代码来源:AwsContextProvider.scala


示例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]]]
) 
开发者ID:ovotech,项目名称:comms-templates,代码行数:35,代码来源:TemplatesContext.scala


示例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
    }
  }

} 
开发者ID:ovotech,项目名称:comms-templates,代码行数:57,代码来源:S3Client.scala


示例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)
  }

} 
开发者ID:knoldus,项目名称:spark-s3,代码行数:51,代码来源:UploaderTest.scala


示例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)
} 
开发者ID:lifeway,项目名称:Chadash,代码行数:24,代码来源:Components.scala


示例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
    }
  }
} 
开发者ID:springnz,项目名称:play-app-base,代码行数:37,代码来源:FileUploadService.scala


示例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)
      }
    }
  }
} 
开发者ID:emrekarakis,项目名称:AmazonProject,代码行数:51,代码来源:S3Service.scala


示例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}")

    }
  }
} 
开发者ID:ovotech,项目名称:comms-audit-log,代码行数:47,代码来源:AmazonS3ClientWrapper.scala


示例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))
    }
  }
} 
开发者ID:ovotech,项目名称:comms-audit-log,代码行数:35,代码来源:AwsClientProvider.scala


示例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)
  }
} 
开发者ID:denen99,项目名称:fastly-lambda,代码行数:57,代码来源:AmazonHelpers.scala


示例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
} 
开发者ID:UKHomeOffice,项目名称:aws-scala-lib,代码行数:26,代码来源:S3Client.scala


示例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)
  }
} 
开发者ID:guardian,项目名称:configuration-magic,代码行数:47,代码来源:S3ConfigurationSource.scala


示例16: s3ConfigurationSourceSpec

//设置package包名称以及导入依赖的类
package com.gu.cm

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.regions.RegionUtils
import com.amazonaws.regions.ServiceAbbreviations._
import com.amazonaws.services.s3.AmazonS3Client
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

class s3ConfigurationSourceSpec extends Specification {
  "a s3 configuration Source" should {
    "look for a versioned file if a version is present" in new s3Scope {
      val version = 2
      val filePath = s3ConfigurationSource.versionedFilePath(identity, Some(version))
      filePath shouldEqual "config/eu-west-1-test-stack.v2.conf"
    }

    "look for a non versioned file if a version is not present" in new s3Scope {
      val filePath = s3ConfigurationSource.versionedFilePath(identity, None)
      filePath shouldEqual "config/eu-west-1-test-stack.conf"
    }
  }

  trait s3Scope extends Scope {

    val identity = AwsApplication(
      stack = "test-stack",
      app = "configuration-magic",
      stage = "test",
      region = "eu-west-1"
    )

    val s3 = {
      val client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain())
      client.setRegion(RegionUtils.getRegion(identity.region))
      client.setEndpoint(RegionUtils.getRegion(identity.region).getServiceEndpoint(S3))
      client
    }

    val s3ConfigurationSource = new S3ConfigurationSource(s3, identity, "frontend", Some(1))

  }
} 
开发者ID:guardian,项目名称:configuration-magic,代码行数:44,代码来源:s3ConfigurationSourceSpec.scala


示例17: S3

//设置package包名称以及导入依赖的类
package com.sample.lambda

import java.io.File
import java.nio.file.{Files, StandardCopyOption}

import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.events.S3Event
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.{CannedAccessControlList, ObjectMetadata, PutObjectRequest}
import com.typesafe.config.ConfigFactory
import org.im4java.core.{ConvertCmd, IMOperation}

class S3 {
  def handler(event: S3Event, context: Context): String = {
    val config = ConfigFactory.load()
    val record = event.getRecords.get(0)

    val bucket = record.getS3.getBucket.getName
    val key = record.getS3.getObject.getKey

    val pattern = """(.+)\.(.+)""".r
    val patternThumbnail = """(.+)_thumbnail\.(.+)""".r

    key match {
      case patternThumbnail(name, format) => {

      }
      case pattern(name, format) => {
        val client = new AmazonS3Client()
        val s3Object = client.getObject(bucket, key)

        val inMetadata = s3Object.getObjectMetadata
        val input = s3Object.getObjectContent
        val inputFile = File.createTempFile("s3handler", "input")
        Files.copy(input, inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING)

        val resizedImage = File.createTempFile("s3handler", "resizedImage")

        val op = new IMOperation()
        op.addImage(inputFile.getAbsolutePath)
        op.resize(config.getInt("resized.width"), config.getInt("resized.height"))
        op.addImage(resizedImage.getAbsolutePath)

        val converter = new ConvertCmd()
        converter.run(op)

        val resizedKey = name + "_thumbnail." + format

        val putObjectRequest = new PutObjectRequest(bucket, resizedKey, resizedImage)
        val resizedImageMetadata = new ObjectMetadata()
        resizedImageMetadata.setContentType(inMetadata.getContentType)
        putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead).withMetadata(resizedImageMetadata)

        client.putObject(putObjectRequest)
      }
    }

    key
  }
} 
开发者ID:phji,项目名称:lambda-resize-s3-image,代码行数:61,代码来源:S3.scala


示例18: S3Client

//设置package包名称以及导入依赖的类
package org.personal.durdina.s3dr.io

import java.io.File

import com.amazonaws.auth.{AWSCredentials, BasicAWSCredentials}
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model._

import scala.annotation.tailrec
import scala.collection.JavaConverters._


class S3Client {

  // TODO: externalize
  val accessKey = "AKIAIM4TY5PUZ3VXMDKA"
  val secretKey = "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"

  val credentials: AWSCredentials = new BasicAWSCredentials(accessKey, secretKey)
  val s3client = new AmazonS3Client(credentials)

  def upload(source: File, target: S3File) {
    val request = new PutObjectRequest(target.bucket, target.key, source)
    s3client.putObject(request)
  }

  def listOfFiles(prefix: S3File) = {
    listObjects(prefix.bucket, prefix.key, _.getKey)
  }

  def emptyBucket(bucket: String) {
    listOfFiles(new S3File(bucket, "")).foreach(s3client.deleteObject(bucket, _))
  }

  private def listObjects[T](bucket: String, keyPrefix: String, transformFunc: S3ObjectSummary => T): Seq[T] = {
    @tailrec
    def aggregateObjects(objectListing: ObjectListing, files: List[S3ObjectSummary]): List[S3ObjectSummary] = {
      if (!objectListing.isTruncated)
        files ++ objectListing.getObjectSummaries.asScala
      else
        aggregateObjects(s3client.listNextBatchOfObjects(objectListing), files ++ objectListing.getObjectSummaries.asScala)
    }

    val files = aggregateObjects(s3client.listObjects(bucket, keyPrefix), Nil)
    files.map(transformFunc)
  }

} 
开发者ID:durdina,项目名称:s3-dist-reader,代码行数:49,代码来源:S3Client.scala


示例19: S3

//设置package包名称以及导入依赖的类
package net.shiroka.tools.ofx.aws

import java.io._
import scala.util.control.Exception.catching
import com.amazonaws.regions._
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model._
import com.amazonaws.services.s3.transfer._
import com.netaporter.uri.Uri
import com.typesafe.config.ConfigFactory
import net.shiroka.tools.ofx.implicits.Tapper
import net.shiroka.tools.ofx._

case class S3() {
  val awsConfig = ConfigFactory.load(getClass.getClassLoader)
  val region = awsConfig.getString("net.shiroka.tools.ofx.aws.region")

  lazy val client: AmazonS3Client =
    new AmazonS3Client(getCredentials)
      .tap(_.setRegion(RegionUtils.getRegion(region)))

  def source(uri: Uri): S3ObjectInputStream = {
    val obj = client.getObject(uri.host.get, uri.path.drop(1))
    obj.getObjectContent
  }

  def uploadAndAwait(conversion: Conversion, src: InputStream, originalUri: Uri, suffix: String): Unit =
    closing(new ByteArrayOutputStream().tap(out => conversion.apply(src, out))) { baos =>
      uploadAndAwait(
        bucket = originalUri.host.get,
        key = originalUri.path.drop(1).stripSuffix(suffix) ++ "ofx",
        is = new ByteArrayInputStream(baos.toByteArray),
        size = baos.size
      )
    }

  def uploadAndAwait(originalUri: Uri, suffix: String, baos: ByteArrayOutputStream): Unit =
    uploadAndAwait(
      bucket = originalUri.host.get,
      key = originalUri.path.drop(1).stripSuffix(suffix) ++ "ofx",
      is = new ByteArrayInputStream(baos.toByteArray),
      size = baos.size
    )

  def uploadAndAwait(bucket: String, key: String, is: InputStream, size: Int): Unit =
    new TransferManager(client).tap { transfer =>
      catching(classOf[InterruptedException]).either {
        transfer.upload(
          bucket,
          key,
          is,
          new ObjectMetadata().tap(_.setContentLength(size))
        ).waitForCompletion
      }.fold(err => throw new IOException(err), identity)
    }.shutdownNow()
} 
开发者ID:ikuo,项目名称:ofx-tools,代码行数:57,代码来源:S3.scala



注:本文中的com.amazonaws.services.s3.AmazonS3Client类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala Duration类代码示例发布时间:2022-05-23
下一篇:
Scala GeneralizedLinearRegression类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap