aws-sdk for Ruby access folder within bucket

后端 未结 5 995
[愿得一人]
[愿得一人] 2020-12-25 08:09

I have a bucket on the Amazon S3 with the folder in it. I\'m trying to access it the following way via aws-sdk gem:

s3 = AWS::S3.new(
    :         


        
相关标签:
5条回答
  • 2020-12-25 08:46

    probably the S3 bucket are trying to use is located outside the US-EAST (default location), so this should help you:

    s3 = AWS::S3.new(
        :access_key_id => "KEY",
        :secret_access_key => "SECRET"
        :s3_endpoint => 's3-eu-west-1.amazonaws.com'
    )
    

    Choose your S3 enpdpoint from the following list:

    US Standard *                           s3.amazonaws.com(default)
    US West (Oregon) Region                 s3-us-west-2.amazonaws.com
    US West (Northern California) Region    s3-us-west-1.amazonaws.com
    EU (Ireland) Region                     s3-eu-west-1.amazonaws.com
    Asia Pacific (Singapore) Region         s3-ap-southeast-1.amazonaws.com
    Asia Pacific (Tokyo) Region             s3-ap-northeast-1.amazonaws.com
    South America (Sao Paulo) Region        s3-sa-east-1.amazonaws.com
    

    In terms of object access, the bucket name is my_bucket, but my_folder should be a part of object.

    0 讨论(0)
  • 2020-12-25 08:57

    The answer by Godsaur is essentially correct. However, it appears to be outdated, perhaps for SDK version 1?

    This worked for me for version 2:

    s3 = Aws::S3::Client.new(endpoint:'https://s3-ap-southeast-1.amazonaws.com')
    

    See docs.

    0 讨论(0)
  • 2020-12-25 08:59
    has_attached_file :photo,
      storage: :s3,
      styles: { medium: "300x300>", thumb: "100x100>" },
      s3_credentials: "#{Rails.root}/config/aws.yml",
      bucket: "bucket-name",
      s3_host_name: "s3-ap-southeast-1.amazonaws.com",
      url: ":s3_domain_url",
      path: 'books/:id/photo/:style_:basename.:extension'
    

    worked for me :)

    0 讨论(0)
  • 2020-12-25 09:03

    You need to configure your region specific endpoint for the bucket (where it was created). You can do this with:

    AWS.config(:s3_endpoint => '...')
    s3 = AWS::S3.new
    

    or

    s3 = AWS::S3.new(:s3_endpoint => '...')
    

    You can avoid this in the future by using DNS comptible bucket names (also avoid dots in bucket names). If a bucket name is a valid subdomain, then you can address your bucket without configuring the region specific endpoint. Consider the following:

    http:://bucket-name.s3.amazonaws.com/path/to/object.txt
    

    Where the bucket is named "bucket-name" and the object key is "path/to/object.txt". This bucket could exist in any region, and yet you can access it using the "default" region. When the bucket name is not dns-compatible, then the url looks like:

    http://s3.amazon.com/bucket/name/path/to/object.txt
    

    In the example above, the bucket is "bucket/name", which is not dns compatible. It becomes part of the path, and now s3.amazon.com must be replaced with the region specific endpoint (if the bucket was not created in the classic region).

    As someone else mentioned, paths should be part of the object key, not bucket name. This allows you to group objects by a common prefix. The '/' is used as a virtual folder (by convention only).

    # print the key of every object with the given prefix
    s3.buckets['bucket-name'].objects.with_prefix('path/to/').each do |object|
      puts object.key
    end
    
    0 讨论(0)
  • 2020-12-25 09:09

    In case anyone is looking for this, here is how it should work according to https://github.com/aws/aws-sdk-ruby

    creds = JSON.load(File.read('secrets.json'))
    Aws.config[:credentials] = Aws::Credentials.new(creds['AccessKeyId'], creds['SecretAccessKey'])
    Aws.config[:region] = 'us-east-1'
    
    client = Aws::S3::Client.new(
        region: Aws.config[:region],
        credentials: Aws.config[:credentials]
    )
    
    File.open('/local_directory/picture.jpg', 'rb') do |file|
      client.put_object(bucket: 'bucket', key: 'folder_inside_bucket/picture.jpg', body: file)
    end
    
    0 讨论(0)
提交回复
热议问题