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

I have aws-sdk-core gem. I get an error while receiving the aws url form the following is my 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 get an error

NoMethodError - undefined method `credentials' for 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 anyone knows how to get the assigned URL, please let us know

thanks

+4
source share
1 answer

A direct error message indicates that you did not configure AWS credentials. This is necessary to create a signature. If you used the client to send the request, you would receive a more useful error message indicating that credentials are 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 to your problem, but you can use the resource interface for S3, which will clear the code a 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

, , strong. ENV .

$ export AWS_ACCESS_KEY_ID=...
$ export AWS_SECRET_ACCESS_KEY=...

~/.aws/credentials

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

ENV shared credentials, . SDK , .

+6

All Articles