【问题标题】:Fog - Get size of object in S3雾 - 在 S3 中获取对象的大小
【发布时间】:2013-04-04 21:42:48
【问题描述】:

如何在不下载的情况下获得雾中 s3 对象的大小?

例如:

connection.get_object(dir.key, latest_backup.key).body.size

需要我先下载对象。

如何在下载前确定大小?

【问题讨论】:

  • 您是否尝试通过metadata 方法访问对象的元数据?
  • 我不知道有一个。我在文档中也看不到它...您能否提供示例或文档链接?干杯。
  • 何对不起,我以为我给了你链接,这里是我找到的那个:docs.aws.amazon.com/AWSRubySDK/latest/frames.html,这个网站很烂,所以我不能给你确切的链接,但是在 s3 部分, 'S3Object' 上有一个方法列表,它说有一个元数据方法
  • @pjam - aws-sdk 不是雾。
  • @pguardiario 确实,我的错,我很快阅读了这个问题并没有看到它不是 aws-sdk ;

标签: ruby-on-rails ruby amazon-s3 fog


【解决方案1】:

要查找文件的大小,请执行以下操作:

connection.head_object(bucket_name, object_name, options = {})

并在返回的对象中寻找这个:

Content-Length

这还将为您提供其他有用的信息,例如 ETag 中的校验和以及其他类似的信息。

参考:http://rubydoc.info/gems/fog/Fog/Storage/AWS/Real#head_object-instance_method

【讨论】:

    【解决方案2】:

    remote_file.content_length 对我有用(雾 1.21.0)

    remote_file 是从远程目录获取的,如下所示:

    def connection
      Fog::Storage.new(fog_config)
    end
    
    def directory
      connection.directories.get(bucket)
    end
    
    def remote_file
      remote_files = directory.files.last
    end
    

    【讨论】:

      【解决方案3】:
      module S3
        def self.directory
          connection.directories.new(key: ENV['AWS_BUCKET'])
        end
      
        def self.connection
          Fog::Storage.new(
            provider: 'AWS',
            aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
            region: ENV['AWS_REGION']
          )
        end
      
        def self.file(key)
          directory.files.head(key)
        end
      end
      
      
      file = S3.file(key)
      file.content_length
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-27
        • 2011-10-09
        • 1970-01-01
        • 1970-01-01
        • 2019-10-14
        • 2010-11-24
        • 2021-07-31
        • 1970-01-01
        相关资源
        最近更新 更多