-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support non-JSON uploads #28
Comments
Hi Jess, I've just recently used googleAuthR to work with GCS myself as part of the bigQueryR package, so think I can help here. Check out the uploadData.R file from there which has very similar syntax to what I see at the GCS help https://github.com/MarkEdmondson1234/bigQueryR/blob/master/R/uploadData.R which in turn borrows from bigrquery. The pertinent bit is adding the csv file as raw output into the body: ## standard_csv() is a function in same file that writes the text
csv <- standard_csv(upload_data)
boundary <- "--bqr_upload"
line_break <- "\r\n"
mp_body_schema <- paste(boundary,
"Content-Type: application/json; charset=UTF-8",
line_break,
jsonlite::toJSON(config, pretty=TRUE, auto_unbox = TRUE),
line_break,
sep = "\r\n")
## its very fussy about whitespace
## must match exactly https://cloud.google.com/bigquery/loading-data-post-request
mp_body_data <- paste0(boundary,
line_break,
"Content-Type: application/octet-stream",
line_break,
line_break,
csv)
mp_body <- paste(mp_body_schema, mp_body_data, paste0(boundary, "--"), sep = "\r\n")
l <-
googleAuthR::gar_api_generator("https://www.googleapis.com/upload/bigquery/v2",
"POST",
path_args = list(projects = projectId,
jobs = ""),
pars_args = list(uploadType="multipart"),
customConfig = list(
httr::add_headers("Content-Type" = "multipart/related; boundary=bqr_upload"),
httr::add_headers("Content-Length" = nchar(mp_body, type = "bytes"))
)
)
req <- l(path_arguments = list(projects = projectId,
datasets = datasetId,
tableId = tableId),
the_body = mp_body) I'm sure modifying this a bit will give you what you need. I'm also planning to look at a Google Storage API at some point so am interested in your progress! From the same package I've needed an extract data and assign ownership to a Google email |
I'm trying to upload image files, so I encoded the file with the base64enc package and used that as the body of the request. However, now I'm getting an error that I'm having trouble tracing.
Looking in my google storage account, I see the upload is actually there with the correct size, but the image does not show up at all. I have no idea what is happening. I cannot figure out where the Error in $ is coming from, since it looks like this is getting all the way to the retry section of code and testing for a 200 or 201 response and then failing somewhere in curl. Have you ever had a problem like this? Thank you for pointing me to your code for BigQuery, it just ended up not being what I needed since I really only need the base64 encoded data in the body. |
Ahh ok its not a simple data.frame :) I'm afraid I haven't uploaded binary/jpeg files before in R. It looks like the error is related to the HTTP request failing, in Having a look in httr issues, this looks close to what you are asking: r-lib/httr#253 and a solution here: ...if true, then the body for googleAuthR would need to be something like:
Let me know how it goes. |
Yes I did try this! The issue with this solution when it comes to Is it possible to prevent the json parsing? Or is this something I just need to do in regular httr? |
Right, yes it assumes all requests are json. Try using the customConfig parameter, to override the encode parameter, something like:
Trouble is I don't know if that will override the existing json one. If it doesn't work, construct it in httr and show me here and I'll look to add support for it in googleAuthR |
Yeah, that didn't work to override it. I'll work out something in httr and let you know what ends up working. |
Alright, I got it to work in shiny! Here is basically what I did. I think all you need to do for googleAuthR to work with this is allow the user to choose if the body should be parsed into json or not. Doing it automatically means that the user cannot use
As you can see, I'm using a service token rather than user authentication, but I can't imagine it's much different either way. I think my only problem was that since I was trying to upload pictures, the automatic json parsing of the body was not the right answer for me. Thanks for your help! |
Great job - ok try the latest version on Github: it will now allow As you say, the reason you still may want to use googleAuthR even though you have it done in httr is the Shiny multi-user authentication, as the token is taken care of with |
Thank you very much, this works great! I'm going to close the issue, it seems solved to me! |
Hmm actually this fix broke a test for me, so I think I will need to revert - BUT when I tried my own upload of pictures it worked fine with the old version. Here is my code, could you compare to yours?
|
This reverts commit 20a0c69.
This code is what worked with the original version? Hmm, that is very strange, it looks pretty much like what I was trying to do. Did you try using this in shiny or just as is? |
Once it works offline it should work in Shiny, that's a big aim of the library. Here is a working version with Shiny using the
|
I just tried that script with everything installed and got the same error I have been getting with just using I'm also using |
Use the github version of googleAuthR v 0.2.0.9000 I tracked down the error to just some feedback messages that are trying to read JSON when there is not. This line corrects it, by only showing the message if its json encoding:
I'll patch it and try to get on CRAN asap. Thanks for helping find it! |
That worked, thank you! I look forward to using this from CRAN. |
I ran into this problem again myself, so who knows what's happened before, but the "encode" is now passed transparently to httr |
I've been trying to use googleAuthR for a shiny app that uploads user files to a universal Google Storage account. I'm having trouble figuring out how to set up the body of the request so that the actual file data gets sent to the API rather than just the json metadata. All that happens right now is that my file in Google Storage is just a json file with the name and type of the file I want to upload.
I tried using httr's upload_file() to create the upload data and then pass that to the body, but I'm not very versed in httr or curl, so I'm not sure how else to do this. I think my issue is that the body of the request gets parsed into json, when I don't want to parse the actual file data.
I'm just trying to use a simple upload at this point (https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload) and I just can't figure it out!
Is there a specific way to create a file data stream or something for use with googleAuthR? I really appreciate the easy authorization with shiny this package provides and I'd rather not have to deal with straight httr!
Thanks,
Jess
The text was updated successfully, but these errors were encountered: