Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write R data as csv directly to s3

I would like to be able to write data directly to a bucket in AWS s3 from a data.frame\ data.table object as a csv file without writing it to disk first using the AWS CLI.

obj.to.write.s3 <- data.frame(cbind(x1=rnorm(1e6),x2=rnorm(1e6,5,10),x3=rnorm(1e6,20,1)))

at the moment I write to csv first then upload to an existing bucket then remove the file using:

fn <- 'new-file-name.csv'
write.csv(obj.to.write.s3,file=fn)
system(paste0('aws s3 ',fn,' s3://my-bucket-name/',fn))
system(paste0('rm ',fn))

I would like a function that writes directly to s3? is that possible?

like image 464
h.l.m Avatar asked May 06 '15 18:05

h.l.m


People also ask

How do I write data into an S3 bucket?

To upload folders and files to an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.

How do I access Amazon S3 from R?

Accessing S3 data from R Create an IAM user (select 'Programmatic access' for access type) Give it the predefined 'AmazonS3FullAccess' permission (do this by clicking on 'Attach existing policies directly' and searching for 'AmazonS3FullAccess'). No need for tags or anything else. Click through and create the user.


1 Answers

In aws.s3 0.2.2 the s3write_using() (and s3read_using()) functions were added.

They make things much simpler:

s3write_using(iris, FUN = write.csv,
                    bucket = "bucketname",
                    object = "objectname")
like image 146
leerssej Avatar answered Oct 07 '22 14:10

leerssej