public
Last active

Example of how to control AWS from R

  • Download Gist
awsExampleRjava.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
library(rJava)
.jinit()
 
awsAccessKeyText <- "yourKeyHere"
awsSecretKeyText <- "yourSecretKeyHere"
pathToSdk <- "/home/jal/aws-java-sdk-1.1.0/"
testFile <- "/home/jal/aws-java-sdk-1.1.0/rJavaExamples/testFile.tst"
 
.jaddClassPath(paste(pathToSdk, "lib/aws-java-sdk-1.1.0.jar", sep=""))
.jaddClassPath(paste(pathToSdk, "third-party/commons-logging-1.1.1/commons-logging-1.1.1.jar", sep=""))
.jaddClassPath(paste(pathToSdk, "third-party/commons-httpclient-3.0.1/commons-httpclient-3.0.1.jar", sep=""))
.jaddClassPath(paste(pathToSdk, "third-party/commons-codec-1.3/commons-codec-1.3.jar", sep=""))
 
attach( javaImport( "java.lang" ) )
attach( javaImport( "java.io" ) )
 
awsCreds <- new(com.amazonaws.auth.BasicAWSCredentials, awsAccessKeyText, awsSecretKeyText)
tx <- new(com.amazonaws.services.s3.transfer.TransferManager, awsCreds)
 
bucketName <- paste("s3-upload-sdk-sample-", tolower(awsAccessKeyText), sep="")
s3 <- tx$getAmazonS3Client()
 
#test if the bucket exists; if not, make bucket
if (s3$doesBucketExist(bucketName) == FALSE) {
s3$createBucket(bucketName)
}
 
fileToUpload <- new(File, new(String, testFile))
 
request <- new(com.amazonaws.services.s3.model.PutObjectRequest, bucketName, fileToUpload$getName(), fileToUpload)
 
s3$putObject(request)
 
 

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.