Chris Snow
Chris Snow

Reputation: 24596

how can I save a binary file from cloud object storage to the notebook filesystem?

Frequently when working with files in IBM Cloud Object Storage from a Watson Studio notebook, I need to save the files to the notebook local file system where I can then access them from R functions.

Project-lib allows me to retrieve the file from cloud object storage as a byte array, how can I save the byte array to a file?

library(projectLib)
project <- projectLib::Project$new(projectId="secret, projectToken="secret")
pc <- project$project_context

my.file <- project$get_file("myfile.csv.gz")

#
# Question: how do I save the file to disk ??
#

df = read.csv2("myfile.csv.gz", sep = "|",
                           colClasses=c("ASSETUNIT_GLOBALID"="character"))

I tried using save() but this was corrupting the data in the file.

Upvotes: 1

Views: 191

Answers (1)

Chris Snow
Chris Snow

Reputation: 24596

The R function writeBin was the solution for me:

library(projectLib)
project <- projectLib::Project$new(projectId="secret, projectToken="secret")
pc <- project$project_context

my.file <- project$get_file("myfile.csv.gz")

#
# writeBin was the solution :
#

writeBin(my.file, 'myfile.csv.gz', size = NA_integer_,
         endian = .Platform$endian, useBytes = TRUE)

df = read.csv2("myfile.csv.gz", sep = "|",
                           colClasses=c("ASSETUNIT_GLOBALID"="character"))

Upvotes: 1

Related Questions