Reputation: 1021
I'm trying to get objects from AWS S3 with a certain words in a Ruby app. There are many files having "lesson_id_" as a part of key, and I would love to download all files including this key word. But if I search with 'lesson_id_*' it says, AWS::S3::Errors::NoSuchKey No Such Key.
I want to efficiently download all files including this key word. Please give me any feedback how to use "keywords" instead of perfect "key".
obj = S3_BUCKET.objects['lesson_id_*']
begin
File.open("name", 'wb') do |file|
obj.read do |chunk|
file.write(chunk)
end
end
rescue
end
Upvotes: 1
Views: 1195
Reputation: 3436
You can simply get the full key by using the following method
resp = api_client.list_objects({ bucket: <bucket-name>, prefix: 'your-prefix-'})
then get the exact file by using the .get_object
method.
file_path = resp.contents[0].key
s3_response = api_client.get_object({ bucket: <bucket-name>, key: file_path })
Upvotes: 0
Reputation: 31
You could do something like this. Get all the objects and check them before you download them. You can then use a regex or whatever you like to check the
require 'aws-sdk'
s3 = Aws::S3::Resource.new(region: 'us-west-2')
bucket = s3.bucket('your-bucket')
bucket.objects.limit(1000).each do |item|
if item.key.start_with?('lesson_id_')
obj = bucket.object(item.key)
obj.get(response_target: item.key)
end
end
Upvotes: 1