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

Ruby脚本上传文件到AWS S3 Bucket触发extract_name错误如何解决

报错根因及修复方案

报错ArgumentError: missing required option :name是AWS S3 SDK触发的:调用s3_resource.bucket()方法时传入了空值nil,SDK提取bucket名称参数失败。你测试调用工具类时直接传入bucket: nil是触发报错的直接原因。

  • 第一步:新增参数非空校验,提前拦截非法空值,避免到SDK层才报错,修改initialize方法:
def initialize(bucket:, object_key:, input_file_path:)
  raise ArgumentError, "bucket 不能为空" if bucket.nil? || bucket.strip.empty?
  raise ArgumentError, "object_key 不能为空" if object_key.nil? || object_key.strip.empty?
  raise ArgumentError, "input_file_path 不能为空" if input_file_path.nil? || input_file_path.strip.empty?
  @bucket = bucket
  @object_key = object_key
  @input_file_path = input_file_path
end
  • 第二步:确认调用工具类时传入的bucket值是正确读取的local.yml配置,不要传空值。注意AWS SDK要求传入的是纯bucket名称,不需要带S3域名、前缀路径,如果你配置里存的是bucket的完整访问地址,需要提取出纯名称部分再传入。
    调用示例(替换为你实际的配置读取逻辑):
# 假设local.yml配置结构为 aws > s3 > excel_bucket,取值后传入
bucket = YAML.load_file('config/local.yml').dig('aws', 's3', 'excel_bucket')
object_key = 's3端存储的文件路径/目标文件.xlsx'
input_file_path = '本地待上传文件的绝对路径.xlsx'

Accounting::Datev::Utils::ExcelUtil.new(
  bucket: bucket,
  object_key: object_key,
  input_file_path: input_file_path
).call
  • 第三步:可选优化,修正call方法里的拼写错误、新增调试日志:
def call
  # 调试用:打印参数确认取值正常,排查配置读取问题
  puts "上传参数:bucket=#{@bucket}, object_key=#{@object_key}, 本地文件=#{@input_file_path}"
  # 原注释的后缀判断拼写错误,xslx改为xlsx
  return unless @input_file_path.end_with?('.xlsx')
  object = s3_resource.bucket(@bucket).object(@object_key)
  object.upload_file(@input_file_path)
end

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 01:27:03