使用Scala连接到Amazon帐户

时间:2017-12-28 15:03:13

标签: scala amazon-s3 amazon

我想连接到我的亚马逊帐户,以便删除我的s3存储空间内的资源。

我有访问密钥和密钥,这就是我开始建立与亚马逊的连接的方式:

def connectToAmaozn(): Unit = {
  val AWS_ACCESS_KEY=conf.getString("WebRecorder.PushSession.AccessKey")
  val AWS_SECRET_KEY=conf.getString("WebRecorder.PushSession.SecretKey")

  val AWSCredentials = new BasicAWSCredentials(AWS_ACCESS_KEY,AWS_SECRET_KEY)

}

你能详细说明我怎么做吗?

2 个答案:

答案 0 :(得分:1)

我使用此解决方案获取存储桶名称和对象数量:

import scala.collection.JavaConversions._
import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials}
import com.amazonaws.services.s3
import com.amazonaws.services.s3.model.{GetObjectTaggingRequest, ObjectListing, S3ObjectSummary}
import com.amazonaws.services.s3.{AmazonS3Client, AmazonS3ClientBuilder}
import com.clicktale.pipeline.framework.dal.ConfigParser.conf
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model._

import scala.language.postfixOps

class Amazon {

  val AWS_ACCESS_KEY = conf.getString("WebRecorder.PushSession.AccessKey")
  val AWS_SECRET_KEY = conf.getString("WebRecorder.PushSession.SecretKey")
  val bucketName = "nv-q-s3-assets-01"
  val provider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY))
  val client = AmazonS3ClientBuilder.standard().withCredentials(provider).withRegion("us-east-1").build()
//  def connectToAmazon(): Unit = {
//
//    val provider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY))
//    val client = AmazonS3ClientBuilder.standard().withCredentials(provider).withRegion("us-east-1").build()

  def removeObjectsFromBucket(){

    println("Removing objects from bucket...")
      var object_listing: ObjectListing = client.listObjects(bucketName)
      var flag: Boolean = true
      while (flag) {
        val iterator: Iterator[_] = object_listing.getObjectSummaries.iterator()
        while (iterator.hasNext) {
          val summary: S3ObjectSummary = iterator.next().asInstanceOf[S3ObjectSummary]
          client.deleteObject(bucketName, summary.getKey())
        }

        flag=false

      }

    }

  def countNumberOfObjectsInsideBucket(): Unit ={

    var object_listing: ObjectListing = client.listObjects(bucketName)
    var flag: Boolean = true
    var count=0
    while (flag) {
      val iterator: Iterator[_] = object_listing.getObjectSummaries.iterator()
      while (iterator.hasNext) {
        val summary: S3ObjectSummary = iterator.next().asInstanceOf[S3ObjectSummary]
        count+=1
      }

      flag=false
      println("Number of objects are: " + count)

    }
  }
}

答案 1 :(得分:0)

您需要AWSCredentialsProvider

 val provider = new AWSStaticCredentialsProvider(
   new BasicAWSCredentials(AWS_ACCESS_KEY,AWS_SECRET_KEY)
 )

然后用它来创建客户端:

 val client = AmazonS3ClientBuilder
   .standard
   .withCredentials(provider)
   .withRegion("us-west-1") // or whatever  your region is
   .build