gpt4 book ai didi

ruby-on-rails - 云上传错误

转载 作者:行者123 更新时间:2023-12-02 21:15:50 24 4
gpt4 key购买 nike

我正在尝试从 AWS 切换到 Cloudinary 来管理我网站上的图像。我在 Rails 应用程序上使用 Cloudinary 和 CarrierWave,并按照 Cloudinary 的指南设置图像上传和存储 ( http://cloudinary.com/documentation/rails_carrierwave )。当我尝试上传时出现此错误:

Cloudinary::CarrierWave::UploadError in UsersController#update

Invalid Signature (value removed). String to sign - 'public_id=(value removed)sf&timestamp=1433525724'.

这是我在用户 Controller 中的更新方法:

def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:notice] = "User information updated!"
redirect_to edit_user_registration_path
else
flash[:error] = "Error updating user information!"
redirect_to :back
end
end

def user_params
params.require(:user).permit(:name, :picture, :picture_cache)
end

这是我的上传文件:

class PictureUploader < CarrierWave::Uploader::Base

include Cloudinary::CarrierWave

version :main do
process :resize_to_fill => [300, 350]
end
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
version :icon do
process :resize_to_fit => [18, 21]
end
version :small do
process :resize_to_fit => [54, 63]
end
version :profile do
process :resize_to_fit => [180, 210]
end

def extension_white_list
%w(jpg jpeg gif png)
end
end

任何想法都将不胜感激。谢谢!

最佳答案

我遇到了完全相同的问题。请仔细检查您是否已正确设置配置参数(云名称、API key 、API key )。它们可以在您的 cloudinary 帐户的管理控制台上找到。 (仪表板 > 帐户详细信息)。

根据他们的文档:(http://cloudinary.com/documentation/rails_additional_topics#configuration_options)

Configuration parameters can be globally set using a cloudinary.yml configuration file, located under the config directory of your Rails project. etc...

Here's an example of a cloudinary.yml file:

production:
cloud_name: "sample"
api_key: "874837483274837"
api_secret: "a676b67565c6767a6767d6767f676fe1"

etc...

... Another configuration option is to use a Rails initializer file. You can place a file named cloudinary.rb in the /config/initializers folder of your Rails project. Here's a sample initializer code:

Cloudinary.config do |config|
config.cloud_name = 'sample'
config.api_key = '874837483274837'
config.api_secret = 'a676b67565c6767a6767d6767f676fe1'
config.cdn_subdomain = true
end

One last configuration option allows you to dynamically configure the Cloudinary library by defining the CLOUDINARY_URL environment variable. The configuration URL is available in the Management Console's dashboard of your account. When using Cloudinary through a PaaS add-on (e.g., Heroku), this environment variable is automatically defined in your deployment environment. Here's a sample value:

CLOUDINARY_URL=cloudinary://874837483274837:a676b67565c6767a6767d6767f676fe1@sample

我实际上是如何解决这个问题的

我通过采用(并稍微修改)第一个选项解决了这个问题,即在 config 目录中创建 cloudinary.yml 文件并编写以下代码:

(config/cloudinary.yml)

development:
cloud_name: <%= ENV["CLOUD_NAME"] %>
api_key: <%= ENV["API_KEY"] %>
api_secret: <%= ENV["API_SECRET"] %>

test:
cloud_name: <%= ENV["CLOUD_NAME"] %>
api_key: <%= ENV["API_KEY"] %>
api_secret: <%= ENV["API_SECRET"] %>

请注意,配置参数(云名称、API key 、API key )设置为环境变量(CLOUD_NAMEAPI_KEYAPI_SECRET) 以防止它们在代码公开共享时被暴露。 (您不想对敏感信息进行硬编码)

您可以通过编辑 .bash_profile 文件来设置 bash 中的环境变量,该文件位于(且隐藏)在主目录中:

(.bash_profile)
.....
export CLOUD_NAME="your cloud name"
export API_KEY="your api key"
export API_SECRET="your api secret"
.....

您可以通过在终端中键入 echo $CLOUD_NAME 来检查这些环境变量是否设置正确。(您可能需要退出并重新启动终端)。如果成功,输出将类似于:

echo $CLOUD_NAME 
> your cloud name

最后,如果您计划将应用程序部署到 Heroku,您可能还想添加 cloudinary 作为插件,通过在终端中输入以下命令,该插件对于初学者选项是免费的:

heroku addons:create cloudinary:starter

您还需要使用以下命令在heroku上配置一个名为CLOUDINARY_URL的环境变量(可在cloudinary帐户的管理控制台上找到。(仪表板>帐户详细信息)):

heroku config:set CLOUDINARY_URL="your cloudinary url"

将这些放在一起可能会解决您的问题。

最后但并非最不重要的一点是,我发现以下博客文章非常有用:

http://www.uberobert.com/rails_cloudinary_carrierwave/

它解释了如何使用 cloudinary 和 Carrierwave 在应用程序上上传和操作图像。

希望对你有帮助!

关于ruby-on-rails - 云上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30672858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com