Skip to content

Instantly share code, notes, and snippets.

@Kalyan-D
Forked from kseada/GSAccess.scala
Created May 13, 2021 16:37
Show Gist options
  • Save Kalyan-D/923a80d1dd356343b5a402f9b2c02190 to your computer and use it in GitHub Desktop.
Save Kalyan-D/923a80d1dd356343b5a402f9b2c02190 to your computer and use it in GitHub Desktop.
Scala code for accessing Google Storage using JetS3t. Add repository to sbt resolvers: "jets3t" at "http://www.jets3t.org/maven2", and library to sbt libraryDependencies: , "net.java.dev.jets3t" % "jets3t" % "0.9.0".
import org.jets3t.service.security.GSCredentials
import org.jets3t.service.impl.rest.httpclient.GoogleStorageService
import org.jets3t.service.model.GSObject
import org.jets3t.service.ServiceException
import java.io.File
import java.io.BufferedReader
import java.io.InputStreamReader
val BUCKET_NAME = "###"
val FILE_PATH = "###"
val FILE_NAME = "###"
val GS_ACCESS_KEY = "******************"
val GS_SECRET_KEY = "******************"
try {
val gsCredentials = new GSCredentials(GS_ACCESS_KEY, GS_SECRET_KEY)
val gsService = new GoogleStorageService((gsCredentials)
// create a new bucket
gsService.createBucket(BUCKET_NAME)
// upload file
val file = new File(FILE_PATH)
GSObject fileObject = new GSObject(file);
gsService.putObject(BUCKET_NAME, fileObject)
// download file
val obj = gsService.getObject(BUCKET_NAME, FILE_NAME)
val reader = new BufferedReader(new InputStreamReader(obj.getDataInputStream()))
var line = reader.readLine
while (line!=null) {
println(line)
line = reader.readLine
}
} catch {
case se: ServiceException => System.err.println("Exception: " + se.toString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment