You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ruby Shrine上传至Cloudflare R2时遇NoSuchKey错误求助

使用Shrine上传至Cloudflare R2时触发Aws::S3::Errors::NoSuchKey错误

之前在AWS S3上使用Shrine gem处理文件上传完全正常,切换到Cloudflare R2对象存储后,文件能成功上传到缓存存储,但从缓存迁移至正式存储的过程失败,抛出Aws::S3::Errors::NoSuchKey(指定的键不存在)错误。已确认缓存文件确实存在(附截图证明)。

环境信息

  • Ruby on Rails 7.1
  • Shrine ~> 3.6.0
  • aws-sdk-s3 ~> 1.159.0

复现代码

require "bundler/setup"
require "active_record"
require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/s3"
require "down"

s3_options = {
  bucket: "bucket_name",
  region: "auto",
  access_key_id: "access_key_id",
  secret_access_key: "secret_access_key",
  endpoint: "https://r2.endpoint.domain",
  force_path_style: true,
  public: false
}

Shrine.storages = {
  cache: Shrine::Storage::S3.new(prefix: "cache", upload_options: {}, **s3_options),
  store: Shrine::Storage::S3.new(copy_options: {}, **s3_options)
}

Shrine.plugin :activerecord
Shrine.plugin :pretty_location

class ImageUploader < Shrine
  def generate_location(io, record: nil, **context)
    tenant_id = record[:tenant_id] || "default"
    "tenants/#{tenant_id}/#{super}"
  end
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.connection.create_table(:posts) { |t| t.text :image_data }

class Post < ActiveRecord::Base
  include ImageUploader::Attachment(:image)
end

post = Post.create(image: Down.download("https://images.pexels.com/photos/15286/pexels-photo.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"))

puts "Image uploaded successfully with data: #{post.image_data}"

Gemfile

source "https://rubygems.org"

gem "bundler", "~> 2.0"
gem "activerecord", "~> 7.1.3.4"
gem "shrine", "~> 3.6.0"
gem 'aws-sdk-s3', '~> 1.159.0'
gem "down"
gem 'image_processing', '~> 1.13'
gem 'mini_exiftool', '~> 2.11.0'
gem "sqlite3", "~> 1.4"
gem 'nokogiri'

恳请各位提供技术解决方案。

内容的提问来源于stack exchange,提问作者Kubatron P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 21:04:57