-3

I ran the following code, and it prints out successfully the "URL" from the s3Service.createUnsignedObjectUrl method. My question is, how does the variable even get returned and stored into the "linkForText" variable? I read that scala functions usually need something like a ": Int" and a "return" within the "def"ined function. But I see none of that here. How is the store function able to do this?

package com.justthor

import org.jets3t.service.impl.rest.httpclient.RestS3Service
import org.jets3t.service.security.AWSCredentials
import org.jets3t.service.model.S3Object
import org.jets3t.service.acl.{ AccessControlList, GroupGrantee, Permission }
import java.io.InputStream

object Main extends App{
  val classPath = "/"

  // upload a simple text file
  val textFilename = "test.txt"
  val linkForText = store(textFilename, getClass.getResourceAsStream(s"$classPath$textFilename"))

  // upload a cat image, taken from http://imgur.com/gallery/bTiwg
  // set the content type to "image/jpg"
  val imageFilename = "cat.jpg"
  val linkForImage = store(imageFilename, getClass.getResourceAsStream(s"$classPath$imageFilename"), "image/jpg")

  println(s"Url for the text file is $linkForText")
  println(s"Url for the cat image is $linkForImage")


  def store(key: String, inputStream: InputStream, contentType: String = "text/plain") = {
    val awsAccessKey = "YOUR_ACCESS_KEY"
    val awsSecretKey = "YOUR_SECRET_KEY"
    val awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey)
    val s3Service = new RestS3Service(awsCredentials)
    val bucketName = "test-scala-upload"
    val bucket = s3Service.getOrCreateBucket(bucketName)
    val fileObject = s3Service.putObject(bucket, {
      // public access is disabled by default, so we have to manually set the permission to allow read access to the uploaded file
      val acl = s3Service.getBucketAcl(bucket)
      acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ)

      val tempObj = new S3Object(key)
      tempObj.setDataInputStream(inputStream)
      tempObj.setAcl(acl)
      tempObj.setContentType(contentType)
      tempObj
    })

    s3Service.createUnsignedObjectUrl(bucketName,
      fileObject.getKey,
      false, false, false)
  }
}
Rolando
  • 58,640
  • 98
  • 266
  • 407
  • 1
    Scala functions does not "NEED" a return in functions definations. You can provide but it is neither required nor preferred. – curious Apr 10 '15 at 04:55
  • possible duplicate of [Return in Scala](http://stackoverflow.com/questions/12560463/return-in-scala) – cchantep Apr 10 '15 at 04:59
  • 1
    " I read that scala functions usually need something like a ": Int" and a "return" within the "def"ined function" Find something different to read, because that one is just wrong. – The Archetypal Paul Apr 10 '15 at 06:39

1 Answers1

1

Inference and scala-isms.

The return type is inferred from the return value, which is the result of the final statement in a method/function.

So, in your case whatever is returned by the last line: s3Service.createUnsignedObjectUrl(...) is the value returned from store. And, as there is no branching, then the return type will be inferred from this value. And, if there is branching, then inferrence will take the least common denominator of the possible return types.

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180