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

Scala AmazonS3类代码示例

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

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



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

示例1: 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


示例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: ListObjectIterator

//设置package包名称以及导入依赖的类
package com.mobilerq.awsutil.s3

import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.model.{ListNextBatchOfObjectsRequest, ListObjectsRequest, ObjectListing}


class ListObjectIterator(request: ListObjectsRequest)(implicit client: AmazonS3) extends Iterator[ObjectListing] {

  def this(bucket: String)(implicit client: AmazonS3) =
    this(new ListObjectsRequest().withBucketName(bucket))

  def this(bucket: String, prefix: String)(implicit client: AmazonS3) =
    this(new ListObjectsRequest().withBucketName(bucket).withPrefix(prefix))

  var last: ObjectListing = _

  override def hasNext: Boolean = last == null || last.isTruncated

  override def next(): ObjectListing = {
    if (!hasNext) throw new IllegalStateException("empty iterator")
    last = if (last == null) {
      client.listObjects(request)
    } else {
      client.listNextBatchOfObjects(new ListNextBatchOfObjectsRequest(last)
        .withGeneralProgressListener[ListNextBatchOfObjectsRequest](request.getGeneralProgressListener)
        .withRequestMetricCollector[ListNextBatchOfObjectsRequest](request.getRequestMetricCollector)
        .withSdkClientExecutionTimeout[ListNextBatchOfObjectsRequest](Option(request.getSdkClientExecutionTimeout).map(_.intValue()).getOrElse(0))
        .withSdkRequestTimeout[ListNextBatchOfObjectsRequest](Option(request.getSdkRequestTimeout).map(_.intValue()).getOrElse(0))
      )
    }
    last
  }

} 
开发者ID:mobilerq,项目名称:mrq-aws-util,代码行数:35,代码来源:ListObjectIterator.scala


示例4: 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


示例5: 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


示例6: 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


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


示例8: 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



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala Configuration类代码示例发布时间:2022-05-23
下一篇:
Scala ThrottleMode类代码示例发布时间: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