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 , .