yassine

yassine

Upload file to google cloud storage

I want to upload a file to google cloud storage with my phoenix application, i found some github project but i didn’t understand them very well, can any one help me to how doing that please !

Most Liked

jhefreyzz

jhefreyzz

I once attempted to use GCS for file uploading using elixir google api but I change a cloud provider since I needed a ecto integration and waffle and s3 integrates well.

As for your question please kindly read the README of elixir google api.

  1. Add google_api_storage and goth to your dependencies
  2. Get your google client id and secret
  3. export the client id and secret as shown below:
export GOOGLE_CLIENT_ID=[YOUR-OAUTH-CLIENT-ID]
export GOOGLE_CLIENT_SECRET=[YOUR-OAUTH-CLIENT-SECRET]
  1. Perform requests
# Obtain an access token using goth
{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform")
conn = GoogleApi.Storage.V1.Connection.new(token.token)

# Call the Storage V1 API (for example) to list buckets
{:ok, response} = GoogleApi.Storage.V1.Api.Buckets.storage_buckets_list(conn, project_id)

# Print the response
Enum.each(response.items, &IO.puts(&1.id))

You can also checkout the example repo: https://github.com/GoogleCloudPlatform/elixir-samples/tree/master/storage

mruoss

mruoss

Is the file generated by the server or by the user? If the file is uploaded by the user, I would not bother uploading the file to your server and then forwarding it to the GCS bucket. Instead, I would generate a signed URL on the server, send that to the browser and let the browser upload the file directly to the bucket.

I describe two variants for creating such a signed URL on gcs_signed_url. The two variants differ in whether your application is running on the Google cloud itself or not.

jhefreyzz

jhefreyzz

Please kindly search on your own how to generate google client id and secrets, for starters

  • you head over to API and services
  • click on Credentials
  • click on Create credentials and select OAuth client ID

As for these two

export GOOGLE_CLIENT_ID=[YOUR-OAUTH-CLIENT-ID]
export GOOGLE_CLIENT_SECRET=[YOUR-OAUTH-CLIENT-SECRET]

after you secure you client id and secret, open a new terminal on the current project directory and type those above.

Last Post!

yassine

yassine

ok i will search how to do that, thanks a lot i appreciate that

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement