Retrieving all versions of an S3 Object - ruby

I'm looking to move from version 1 of the AWS SDK for Ruby to version 2. However I've hit a snag with S3 object versioning.
Given a reference to an S3 object in Version 1 of the API you could retrieve all versions of just that object: http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html#versions-instance_method
However Version 2 of the API does not seem to replicate this feature: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Object.html
Am I missing something?

I think you're correct, and this feature is missing from the V2 API. I believe your only options are bucket.object_versions or client.list_object_versions.
You can retrieve all versions of an S3 object from the bucket like this:
# Retrieve Collection<ObjectVersion>
Aws::S3::Bucket.new('bucket-name')
.object_versions(prefix: 'object-key')
.reject { |version| version.key != 'object-key' }
I would guess that the Ruby SDK has made this change to better reflect the S3 REST API, where versions is its own subresource and objects don't have any knowledge of their own version history.

Related

Amazon S3 get S3 Object by URL in Ruby SDK

I am looking to rename (or move) my S3 objects, I have their URLs like https://s3.eu-west-2.amazonaws.com/sample-bucket/temp/sample-picture.jpg
Is there any standard way in Ruby SDK to get Aws::S3::Object by only URL or I have to parse it by regular expression for example ?
In Java SDK there is AmazonS3URI.java
I haven't found any method to just take the s3 url and get the object. I ended up with parsing it by myself.

How can I get image list and download these images from public-shared-gallery-link by using Dropbox SDK?

I want to get the image list and download those images from a Dropbox's public-shared-link, which I get it from my client.
I'm using Dropbox SDK for ruby and only find the methods to manage files via my Dropbox account, such as put_file, get_file, get_file_and_metadata, get_chunked_uploader, upload and so on.
Is there any way to do that?
Dropbox API v1 has an endpoint that allows you to retrieve metadata about shared links:
https://www.dropbox.com/developers-v1/core/docs#metadata-link
Dropbox API v2 has a similar pair of endpoints that allows you to retrieve metadata and file content, respectively, from shared links:
https://www.dropbox.com/developers/documentation/http/documentation#sharing-get_shared_link_metadata
https://www.dropbox.com/developers/documentation/http/documentation#sharing-get_shared_link_file
Unfortunately, none of these are currently implemented in the Ruby SDK. You can call them directly though, or modify the Ruby SDK to do so.

Getting readable information from Amazon sdk ruby

Maybe someone could help me with an issue while working with Amazon SDK for Ruby.
When trying to retrieve information with commands like "get_bucket_login" or "get_bucket_location" what I get as a response is:
<Seahorse::Client::Response:....>
According to the documentation, these requests should return strings.
What am I missing? Someone found the same issue?
Seahorse is part of the core of the SDK:
https://github.com/aws/aws-sdk-ruby/tree/master/aws-sdk-core/lib/seahorse
The clients of all the services are modeled to use the Base of this.
Now back to your question:
You get an "empty" response as in it does not conform to what the client base is used to.
=== for get_bucket_location ===
the way to get your bucket location is the following:
resp = s3.get_bucket_location(bucket: "mybucketname")
puts resp.data.location_constraint
empty string means US Standard, per documentation here: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
the code that monkey patches this in is here: https://github.com/aws/aws-sdk-ruby/blob/53712d3e4583c982837fb3a301fa2c67226a05ff/aws-sdk-core/lib/aws-sdk-core/plugins/s3_get_bucket_location_fix.rb
== for get_bucket_login ==
I don't see this method on the client. If it's logging instead of login, you can see the response structure in the documentation: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html#get_bucket_logging-instance_method

How to mock aws-sdk gem?

I have some code that uploads a file to Amazon S3, using the aws-sdk gem. Apparently it does an HTTP put to upload the file.
Is there a good way to mock this functionality of the aws-sdk gem?
I tried using Webmock, but the aws-sdk gem seems to do a get latest/meta-data/iam/security-credentials/ first. It seems that using Webmock may not be the best way to mock this functionality.
Working in RSpec.
If you're using version 2 of the aws-sdk gem try adding:
Aws.config.update(stub_responses: true)
to your RSpec.configure block (usually found in your rails_helper.rb file)
While the above works, it will return empty responses if you don't further specify response content - not necessarily valid, but stubbed.
You can generate and return stubbed response data from a named operation:
s3 = Aws::S3::Client.new
s3.stub_data(:list_buckets)
#=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[], owner=#<struct Aws::S3::Types::Owner display_name="DisplayName", id="ID">>
In addition to generating default stubs, you can provide data to apply to the response stub.
s3.stub_data(:list_buckets, buckets:[{name:'aws-sdk'}])
#=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[#<struct Aws::S3::Types::Bucket name="aws-sdk", creation_date=nil>], owner=#<struct Aws::S3::Types::Owner display_name="DisplayName", id="ID">>
For more details refer to: http://docs.aws.amazon.com/sdkforruby/api/Aws/ClientStubs.html
There are a lot of ways to mock requests in the AWS SDK for Ruby. Trevor Rowe recently posted an article on using the SDK's native support for object stubbing, which does not require any external dependencies like Webmock. You can also use tools like VCR (link will send you to another blog post) to build cacheable integration tests; this way you can test against the live service when you want accuracy and avoid hitting network when you want speed.
Regarding the get request on latest/meta-data/iam/security-credentials/, this happens because the SDK is trying to look up credentials, and, if none are provided, it will check if you are running on an EC2 instance as a last resort, causing the SDK to make an extra HTTP request. You can avoid this check by simply providing bogus static credentials, though if you are using something like VCR, you will want to provide valid credentials for the first run. You can read about how to provide static credentials in another blog post that Trevor wrote on credential management (this should also be in the developer guide and SDK documentation).

How can I obtain the Hosted Zone ID of an AWS ELB in Ruby?

I'm writing some Ruby code to automatically update Route53 DNS zones (domains) using the Zone apex virtual A record feature. However, to setup such records requires the Elastic Load-balancer Hosted Zone ID in addition to the FQDN.
Does anyone know the best way to do that? (i.e. any gems etc that can do it?)
I'm currently using the appoxy aws gem in combination with pcorliss's route53 gem.
Thanks.
In the modern AWS Ruby SDK's ELB module, I came up with this solution (partially pseudo-code):
credentials_data = # something
config = AWS.config(credentials_data)
elb_name = 'your.elb.dns.name.elb.amazonaws.com.'
elb_client = config.elb_client
response = elb_client.describe_load_balancers()
elbs = response[:load_balancer_descriptions]
the_elb = elbs.select {|elb| elb[:dns_name] == elb_name}.first
the_hosted_id = the_elb[:canonical_hosted_zone_name_id]
Then, when doing your Route53 stuff, later, you can include this data as:
# ...
:alias_target => {
:dns_name => elb_name,
:hosted_zone_id => the_hosted_id,
:evaluate_target_health => true, # or false, if you wish
},
# ...
I admit, I do wish there was a nice "ruby-ish" (more idiomatic) front-end to all of this (or perhaps there is, and I just need to go find it), but the above worked for me using the stock aws-sdk gem. Hopefully that's helpful for someone at some point.
The Aws gem Aws::Elb code is using AWS ELB API version 2009-05-15. There have been several API version updates by Amazon since then - the latest currently being 2011-11-15.
The format of the DescribeLoadBalaners response was changed between version 2009-05-15 and version 2009-11-25, which breaks the current Aws::Elb code (Listeners was changed to ListenerDescriptions). Also, the hosted zone elements in the response weren't added until version 2011-04-05.
It is possible to get the hosted zone information by hacking the Aws::Elb source to use the latest API and extract the relevant data.
Now we just need to get the Aws maintainers to update the official gem.

Resources