Aws::S3::Presigner undefined method credentials for nil:NilClass in Ruby -


i have use aws-sdk-core gem getting error during getting url form aws following code

  def initialize(bucket:, region:)     @bucket = bucket     client = aws::s3::client.new(region: region)     @signer = aws::s3::presigner.new(client: client)   end    def sign(key, expires_in: 3600)     @signer.presigned_url(:get_object, bucket: @bucket, key: key,    expires_in: expires_in)   end 

i getting error

nomethoderror - undefined method `credentials' nil:nilclass:   aws-sdk-core (2.1.15) lib/aws-sdk-core/signers/v4.rb:24:in `initialize'   aws-sdk-core (2.1.15) lib/aws-sdk-core/s3/presigner.rb:88:in `block in sign_but_dont_send' 

if known how presigned url please lets known

thanks

the unhelpful error message indicates have not configured aws credentials. these need generate signature. if had used client send request have gotten more helpful error message indicating credentials required.

def initialize(bucket:, region:)   @bucket = bucket   creds = aws::credentials.new('access_key', 'secret_access_key')   client = aws::s3::client.new(region: region, credentials, creds)   @signer = aws::s3::presigner.new(client: client) end  def sign(key, expires_in: 3600)   @signer.presigned_url(:get_object, bucket: @bucket, key: key, expires_in: expires_in) end 

not related problem, can use resource interface s3 cleans code bit.

def initialize(bucket:, region:)   @bucket = aws::s3::bucket.new(bucket, {     region: region,     credentials: aws::credentials.new('access_key', 'secret_access_key'),   }) end  def sign(key, expires_in: 3600)   @bucket.object(key).presigned_url(:get, expires_in: expires_in) end 

while showed how configure credentials in code, strongly recommend against this. should export credentials env before launching applications or put them in shared credentials file.

$ export aws_access_key_id=... $ export aws_secret_access_key=... 

or put following in ~/.aws/credentials

[default] aws_access_key_id=... aws_secret_access_key=... 

if use env or shared credentials file, no longer need configure them in code. sdk attempt source these locations when not given client constructor.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -