当前位置: 首页>>代码示例>>Scala>>正文


Scala AmazonS3ClientBuilder类代码示例

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


在下文中一共展示了AmazonS3ClientBuilder类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: LambdaMain

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

import java.util.Locale

import com.amazonaws.services.kms.AWSKMSClientBuilder
import com.amazonaws.services.lambda.runtime.events.S3Event
import com.amazonaws.services.lambda.runtime.{Context, RequestHandler}
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import com.amazonaws.services.s3.event.S3EventNotification.S3Entity
import com.amazonaws.services.s3.model.S3Object
import com.dropbox.core.DbxRequestConfig
import com.dropbox.core.v2.DbxClientV2
import com.dropbox.core.v2.files.WriteMode
import com.s3dropbox.lambda.LambdaMain._
import com.s3dropbox.lambda.ZipFileIterator.ZipFileEntry
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream


final class LambdaMain extends RequestHandler[S3Event, Unit] {

  override def handleRequest(event: S3Event, context: Context): Unit = {
    val kmsDecryption: KMSDecryption = new KMSDecryption(AWSKMSClientBuilder.defaultClient())
    val dbxToken: String = kmsDecryption.decrypt(sys.env(EncryptedDbTokenEnvVar))

    val config: DbxRequestConfig =
      DbxRequestConfig
        // the clientIdentifier might be the app key...
        .newBuilder(context.getFunctionName)
        .withAutoRetryEnabled()
        .withUserLocaleFrom(Locale.US)
        .build()

    val dbxClient: DbxClientV2 = new DbxClientV2(config, dbxToken)

    val s3entity: S3Entity = event.getRecords.get(event.getRecords.size - 1).getS3
    val s3obj: S3Object = AmazonS3ClientBuilder
      .defaultClient()
      .getObject(s3entity.getBucket.getName, s3entity.getObject.getKey)

    val zipFileIter: ZipFileIterator = new ZipFileIterator(s3obj.getObjectContent)
    zipFileIter.foreach((zentry: ZipFileEntry) => {
      println(s"Uploading [${zentry.filename}] to Dropbox")
      dbxClient
        .files()
        .uploadBuilder(s"/${zentry.filename}")
        .withMode(WriteMode.OVERWRITE)
        .uploadAndFinish(new ByteInputStream(zentry.data, zentry.data.length))
    })
    zipFileIter.close()
  }
}

private object LambdaMain {
  var EncryptedDbTokenEnvVar: String = "EncryptedDropboxToken"
} 
开发者ID:ErrorsAndGlitches,项目名称:S3DropboxLambda,代码行数:56,代码来源:LambdaMain.scala

示例2: ApplicationStartupS3DAO

//设置package包名称以及导入依赖的类
package daos

import java.io.{File, FileOutputStream, InputStream}
import javax.inject.{Inject, Singleton}

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.regions.Regions
import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
import org.apache.commons.io.IOUtils
import play.api.Configuration



@Singleton
class ApplicationStartupS3DAO @Inject()(config: Configuration) {
  private val s3: AmazonS3 = AmazonS3ClientBuilder.standard()
      .withCredentials(new DefaultAWSCredentialsProviderChain())
      .withRegion(Regions.EU_WEST_2)
      .build()
  private val bucketName = config.underlying.getString("s3-static")

  def downloadImageToTempFile(key: String): File = {
    val inputStream: InputStream = s3.getObject(bucketName, key).getObjectContent
    val tempFile = File.createTempFile(s"temp-file-$key", ".tmp")
    tempFile.deleteOnExit()
    val out = new FileOutputStream(tempFile)
    IOUtils.copy(inputStream, out)
    inputStream.close()
    out.close()
    tempFile
  }
} 
开发者ID:muhsinali,项目名称:picture-gallery-scala,代码行数:33,代码来源:ApplicationStartupS3DAO.scala

示例3: pushTemplate

//设置package包名称以及导入依赖的类
package uk.co.telegraph.cloud.aws

import com.amazonaws.services.s3.transfer.{TransferManager, TransferManagerBuilder}
import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
import sbt.{File, Logger, URI}
import uk.co.telegraph.cloud.AuthCredentials

import AwsS3Bucket._

private [aws] trait AwsS3Bucket { this: AwsClientWithAuth =>


  lazy val s3Client: AmazonS3 = AmazonS3ClientBuilder.standard()
    .withRegion     ( region )
    .withCredentials( authProvider )
    .build()

  lazy val transferManager: TransferManager = TransferManagerBuilder.standard()
    .withS3Client(s3Client)
    .build()

  
  def pushTemplate(storageUrl:URI, localPath:File):Unit = {
    if( storageUrl.getScheme != "s3" ){
      throw S3InvalidProtocol
    }
    if( !localPath.exists() ){
      throw S3InvalidLocalPath
    }

    val s3Bucket = storageUrl.getHost
    val s3Key    = storageUrl.getPath.replaceFirst("^/", "")

    if (localPath.isDirectory) {
      transferManager.uploadDirectory(s3Bucket, s3Key, localPath, true).waitForCompletion()
    } else {
      transferManager.upload(s3Bucket, s3Key, localPath).waitForCompletion()
    }
  }
}

object AwsS3Bucket {
  object S3InvalidProtocol extends Exception{
    override def getMessage: String = "S3Client - Invalid protocol for S3 Path (s3://{s3Bucket}/{s3Key})."
  }

  object S3InvalidLocalPath extends Exception {
    override def getMessage: String = "S3Client - Invalid LocalPath - Path does not exist."
  }

  private case class AwsS3BucketImp(region:String, authCredentials: AuthCredentials, log: Logger)
    extends AwsS3Bucket with AwsClientWithAuth

  def apply(region:String, authCredentials: AuthCredentials)(implicit log:Logger):AwsS3Bucket = {
    AwsS3BucketImp(region, authCredentials, log)
  }
} 
开发者ID:telegraph,项目名称:sbt-pipeline-plugin,代码行数:58,代码来源:AwsS3Bucket.scala

示例4: BucketFiles

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

import java.nio.file.Path

import akka.stream.scaladsl.StreamConverters
import com.amazonaws.regions.Regions
import com.amazonaws.services.s3.model.ObjectListing
import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
import com.malliina.storage.StorageLong

import scala.collection.JavaConversions._

class BucketFiles(val aws: AmazonS3, val bucket: BucketName) extends PicFiles {
  val bucketName: String = bucket.name

  override def load(from: Int, until: Int): Seq[Key] = {
    val size = until - from
    if (size <= 0) Nil
    else loadAcc(until, aws.listObjects(bucketName), Nil).drop(from)
  }

  private def loadAcc(desiredSize: Int, current: ObjectListing, acc: Seq[Key]): Seq[Key] = {
    val newAcc = acc ++ current.getObjectSummaries.map(s => Key(s.getKey))
    if (!current.isTruncated || newAcc.size >= desiredSize) newAcc take desiredSize
    else loadAcc(desiredSize, aws.listNextBatchOfObjects(current), newAcc)
  }

  override def contains(key: Key): Boolean =
    aws.doesObjectExist(bucketName, key.key)

  override def get(key: Key): DataStream = {
    val obj = aws.getObject(bucketName, key.key)
    val meta = obj.getObjectMetadata
    DataStream(
      StreamConverters.fromInputStream(obj.getObjectContent),
      Option(meta.getContentLength).map(_.bytes),
      Option(meta.getContentType).map(ContentType.apply)
    )
  }

  override def put(key: Key, file: Path): Unit =
    aws.putObject(bucketName, key.key, file.toFile)

  override def remove(key: Key): Unit =
    aws.deleteObject(bucketName, key.key)
}

object BucketFiles {
  def forBucket(bucket: BucketName) = forS3(Regions.EU_WEST_1, bucket)

  def forS3(region: Regions, bucket: BucketName): BucketFiles = {
    val bucketName = bucket.name
    val aws = AmazonS3ClientBuilder.standard().withRegion(region).build()
    if (!aws.doesBucketExist(bucketName))
      aws.createBucket(bucketName)
    new BucketFiles(aws, bucket)
  }
} 
开发者ID:malliina,项目名称:pics,代码行数:59,代码来源:BucketFiles.scala

示例5: DigitalElementIndexModule

//设置package包名称以及导入依赖的类
package utils

import java.io.{BufferedInputStream, InputStream}
import java.nio.file.{Files, Paths}

import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
import com.google.inject.name.Named
import com.google.inject.{AbstractModule, Provides, Singleton}
import play.api.Logger

class DigitalElementIndexModule extends AbstractModule {

  private[this] val fileUri = "^file:(.*)".r
  private[this] val s3Uri = "^s3://([^/]+)/(.*)".r

  override def configure(): Unit = ()

  @Provides
  @Named("DigitalElementIndex")
  @Singleton
  def getIndex(@javax.inject.Inject() environmentVariables: EnvironmentVariables): DigitalElementIndex = {
    val is: InputStream = environmentVariables.digitalElementFileUri match {
      case fileUri(path) => new BufferedInputStream(Files.newInputStream(Paths.get(path)))
      case s3Uri(bucket, key) => {
        val s3: AmazonS3 = AmazonS3ClientBuilder.standard().build()
        s3.getObject(bucket, key).getObjectContent()
      }
      case _ => throw new IllegalArgumentException("Invalid digitalElementFileUri.  Must use either s3:// or file:// protocol")
    }
    Logger.info(s"Building index from ${environmentVariables.digitalElementFileUri}")
    val start = System.currentTimeMillis()
    val index = DigitalElement.buildIndex(is, ';', '\n')
    is.close()
    Logger.info(s"Indexed ${index.size} records in ${(System.currentTimeMillis() - start) / 1000} seconds")
    index
  }

} 
开发者ID:flowcommerce,项目名称:location,代码行数:39,代码来源:DigitalElementIndexModule.scala

示例6: ObjectStorageUtils

//设置package包名称以及导入依赖的类
package org.alcaudon.runtime

import java.net.URL
import java.util.Date

import com.amazonaws.HttpMethod
import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest
import org.alcaudon.runtime.BlobLocation.AWSInformation

import scala.concurrent.duration._

object ObjectStorageUtils {

  def sign(bucketName: String, objectKey: String)(
      implicit awsInfo: AWSInformation): URL = {

    val s3client = AmazonS3ClientBuilder
      .standard()
      .withCredentials(new AWSStaticCredentialsProvider(awsInfo.credentials))
      .withRegion(awsInfo.region)
      .build()

    val expiration = new Date
    var milliSeconds = expiration.getTime
    milliSeconds += 1.hour.toMillis

    expiration.setTime(milliSeconds)

    val generatePresignedUrlRequest =
      new GeneratePresignedUrlRequest(bucketName, objectKey)
    generatePresignedUrlRequest.setMethod(HttpMethod.PUT)
    generatePresignedUrlRequest.setExpiration(expiration)

    s3client.generatePresignedUrl(generatePresignedUrlRequest)
  }
} 
开发者ID:fcofdez,项目名称:alcaudon,代码行数:39,代码来源:ObjectStorageUtils.scala


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