Ruby中针对nil:NilClass的Aws :: S3 :: Presigner未定义方法凭证 [英] Aws::S3::Presigner undefined method credentials for nil:NilClass in Ruby

查看:76
本文介绍了Ruby中针对nil:NilClass的Aws :: S3 :: Presigner未定义方法凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了aws-sdk-core gem,但是在获取url形式的aws时出现错误 以下是我的代码

i have use aws-sdk-core gem i am getting error during getting url form aws 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

我遇到错误

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 known how get presigned url please lets us known

谢谢

推荐答案

无用的错误消息表明您尚未配置AWS凭证.这些是生成签名所必需的.如果您使用客户端发送请求,则会得到一条更有用的错误消息,指出需要凭据.

The unhelpful error message indicates that you have not configured AWS credentials. These are need to generate the signature. If you had used the client to send a request you would have gotten a more helpful error message indicating that credentials were 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

与您的问题无关,但是您可以使用S3的资源接口来稍微清理一下代码.

Not related to your problem, but you can use the resource interface for S3 which cleans up 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

虽然我展示了如何在代码中配置凭据,但我强烈对此提出了建议.您应该在启动应用程序之前将凭据导出到ENV或将其放入共享凭据文件中.

While I showed how to configure credentials in code, I strongly recommend against this. You should export credentials to ENV before launching your applications or put them in the shared credentials file.

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

或将以下内容放入〜/.aws/credentials

Or put the following in ~/.aws/credentials

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

如果使用ENV或共享凭据文件,则不再需要在代码中配置它们.当未将这些位置提供给客户端构造函数时,SDK将尝试获取这些位置.

If you use the ENV or shared credentials file, then you no longer need to configure them in code. The SDK will attempt to source these locations when they are not given to the client constructor.

这篇关于Ruby中针对nil:NilClass的Aws :: S3 :: Presigner未定义方法凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆