[关闭]
@xtccc 2016-11-15T15:07:49.000000Z 字数 2908 阅读 2420

S3

给我写信
GitHub

此处输入图片的描述


AWS


参考链接:



目录


1. 用Java API 访问S3


需要用到的依赖是:com.amazonaws:aws-java-sdk:1.9.6
Scala代码:

  1. package cn.gridx.aws.s3
  2. import java.io._
  3. import java.util
  4. import com.amazonaws.regions.{Regions, Region}
  5. import com.amazonaws.services.s3.AmazonS3Client
  6. import com.amazonaws.services.s3.model._
  7. /**
  8. * Created by tao on 1/26/16.
  9. */
  10. object Fundamentals extends App {
  11. val s3: AmazonS3Client = new AmazonS3Client()
  12. val region_us_west_1: Region = Region.getRegion(Regions.US_WEST_1)
  13. s3.setRegion(region_us_west_1)
  14. /**
  15. * 关于 access key / private key
  16. *
  17. * 由于本机存在文件 ~/.aws/credentials,里面的内容为
  18. * [default]
  19. * aws_access_key_id = YOUR_ACCESS_KEY_ID
  20. * aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
  21. * 因此,本代码运行时可以直接访问S3
  22. *
  23. * 如果没有这个文件,则需要在代码中指定
  24. * 参考[http://docs.ceph.com/docs/master/radosgw/s3/java/]
  25. * */
  26. /**
  27. * 列出全部的buckets
  28. * */
  29. println("\n\n列出所有buckets的名字 ...")
  30. val buckets: util.List[Bucket] = s3.listBuckets
  31. for (i <- 0 until buckets.size) {
  32. val bucket = buckets.get(i)
  33. println(s"Bucket Name: ${bucket.getName}\t, Creation Time: ${bucket.getCreationDate}")
  34. }
  35. /**
  36. * 向名为`gridx-tmp`的bucket中放入1个object
  37. * */
  38. println("\n\n向名为`gridx-tmp`的bucket中写入1个object ...")
  39. def BucketName = "gridx-tmp"
  40. def MyObjKey = "my_obj_key"
  41. def MyFileName = "my_file_name"
  42. s3.putObject(new PutObjectRequest(BucketName, MyObjKey, createTmpFile(MyFileName)))
  43. /**
  44. * 从名为`gridx-tmp`的bucket中下载刚才放入的object
  45. * */
  46. println("从名为`gridx-tmp`的bucket中下载刚才放入的object ...")
  47. val obj: S3Object = s3.getObject(new GetObjectRequest(BucketName, MyObjKey))
  48. val meta: ObjectMetadata = obj.getObjectMetadata
  49. println(s"Content-Type : ${meta.getContentType}, " +
  50. s" Content-Length : ${meta.getContentLength}, " +
  51. s"Content-Encoding : ${meta.getContentEncoding}" )
  52. println("内容为:")
  53. val in = obj.getObjectContent
  54. displayTextStream(in)
  55. in.close
  56. /**
  57. * 如果某个bucket中有很多的objects,那么listObjects返回的可能只是部分结果,
  58. * 这需要用`ObjectListing.isTruncated`来判断
  59. * 如果确实是只返回了部分结果,那么需要用`listNextBatchOfObjects`来继续查询剩下的数据
  60. *
  61. * `listObjects`并不是只返回最上层的目录,而是返回所有的objects(如果返回结果没有被截断)
  62. * */
  63. println("\n\n列出名为`gridx-jars`的bucket中的全部objects")
  64. // 名为`gridx-jars`的bucket所在的Region是 US Standard
  65. s3.setRegion(Region.getRegion(Regions.US_EAST_1))
  66. var objList: ObjectListing = s3.listObjects("gridx-jars")
  67. var loop = false
  68. do {
  69. val summeryList: util.List[S3ObjectSummary] = objList.getObjectSummaries
  70. for (i <- 0 until summeryList.size) {
  71. val summery = summeryList.get(i)
  72. println(s" Key : ${summery.getKey}, Size : ${summery.getSize}")
  73. }
  74. if (objList.isTruncated) {
  75. objList = s3.listNextBatchOfObjects(objList)
  76. loop = true
  77. }
  78. } while (loop)
  79. /**
  80. * 删除指定bucket中的某个object
  81. * */
  82. println("\n\n删除指定bucket中的某个object ...")
  83. s3.deleteObject("gridx-tmp", MyObjKey)
  84. // 创建一个临时文件
  85. def createTmpFile(path: String): File = {
  86. val file = File.createTempFile(path, "txt")
  87. file.deleteOnExit
  88. val writer = new OutputStreamWriter(new FileOutputStream(file))
  89. writer.write("美国国务卿克里今访华 制裁朝鲜或为头号议题\n")
  90. writer.write("副军长曝南沙守礁生活:狗上礁三个月就疯了\n")
  91. writer.write("2016日本小姐大赛决赛在东京举行,庆应大学文学系的二年级学生松野未佳夺得冠军")
  92. writer.close
  93. file
  94. }
  95. // 展示输入流中的文本内容
  96. def displayTextStream(in: InputStream): Unit = {
  97. val reader = new BufferedReader(new InputStreamReader(in))
  98. var line = reader.readLine
  99. while (null != line) {
  100. println(line)
  101. line = reader.readLine
  102. }
  103. }
  104. }




添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注