Reputation: 2170
I am using amazon s3, rails 4, and the FOG gem. I have an amazon bucket called uipstudy with 100 folders, each containing about 20 images. I use the following to get all the images in a specific folder (In my application_helper.rb which is included in the application_controller.rb).
def get_files(image_folder)
connection = Fog::Storage.new(
provider: 'AWS',
aws_access_key_id: '######',
aws_secret_access_key: '#######'
)
connection.directories.get('uipimages', prefix:image_folder).files.map do |file|
file.key
end
end
In my controller I have this....in this example I am looking in the folder "1" in the uipstudy bucket.
#Amazon solution:
@images = get_files('1')
@images.each do |image|
image = "https://s3.amazonaws.com/uipstudy/#{image}"
@image_array << image
end
The problem is that its returning the files inside the folder labelled "1" but also in 10, 11, 12,13....etc. I assumed that the prefix was an absolute but it appears not. Is there a way to enforce that the prefix gets exactly the folder specified in the prefix?
Upvotes: 0
Views: 850
Reputation: 2542
I think you should be able to make a small change in your script to get the behavior you want. Simply append a forward slash to the prefix so that it clearly shows you want things that are like a directory instead of any/all things that begin with a particular character.
So, that would get you something like:
directory = connection.directories.get('upimages', prefix: image_folder + '/')
directory.files.map do |file|
file.key
end
(I just split it into two commands to make it format/read easier)
Upvotes: 1
Reputation: 26169
Below is my solution using the aws-sdk gem.
s3 = AWS::S3.new
bucket = s3.buckets[ENV['AWS_BUCKET']]
regex = %r{_inbox/(?:[^/]+/)*[^/]+\.ipa}i
bucket.objects.select { |o| o.key.match(regex) }.each do |ipa|
Upvotes: 1