Created
January 23, 2014 08:38
-
-
Save juliaferraioli/8575063 to your computer and use it in GitHub Desktop.
Import data from Google Cloud Storage, save locally, produce png, and save to Google Cloud Storage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pkg.add("Gadfly") | |
Pkg.add("HTTPClient") | |
Pkg.add("JSON") | |
Pkg.add("RDatasets") | |
using Gadfly | |
using HTTPClient.HTTPC | |
using JSON | |
using RDatasets | |
token_url = "http://metadata/computeMetadata/v1/instance/service-accounts/default/token" | |
request = HTTPC.get(token_url, RequestOptions(headers=[("X-Google-Metadata-Request", "True")])) | |
access_token = JSON.parse(bytestring(request.body))["access_token"] | |
bucket_name = <bucket_name> | |
file_name = <data_file_name> | |
project_number = <project_number> | |
request_url = string("https://www.googleapis.com/storage/v1beta2/b/", bucket_name, "/o/", file_name, "?alt=media") | |
request = HTTPC.get(request_url, RequestOptions(headers=[("Authorization", string("OAuth ", access_token)), | |
("x-goog-api-version", "2"), | |
("x-goog-project-id", project_number)])) | |
fp = open(file_name, "w") | |
write(fp, takebuf_string(request.body)) | |
wine_data = readtable(file_name) | |
alcohol_vs_magnesium = plot(wine_data, x="Alcohol", y="Magnesium", Geom.point) | |
png_name = "alcohol_vs_magnesium.png" | |
draw(PNG(png_name, 6inch, 3inch),alcohol_vs_magnesium) | |
file_len = strip(readall(`stat -c %s $png_name`)) | |
request_url = string("https://www.googleapis.com/upload/storage/v1beta2/b/", bucket_name, "/o?uploadType=media&name=",png_name) | |
options = RequestOptions(headers=[("Authorization", string("OAuth ", access_token)),("Content-Type","image/png"),("Content-Length", string(file_len))]) | |
request = HTTPC.post(request_url, (:file, png_name), options) | |
println(request.http_code) | |
println("Check your bucket!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment