在轨道教程第11章由迈克尔·哈特尔最后我成功地设法通过创建一个水桶,使用IAM设置用户和授权用户的AmazonS3FullAccess政策,使用户上传到亚马逊S3服务.感觉很脏,很没有安全感,让我的网站上未知的用户有在我的网站全面进入水桶的上传图片和我不知道我是否应该有这样的感觉.我创建了一个自定义策略
以下是:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1445501067518",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::bucketname"
}
]
}
我对我的解决方案没有信心,也无法找到任何答案谷歌搜索最佳方式.我正在使用carrierwave(有意使用carrierwave_direct用于我自己的项目),雾和mini_magick宝石.
ruby-on-rails amazon-s3 amazon-web-services railstutorial.org amazon-iam
我正在经历"以艰难的方式学习Ruby",而在练习20中,有一段我不理解的代码片段.我不明白为什么在函数"print_a_line"中调用f的gets.chomp.
input_file = ARGV.first
def print_all(f)
puts f.read
end
def rewind(f)
f.seek(0)
end
def print_a_line(line_count, f)
puts "#{line_count}, #{f.gets.chomp}"
end
current_file = open(input_file)
puts "First let's print the whole file:\n"
print_all(current_file)
puts "Now let's rewind, kind of like a tape."
rewind(current_file)
puts "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
Run Code Online (Sandbox Code Playgroud)
因此,我不明白输出的第二部分是如何产生的.我理解它是传递给代码的test.txt文件的前3行,但我不明白f.gets.chomp是如何产生这一行的.
$ ruby ex20.rb test.txt
First let's print the whole file:
This is line 1 …Run Code Online (Sandbox Code Playgroud) 我试图得到一个两和算法来传递LeetCode并且遇到时间限制导致它失败.
我不确定如何使我的代码更快以使其通过.在这个问题中,我得到一个整数数组(nums)和一个目标值,nums数组中的两个数字必须相加.找到这些数字后,以数组的形式返回其索引值.
def two_sum(nums, target)
numbers_hash = Hash[(0...nums.size).zip(nums)]
numbers_hash.delete_if do |k, v|
key_indicies = ((numbers_hash.select { |ki,vi| vi == (target - v) }.keys) - [k])
key_indicies.empty? ? true : (return [k, key_indicies].flatten)
end
return "Couldn't find target value"
end
Run Code Online (Sandbox Code Playgroud)
我可以保留原始的nums数组并在循环内执行循环,delete_if以修改迭代的数组,并在找到正确的目标值时返回.我更喜欢使用哈希方法,因为它更具可读性,我认为将数组转换为哈希的时间限制并不多.
我正在关注"The Bastards Book of Ruby",我正在尝试使用nokogiri构建一个webscraper,但是当我尝试运行代码时,大约四分之一的内容会抛出错误:
Crawler.rb:6:in `mkdir': No such file or directory @ dir_s_mkdir - data-hold/nobel (Errno::ENOENT)
from Crawler.rb:6:in `<main>'
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
DATA_DIR = "data-hold/nobel"
Dir.mkdir(DATA_DIR) unless File.exists?(DATA_DIR)
BASE_WIKIPEDIA_URL = "http://en.wikipedia.org"
LIST_URL = "#{BASE_WIKIPEDIA_URL}/wiki/List_of_Nobel_laureates"
HEADERS_HASH = {"User-Agent" => "Ruby/#{RUBY_VERSION}"}
page = Nokogiri::HTML(open(LIST_URL))
rows = page.css('div.mw-content-ltr table.wikitable tr')
rows[1..-2].each do |row|
hrefs = row.css("td a").map{ |a|
a['href'] if a['href'] =~ /^\/wiki\//
}.compact.uniq
hrefs.each do |href|
remote_url = BASE_WIKIPEDIA_URL + href
local_fname = "#{DATA_DIR}/#{File.basename(href)}.html"
unless File.exists?(local_fname)
puts …Run Code Online (Sandbox Code Playgroud)