Aws-Sdk for Ruby Access Folder Within Bucket

aws-sdk for Ruby access folder within bucket

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.

put object to a specific folder in aws S3 ruby

Try this,

s3 = Aws::S3::Resource.new
path = 'photos/deepak_file/aws_test.txt'
s3.bucket('storagy-teen-dev-us').object(path).upload_file(./tmp/aws_test.txt)

you just need to specify full path of directory with file name.

Retrieving list of files in certain folder in AWS S3(v 2.4.4) and deleting them

You have syntax error, options need to be a Hash. Your command should be:

bucket.objects({prefix: '/tmp-files/'})

For more options, refer docs: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Bucket.html#objects-instance_method

list all the files from s3 using aws-sdk gem

You can simple iterate over bucket objects and use the with_prefix method

s3.buckets[YOUR BUCKET NAME].objects.with_prefix('videos/my_videos/college').each.collect(&:key)
#=> ["videos/my_videos/college/myfirst_day.mp4"]

OR use the as_tree method

s3.buckets[YOUR BUCKET NAME].as_tree(prefix:'videos/my_videos/college').select(&:leaf?).collect(&:key)
#=> ['videos/my_videos/college/myfirst_day.mp4']

Obviously these are fictional since I have no access to your bucket but take a look at ObjectCollection and Tree for more methods in the AWSSDK.

There are quite a few methods for bucket traversal available such as Tree responds to children which will list both LeafNodes (File) and BranchNodes (Directory). BranchNodes will then also respond to children so you can make this recursive if needed.

To get the suffix (e.g. just the filename) you could possibly patch these in.

class LeafNode
def suffix
@member.key.split(delimiter).pop
end
end
class S3Object
def suffix
@key.split("/").pop
end
end

I have not fully tested these in any way but they should work for returning just the file name itself if it is nested inside a branch.

AWS S3: The bucket you are attempting to access must be addressed using the specified endpoint

It seems likely that this bucket was created in a different region, IE not us-west-2. That's the only time I've seen "The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint."

US Standard is us-east-1

create empty folder S3 ruby SDK

The Amazon S3 virtual folders spring into existence when any key contains a '/'. When browsing a bucket in the S3 console, it scans object keys for common prefixes and then uses that prefix to show a subset of a bucket.

Given the following object keys:

  • photos/family/reunion.jpg
  • photos/family/vacation.jpg
  • videos/funny.mp4

Then Amazon S3 would show the top level folders "photos" and "videos". If you delete the "videos/funny.mp4" object, then the "videos" directory would disappear.

Amazon S3: set permissions using aws-sdk-ruby

From their docs @ aws-sdk doc

als you may want to reference the bucket doc

S3 supports a number of canned ACLs for buckets and objects. These include:

:private
:public_read
:public_read_write
:authenticated_read
:bucket_owner_read (object-only)
:bucket_owner_full_control (object-only)
:log_delivery_write (bucket-only)

Here is an example of providing a canned ACL to a bucket:

s3.buckets['bucket-name'].acl = :public_read

How can I enumerate all files in an S3 folder within a bucket?

You can use the .with_prefix method:

bucket.objects.with_prefix('some/folder/')

You can then fetch them one by one. The above returns an Enumerator. If you call .map on it, you can access, e.g., when it was .last_modified, among other things.



Related Topics



Leave a reply



Submit