Skip to content

Instantly share code, notes, and snippets.

@CerebralMastication
Created November 30, 2010 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CerebralMastication/722230 to your computer and use it in GitHub Desktop.
Save CerebralMastication/722230 to your computer and use it in GitHub Desktop.
Example of how to control AWS from R
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment