2017-08-08 2 views
0

Dans les rails 5, j'ai besoin de configurer la fonction dynamodb. J'ai référé quelques blogs et essayé de l'implémenter. Tout d'abord en localhost il était en cours d'exécution sans aucun problème, mais quand je passe à un autre nouveau système ou un serveur, il affiche une erreur comme,Comment configurer une dynamodb dans les rails?

/home/NICHEPRO/shruthir/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-2.10.19/lib/aws-sdk-core/plugins/regional_endpoint.rb:34:in `after_initialize': missing region; use :region option or export region name to ENV['AWS_REGION'] (Aws::Errors::MissingRegionError) 
from /home/NICHEPRO/shruthir/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-2.10.19/lib/seahorse/client/base.rb:84:in `block in after_initialize' 

joyau AWS est

aws-sdk (2.10.19) 
aws-sdk-core (2.10.19) 
aws-sdk-resources (2.10.19) 

Référals De:

https://assist-software.net/snippets/how-save-data-amazon-dynamodb-using-ruby-on-rails

J'ai aussi essayé de résoudre ce problème en se référant d'autres blogs, mais je vais faire ci-dessous erreur aussi,

Failed to open TCP connection to localhost:8080 (Connection refused - connect(2) for "localhost" port 8080) 

Comment résoudre ce problème?

Répondre

1

Espérons que vous utilisez Dynamoid gem. Dans app/config/initializer, ajoutez un nouveau fichier de configuration et ajoutez le code ci-dessous.

Dynamoid.configure do |config| 
    config.adapter = 'aws_sdk_v2' # This adapter establishes a connection to the DynamoDB servers using Amazon's own AWS gem. 
    config.access_key = (ENV['AWS_ACCESS_KEY_ID'] || APP_CONFIG[:aws_access_key_id]) 
    config.secret_key = (ENV['AWS_SECRET_ACCESS_KEY'] || APP_CONFIG[:aws_secret_access_key]) 
    config.region = (ENV['AWS_REGION'] || 'us-east-1') 
    config.namespace = nil # To namespace tables created by Dynamoid from other tables you might have. Set to nil to avoid namespacing. 
    config.warn_on_scan = true # Output a warning to the logger when you perform a scan rather than a query on a table. 
    config.read_capacity = 100 # Read capacity for your tables 
    config.write_capacity = 200 # Write capacity for your tables 
    config.endpoint = (ENV['DYNAMO_ENDPOINT'] || APP_CONFIG[:dynamo_endpoint]) # [Optional]. If provided, it communicates with the DB listening at the endpoint. This is useful for testing with [Amazon Local DB] (http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html). 
end 

Assurez-vous de mettre à jour vos variables ENV. Ou si vous vous connectez directement à AWS au lieu de bijou Dynamoid il suffit de suivre ...

def client 
    @client ||= Aws::DynamoDB::Client.new(
    access_key_id: (ENV['AWS_ACCESS_KEY_ID'] || APP_CONFIG[:aws_access_key_id]), 
    secret_access_key: (ENV['AWS_SECRET_ACCESS_KEY'] || APP_CONFIG[:aws_secret_access_key]), 
    region: (ENV['AWS_REGION'] || 'us-east-1'), 
    endpoint: (ENV['DYNAMO_ENDPOINT'] || APP_CONFIG[:dynamo_endpoint]) 
) 
end 

et faire une requête comme celui-ci

client.query(
    table_name: table_name, 
    select: 'COUNT', 
    expression_attribute_values: { 
     ':v1' => index 
    }, 
    key_condition_expression: 'user_id = :v1' 
).count 

Pour plus d'informations http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/DynamoDB.html

+0

Désolé! Je n'utilise pas la gemme 'Dynamoid'. –

+0

Ensuite, vous pouvez utiliser la deuxième option en appelant la méthode 'client' – Selvamani