scala-s3
aws s3 client in scala
git clone https://9o.is/git/scala-s3.git
S3.scala
(1342B)
1 package inc.pyc
2 package aws
3 package s3
4
5 import dispatch._
6 import Defaults._
7 import java.io._
8 import com.ning.http.client.Response
9
10 /**
11 * Amazon S3 API
12 */
13 case class S3(access_key: String, secret_key: String, bucket: String, path: String = "") {
14 import AmazonS3._
15
16 /**
17 * Creates a file given a file name, binary data and file content-type.
18 */
19 def createFile(fn: String, data: Array[Byte], contentType: String): Future[Response] =
20 Http.configure(_ setCompressionEnabled true)(Bucket(bucket).PUT.setBody(data) / (path + fn) <:<
21 Map("content-type" -> contentType) <@(access_key, secret_key))
22
23 /**
24 * Creates a file given a file name, a file and file content-type.
25 */
26 def createFile(fn: String, file: java.io.File, contentType: String): Future[Response] = {
27 val bis = new BufferedInputStream(new FileInputStream(file))
28 val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray
29
30 createFile(fn, bArray, contentType)
31 }
32
33 /**
34 * Deletes a file given a file name.
35 */
36 def deleteFile(fn: String) =
37 Http.configure(_ setCompressionEnabled true)(Bucket(bucket).DELETE /
38 (path + fn) <@(access_key, secret_key))
39
40 /**
41 * Generates file's S3 url given a file name.
42 */
43 def fileUrl(fn: String) = "http://" + AmazonS3.Root + "/" + bucket + path + fn
44 }