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.

jhefreyzz

jhefreyzz

To be honest, I didn’t use OAuth2 for authentication instead I use Google service account. Do you really need to use OAuth2?

jhefreyzz

jhefreyzz

Sure, do you have the service account json file already, if not please secure it first.

Then in your terminal just type:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json

Then try make a request again and see if it works.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement