Ruby on rails 使用Rails gem为Froala图像编辑器创建Amazon Signaure V4

Ruby on rails 使用Rails gem为Froala图像编辑器创建Amazon Signaure V4,ruby-on-rails,ruby,character-encoding,froala,Ruby On Rails,Ruby,Character Encoding,Froala,我按照这本指南创建亚马逊签名 def getSignatureKey kDate = OpenSSL::HMAC.digest('sha256', 'AWS4' + Figaro.env.aws_secret_access_key, Time.zone.now.utc.strftime('%Y%m%d')) kRegion = OpenSSL::HMAC.digest('sha256', kDate, 'us-west-2') kService = OpenS

我按照这本指南创建亚马逊签名

  def getSignatureKey
    kDate    = OpenSSL::HMAC.digest('sha256', 'AWS4' + Figaro.env.aws_secret_access_key, Time.zone.now.utc.strftime('%Y%m%d'))
    kRegion  = OpenSSL::HMAC.digest('sha256', kDate, 'us-west-2')
    kService = OpenSSL::HMAC.digest('sha256', kRegion, 's3')
    kSigning = OpenSSL::HMAC.digest('sha256', kService, 'aws4_request')
    kSigning
  end
我使用froala gem for rails并使用
imageUploadToS3
选项。但是,我在使用新的Amazon签名版本时遇到了这个错误

Encoding::UndefinedConversionError at /admin/campaigns/1/edit_content
"\xAC" from ASCII-8BIT to UTF-8
我尝试将其更改为
getSignatureKey.force_编码(“ISO-8859-1”).encode(“UTF-8”)
。之后,服务器运行正常,当我上传一张图片时,我从Amazon上获得了
signaturedesnotmatch


任何帮助都将不胜感激。

只是一个猜测,但是这个怎么样

kDate    = OpenSSL::HMAC.digest('sha256', 'AWS4' + Figaro.env.aws_secret_access_key, Time.zone.now.utc.strftime('%Y%m%d')).encode("iso-8859-1").force_encoding("utf-8")
或者只是

kDate    = OpenSSL::HMAC.digest('sha256', 'AWS4' + Figaro.env.aws_secret_access_key, Time.zone.now.utc.strftime('%Y%m%d')).encode("UTF-8")

Ivangrx问我最终做了什么来解决这个问题。我最终没有采用这种方式将图像直接上传到S3。相反,我在froala初始化中添加了一个imageUploadURL。这是一个职位的要求,我关心做我所有的工作。这就是我关心的事情。我正在使用aws sdk的更新版本

module Uploadable
  extend ActiveSupport::Concern
  # NOTE: This method must be set inside all controllers that are including this module
  # This is set this way so that it will throw an exception if you forget to create the method because that would
  # silently cause the image uploading to not work correctly.
  included do
    before_action :set_image_upload_path
  end

  def upload_image
    # Setup AWS credentials
    Aws.config.update(access_key_id: Figaro.env.aws_access_key_id,
                      secret_access_key: Figaro.env.aws_secret_access_key)

    s3 = Aws::S3::Resource.new(region: 'us-west-2')

    # Prepare the necessary parameters
    bucket = if Rails.env.production?
               'app-production'
             else
               'app-dev'
             end

    # If no user, then user_id_prefix will be nil which means the user id will just not be included in the filename
    name = build_unique_filename
    file = params[:file].tempfile

    # Create the image to upload
    image = s3.bucket(bucket).object(name)

    # Upload it and respond back to Froala but respond back to Airbrake with an error message
    if image.upload_file(file, acl: 'public-read')
      render json: { link: image.public_url }
    else
      Airbrake.notify_or_ignore(error_message: 'There was a problem uploading an image to the S3 account.')
    end
  end

  def build_unique_filename
    user_id_prefix = "#{(current_user || current_admin).id}-" if current_user || current_admin

    "#{controller_name}/#{user_id_prefix}#{Time.now.to_i}-" + params[:file].original_filename
  end
end

这非常有效,因为我能够为我的每个资源定义不同的上传路径,并在S3存储桶中的特定目录中上传图像。如果您有任何问题,请留下评论。

您找到解决方案了吗?我也有同样的错误。你能试试我贴的答案吗?