@xtccc
2016-11-15T15:07:49.000000Z
字数 2908
阅读 2397
AWS
参考链接:
目录
需要用到的依赖是:com.amazonaws:aws-java-sdk:1.9.6
Scala代码:
package cn.gridx.aws.s3
import java.io._
import java.util
import com.amazonaws.regions.{Regions, Region}
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model._
/**
* Created by tao on 1/26/16.
*/
object Fundamentals extends App {
val s3: AmazonS3Client = new AmazonS3Client()
val region_us_west_1: Region = Region.getRegion(Regions.US_WEST_1)
s3.setRegion(region_us_west_1)
/**
* 关于 access key / private key
*
* 由于本机存在文件 ~/.aws/credentials,里面的内容为
* [default]
* aws_access_key_id = YOUR_ACCESS_KEY_ID
* aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
* 因此,本代码运行时可以直接访问S3
*
* 如果没有这个文件,则需要在代码中指定
* 参考[http://docs.ceph.com/docs/master/radosgw/s3/java/]
* */
/**
* 列出全部的buckets
* */
println("\n\n列出所有buckets的名字 ...")
val buckets: util.List[Bucket] = s3.listBuckets
for (i <- 0 until buckets.size) {
val bucket = buckets.get(i)
println(s"Bucket Name: ${bucket.getName}\t, Creation Time: ${bucket.getCreationDate}")
}
/**
* 向名为`gridx-tmp`的bucket中放入1个object
* */
println("\n\n向名为`gridx-tmp`的bucket中写入1个object ...")
def BucketName = "gridx-tmp"
def MyObjKey = "my_obj_key"
def MyFileName = "my_file_name"
s3.putObject(new PutObjectRequest(BucketName, MyObjKey, createTmpFile(MyFileName)))
/**
* 从名为`gridx-tmp`的bucket中下载刚才放入的object
* */
println("从名为`gridx-tmp`的bucket中下载刚才放入的object ...")
val obj: S3Object = s3.getObject(new GetObjectRequest(BucketName, MyObjKey))
val meta: ObjectMetadata = obj.getObjectMetadata
println(s"Content-Type : ${meta.getContentType}, " +
s" Content-Length : ${meta.getContentLength}, " +
s"Content-Encoding : ${meta.getContentEncoding}" )
println("内容为:")
val in = obj.getObjectContent
displayTextStream(in)
in.close
/**
* 如果某个bucket中有很多的objects,那么listObjects返回的可能只是部分结果,
* 这需要用`ObjectListing.isTruncated`来判断
* 如果确实是只返回了部分结果,那么需要用`listNextBatchOfObjects`来继续查询剩下的数据
*
* `listObjects`并不是只返回最上层的目录,而是返回所有的objects(如果返回结果没有被截断)
* */
println("\n\n列出名为`gridx-jars`的bucket中的全部objects")
// 名为`gridx-jars`的bucket所在的Region是 US Standard
s3.setRegion(Region.getRegion(Regions.US_EAST_1))
var objList: ObjectListing = s3.listObjects("gridx-jars")
var loop = false
do {
val summeryList: util.List[S3ObjectSummary] = objList.getObjectSummaries
for (i <- 0 until summeryList.size) {
val summery = summeryList.get(i)
println(s" Key : ${summery.getKey}, Size : ${summery.getSize}")
}
if (objList.isTruncated) {
objList = s3.listNextBatchOfObjects(objList)
loop = true
}
} while (loop)
/**
* 删除指定bucket中的某个object
* */
println("\n\n删除指定bucket中的某个object ...")
s3.deleteObject("gridx-tmp", MyObjKey)
// 创建一个临时文件
def createTmpFile(path: String): File = {
val file = File.createTempFile(path, "txt")
file.deleteOnExit
val writer = new OutputStreamWriter(new FileOutputStream(file))
writer.write("美国国务卿克里今访华 制裁朝鲜或为头号议题\n")
writer.write("副军长曝南沙守礁生活:狗上礁三个月就疯了\n")
writer.write("2016日本小姐大赛决赛在东京举行,庆应大学文学系的二年级学生松野未佳夺得冠军")
writer.close
file
}
// 展示输入流中的文本内容
def displayTextStream(in: InputStream): Unit = {
val reader = new BufferedReader(new InputStreamReader(in))
var line = reader.readLine
while (null != line) {
println(line)
line = reader.readLine
}
}
}