scala-s3

aws s3 client in scala

git clone https://9o.is/git/scala-s3.git

S3Spec.scala

(2676B)


      1 package inc.pyc
      2 package aws
      3 package s3
      4 
      5 import AmazonS3._
      6 import java.util.{TimeZone, Calendar}
      7 import org.specs._
      8 import dispatch._
      9 import java.io.{File,FileWriter}
     10 import com.ning.http.client.Response
     11 
     12 object S3Spec extends Specification {
     13 
     14   val bucketName = getValue("awsBucket")
     15   val access_key = getValue("awsAccessKey")
     16   val secret_key = getValue("awsSecretAccessKey")
     17   
     18   def s3(): Option[S3] = 
     19     for {
     20       bucketName <- bucketName
     21       access_key <- access_key
     22       secret_key <- secret_key
     23     } yield S3(access_key, secret_key, bucketName)
     24 
     25   def shouldWeSkip_? = List(s3) must notContain(None).orSkip
     26 
     27   def newTempFile = {
     28     val testFile = File.createTempFile("s3specs","bin")
     29     val writer = new FileWriter(testFile)
     30     writer.write("testing")
     31     writer.close
     32     testFile
     33   }
     34 
     35   "S3" should {
     36     "be able to create a file" in {
     37       shouldWeSkip_?
     38 
     39       val testFile = newTempFile
     40       s3() map {
     41         s3 =>
     42           val r: Response = s3.createFile(testFile.getName, testFile, "text/plain")()
     43           r.getStatusCode must be_==(200)
     44       }
     45     }
     46 
     47     "be able to delete a file" in {
     48       shouldWeSkip_?
     49       
     50       val testFile = newTempFile
     51       s3() map {
     52         s3 =>
     53           val r: Response = s3.deleteFile(testFile.getName)()
     54           r.getStatusCode must be_==(200)
     55       }
     56     }
     57     
     58     "create the correct canonical string for a request with a date header" in {
     59       val expected = "PUT\nmd5sum\ntext/plain\nMon, 17 Oct 2011 11:43:29 GMT\nx-amz-meta-author:john@doe.com\n/mybucket/newobject123"
     60       val cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"))
     61       cal.set(2011, 9, 17, 11, 43, 29)
     62       val date = cal.getTime()
     63       val amzHeaders = Map("x-amz-meta-author" -> Set("john@doe.com"))
     64       val res = canonicalString("PUT", "/mybucket/newobject123", Left(date), Some("text/plain"), Some("md5sum"), amzHeaders)
     65       res must_== expected
     66     }
     67     
     68     "create the correct canonical string for a request with an Expires value" in {
     69       val expires = System.currentTimeMillis() / 1000 + 30
     70       val expected = "PUT\n\n\n%s\nx-amz-meta-author:john@doe.com\n/mybucket/newobject123".format(expires.toString)
     71       val amzHeaders = Map("x-amz-meta-author" -> Set("john@doe.com"))
     72       val res = canonicalString("PUT", "/mybucket/newobject123", Right(expires), None, None, amzHeaders)
     73       res must_== expected
     74     }
     75 
     76   }
     77 
     78   doAfterSpec {
     79     Http.shutdown()
     80   }
     81 
     82   def getValue(key: String): Option[String] = {
     83     if (System.getenv(key) != null) {
     84       Some(System.getenv(key))
     85     } else if (System.getProperty(key) != null) {
     86       Some(System.getProperty(key))
     87     } else {
     88       None
     89     }
     90   }
     91 }