It's pretty straightforward but I have this issue:
Unexpected error while processing request: undefined method `PresignedPost' for Aws::S3:Module
My objective: Get the presigned URL for an object to which I can perform an upload file.
My gem has
gem 'aws-sdk', '~> 2'
Code:
##aws_creds = Aws::SharedCredentials.new(profile_name: profile)
Aws.config.update({region: 'us-west-2',credentials: ##aws_creds})
s3 = Aws::S3::Resource.new
#bucket = s3.bucket(bucketName)
form = Aws::S3::PresignedPost(:key => key )
if(form)
form.fields
end
you normally don't do a standalone presignedpost. you do it using the bucket method.
something like #bucket.presigned_post(:key=>key)
See doc:
http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/PresignedPost.html
Note: Normally you do not need to construct a PresignedPost yourself.
See Bucket#presigned_post and Object#presigned_post.
Related
I need to programatically modify feature files of cucumber.
I have parsed a feature file using gherkin's gem 'gherkin/parser'.
The problem I find is that after parsing, I end up with a hash with the following data as example:
{:type=>:GherkinDocument, :feature=>{:type=>:Feature, :tags=>[], :location=>{:line=>1, :column=>1}, :language=>"en", :keyword=>"Feature", :name=>"MyFeature", :description=>" As an user\n I want to test a feature", :children=>[{:type=>:Scenario, :tags=>[{:type=>:Tag, :location=>{:line=>5, :column=>3}, :name=>"#MyTag"}], :location=>{:line=>6, :column=>3}, :keyword=>"Scenario", :name=>"My scenario", :steps=>[{:type=>:Step, :location=>{:line=>7, :column=>5}, :keyword=>"Given ", :text=>"I start the app"}, {:type=>:Step, :location=>{:line=>8, :column=>5}, :keyword=>"And ", :text=>"I generate a test user"}, {:type=>:Step, :location=>{:line=>9, :column=>5}, :keyword=>"And ", :text=>"I finish the flow"}]}]}, :comments=>[]}
is it possible to convert this GherkinDocument generated by the parser to a plain text feature file to save it? What method or gem should I use to get
According to the docs, you would use the Ruby Gherkin::Pickles::Compiler:
require 'gherkin/parser'
require 'gherkin/pickles/compiler'
parser = Gherkin::Parser.new
gherkin_document = parser.parse("Feature: ...")
# Make changes to gherkin_document
pickles = Gherkin::Pickles::Compiler.new.compile(gherkin_document)
When having a look to my couchbase view using the web api I got this result:
{
"total_rows": 18279385,
"rows": []
}
But I'm using the ruby couchbase gem like follows
require 'couchbase'
c = Couchbase.connect(...)
sources = c.design_docs['Data']
pp sources.All
#<Couchbase::View:47373151271840 #endpoint="_design/Data/_view/All" #params={:connection_timeout=>75000}>
But how do I get the total_rows from the view? I've found few documentation which relates to a Method "total_rows" but that doesn't seem to be present at this point.
The comment of Anthony solves the problem:
sources.All(limit: 0).fetch.total_rows
Use limit: 0 for speeding up the request.
Any example how rollback to previous version of deleted file on s3 via ruby aws-sdk?
Looks like GEM aws-sdk-ruby not show deleted files in list of objects
s3 = Aws::S3::Resource.new
bucket = s3.bucket('aws-sdk')
bucket.objects.each do |obj|
if obj.key.start_with?("images/file_name.jpg")
puts obj.to_yaml
end
end
You can list previous versions like this:
aws_versions = s3.client.list_object_versions(
bucket: 'bucket_name',
prefix: 'images/12345/50x50.jpg'
).versions
https://docs.aws.amazon.com/sdk-for-ruby/v2/api/Aws/S3/Client.html#list_object_versions-instance_method
Download a necessary version like this:
cache = s3.client.get_object(
bucket: 'bucket_name',
key: 'images/12345/50x50.jpg',
version_id: 'your_version_id',
response_target: Rails.root.join('tmp/images/12345/50x50.jpg')
)
https://docs.aws.amazon.com/sdk-for-ruby/v2/api/Aws/S3/Client.html#get_object-instance_method
And finally, save it to your model:
model.attachment = Rails.root.join('tmp/images/12345/50x50.jpg').open
model.save
PS: It's good to have paper_trail installed, so you could find the previous version prefix (the images/12345/50x50.jpg part) in model change history.
I am having a struggle getting this to work so I've created a hell-world Rails app to try and get this to work.
Here's the repo with the code that is not working: https://github.com/pitosalas/shibtry
Here's what I've done starting from an empty Rails application:
I've added two gems to gem files:
gem 'omniauth-shibboleth'
gem 'rack-saml'
I got the shibboleth meta data from my university's web site and converted it using shib_conv.rb into the corresponding YAML: ./config.yml
I've updated routes adding get '/auth/:provider/callback', to: 'sessions#create'
I've put a breakpoint at SessionController#create
I've added initializers: omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :shibboleth, {
:shib_session_id_field => "Shib-Session-ID",
:shib_application_id_field => "Shib-Application-ID",
:debug => true,
:extra_fields => [
:"unscoped-affiliation",
:entitlement
]
}
end
I've added rack_sam.rb initializer:
Rails.application.config.middleware.insert_after Rack::ETag, Rack::Saml,
{ :metadata => "#{Rails.root}/config/metadata.yml"}
Now, run the server and go to http://0.0.0.0:3000/auth/shibboleth and I get an error:
undefined method `[]' for nil:NilClass'
which is traced back to this line in rack-saml/misc/onelogin_setting.rb line 13 which is:
settings.idp_sso_target_url = #metadata['saml2_http_redirect']
in other words, looking for the metadata hash for that key. It happens that in my metadata.yml file that key is present, but by the time I get to this onelogin_setting.rb line 13, #metadata is nil (it should contain the contents of the file) and consequently that key doesn't exist.
And that's where, for now, the trail dries up.
I bypassed Shibboleth totally. My goal was to allow login to my universities authentication system specifically to allow students to log in with their student login, which is fronted by google apps. So this was much easier: https://developers.google.com/identity/sign-in/web/
Looks like you forgot to add your config file to the initializer:
Rails.application.config.middleware.insert_after Rack::ETag, Rack::Saml,
{
:metadata => "#{Rails.root}/config/metadata.yml",
:config => "#{Rails.root}/config/rack-saml.yml"
}
And the saml_idp setting in the rack-saml.yml must match the key for the idp_lists entry in your metadata.yml
I am getting following errors on my Heroku logs:-
Excon::Errors::Forbidden (Expected(200) <=> Actual(403 Forbidden)
2013-10-02T16:25:51.131316+00:00 app[web.1]: response => #"\nInvalidAccessKeyIdThe AWS Access Key Id you provided does not exist in our records.5CA6A058BCE5D28AQ6grl4LPNO+F9YVtJZA7YIASYUFw4IpggAVlMJEzsdAhdwSWOTIB8K+VolEwyGYLS3_KEY", :headers=>{"x-amz-request-id"=>"5CA6A058BCE5D28A", "x-amz-id-2"=>"Q6grl4LPNO+F9YVtJZA7YIASYUFw4IpggAVlMJEzsdAhdwSWOTIB8K+VolEwyGYL", "Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked", "Date"=>"Wed, 02 Oct 2013 16:25:50 GMT", "Connection"=>"close", "Server"=>"AmazonS3"}, :status=>403, :remote_ip=>"176.32.100.200"}, #body="\nInvalidAccessKeyIdThe AWS Access Key Id you provided does not exist in our records.5CA6A058BCE5D28AQ6grl4LPNO+F9YVtJZA7YIASYUFw4IpggAVlMJEzsdAhdwSWOTIB8K+VolEwyGYL
I have checked the AWS key for at least a dozen times.
I have set up Heroku variables by using following:
heroku config:add S3_KEY=XXXXXXXXXXXXXXX S3_SECRET=XXXXXXXXXXXXXXXXXXXXXX
But I get the error as above.
Looks like your AWS access key is invalid. A couple things to double check:
Do your access key, secret key and bucket all match what's in the AWS dashboard?
Are you setting those variables correctly in your carrierwave initializer? You should be able to check by running the following from heroku run rails console: CarrierWave.configure { |config| puts config.fog_credentials; puts config.fog_directory }.
If you double and triple check those and there really isn't anything wrong, then you may have a weird problem with your S3 account (can you access your S3 account with another S3 utility using the same credentials?), or there's something loony happening in your code.
Good luck!
I was able to figure out using Taavo's suggestion. I used figaro gem where I did put AWS credentials into config/application.yml.
In addition, I also changed my carrierwave.rb file from:
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: "S3_KEY",
aws_secret_access_key: "S3_SECRET",
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = "S3_BUCKET_NAME"
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
to
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV["S3_KEY"],
aws_secret_access_key: ENV["S3_SECRET"],
#region: 'Northern California'
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV["S3_BUCKET_NAME"]
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
Then added following to Heroku:
$ heroku config:set S3_BUCKET_NAME=your_bucket_name
$ heroku config:set S3_KEY=your_access_key_id
$ heroku config:set S3_SECRET=your_secret_access_key
That did the work. Thanks Taavo for suggestions.