Skip to content

Instantly share code, notes, and snippets.

@kseada
Last active June 1, 2022 09:15
Show Gist options
  • Save kseada/6c6d006ab1dc1bc94d7b to your computer and use it in GitHub Desktop.
Save kseada/6c6d006ab1dc1bc94d7b to your computer and use it in GitHub Desktop.
Scala code for accessing Amazon S3 using AWS SDK for Java. Add dependency to sbt libraryDependencies: "com.amazonaws" % "aws-java-sdk" % "1.3.32"
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.AmazonClientException
import com.amazonaws.AmazonServiceException
import java.io.File
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.FileOutputStream
import org.apache.commons.io.IOUtils
val BUCKET_NAME = "###"
val FILE_PATH = "###"
val FILE_NAME = "###"
val AWS_ACCESS_KEY = "******************"
val AWS_SECRET_KEY = "******************"
try {
val awsCredentials = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)
val amazonS3Client = new AmazonS3Client(awsCredentials)
// create a new bucket
amazonS3Client.createBucket(BUCKET_NAME)
// upload file
val file = new File(FILE_PATH)
amazonS3Client.putObject(BUCKET_NAME, FILE_NAME, file)
// download file and read line by line
val obj = amazonS3Client.getObject(BUCKET_NAME, FILE_NAME)
val reader = new BufferedReader(new InputStreamReader(obj.getObjectContent()))
var line = reader.readLine
while (line!=null) {
println(line)
line = reader.readLine
}
// download file and write to local file system
val obj = amazonS3Client.getObject(BUCKET_NAME, FILE_NAME)
val bytes = IOUtils.toByteArray(obj.getObjectContent())
val file = new FileOutputStream(FILE_NAME)
file.write(bytes)
} catch {
case ase: AmazonServiceException => System.err.println("Exception: " + ase.toString)
case ace: AmazonClientException => System.err.println("Exception: " + ace.toString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment