Skip to content

Instantly share code, notes, and snippets.

@sotayamashita
Last active December 5, 2018 13:49
Show Gist options
  • Save sotayamashita/60eeb6a43dbc340d018e92d100bed5dd to your computer and use it in GitHub Desktop.
Save sotayamashita/60eeb6a43dbc340d018e92d100bed5dd to your computer and use it in GitHub Desktop.
TIL: How to control files on S3 from Ruby SDK
s3 = Aws::S3::Client.new(
  access_key_id: <>,
  secret_access_key: <>,
  region: <>,
)

Create a directory

Amazon S3 has a flat structure with no hierarchy like you would see in a file system. However, you can create a directory structure. https://docs.aws.amazon.com/AmazonS3/latest/user-guide/using-folders.html

s3.bucket(<bucket name>).object("directory1/text1.txt").put(body: "<text1.txt's content>")

Download a image

# with Class: Aws::S3::Resource

File.open('filename', 'wb') do |file|
  s3.bucket('bucket-name').object('object-key').get(response_target: file)
end
# with Class: Aws::S3::Client

File.open('filename', 'wb') do |file|
  s3.get_object({ bucket:'bucket-name', key:'object-key'}, target: file)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment