Search code examples
google-cloud-platformcarrierwavegoogle-cloud-storagefog-google

Settting up environment variable GOOGLE_APPLICATION_CREDENTIALS


I am trying to setup the service-account and storing the credential file inside linux server and storing the path in GOOGLE_APPLICATION_CREDENTIALS provided by Google.

It is said that Google Cloud Client Libraries use a library called Application Default Credentials (ADC) to automatically find your service account credentials.

https://cloud.google.com/docs/authentication/production

I am using CarrierWave inside this fog:google library to upload and download the files. Could you please assist or share your thoughts how can we use in this libraries.

My CarrierWave uploader implementation is below:


class TestUploader < CarrierWave::Uploader::Base

  storage :fog

  def fog_credentials
    {
      :provider => 'google',
      :google_project =>'my project',
      :google_json_key_location => GOOGLE_APPLICATION_CREDENTIALS
    }
  end

  def fog_provider
    'fog/google'
  end

  def fog_directory
    '{#bucket-name}'
  end

  def store_dir

    when :File
      "#{file.getpath}/file"
    when :audio
      "#{file.getpath}/audio"
    else
      p " Invalid file "
    end
  end
end

I tried it but it did not work. Kindly suggest here.


Solution

  • There is an Github issue stated as:

    Closing issue. 1.0.0 is out, and we have no more mocks for json backed objects.
    

    You can also check the Documentation for google cloud storagee implementation where mentioned as :

    Use the official google-cloud gem by Google for Google Cloud Storage, instead of Fog. No need to activate Interoperable Access on your project. Rely on Google's preferred authentication mechanism. ie: Service Accounts.

    As mentioned in the Stackoverflow Answer:

    You can see this code:

    Google Cloud Storage using Fog gem: ```ruby

    require "fog/google"
    
    # Uncomment the following line if you want to use Mock
    #Fog.mock!
    
    # Bucket name
    bucket = "an-existing-bucket"
    
    # Timestamp used as sample string
    test = Time.now.utc.strftime("%Y%m%d%H%M%S")
    
    connection = Fog::Storage.new({
      :provider => "Google",
      :google_project => "your-project",
      :google_json_key_location => "path-to-key.json",
    })
    
    # Lists objects in a bucket
    puts connection.list_objects(bucket)
    
    #Creates new object
    connection.put_object(bucket, test, test)
    puts "Object #{test} was created."
    ``
    

    `