本文整理汇总了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"
}
示例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
}
}
示例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)
}
}
示例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)
}
}
示例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
}
}
示例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)
}
}