繁体   English   中英

如何 s3 与 cloudfront 一起使用的对象 URL?

[英]How to s3 object URL that works with cloudfront?

我目前正在 S3 上私下存储文件。 在我的 Rails 应用程序中,在attachment.rb 模型中,我可以获得私有文件的公共 URL,如下所示:

def cdn_url ( style='original' )
  attachment.s3_object(style).url_for( :read, secure: true, response_content_type: self.meta['file_content_type'], expires: 1.hour ).to_s
end

问题是这是为 S3 提供 URL 并重写 URL 以使用我的 Cloudfront 源 URL 时出错:

我们计算的请求签名与您提供的签名不匹配。 检查您的密钥和签名方法。

如何获得如下所示的公共 URL 资产但通过 Cloudfront 提供资产?

第一种方式(简单)

只需使用aws_cf_signer gem。 把它放在你的捆绑器里。

有了这个,你可以做类似的事情

def cdn_url (options = {})
  style = options[:style] || 'original'
  cloudfront_domain =  options[:cloudfront_domain] || 'example.cloudfront.net'
  cloudfront_pem_key_path = options[:cloudfront_pem_key_path]
  cloudfront_key_paid_id = options[:cloundfrount_key_paid_id] 
  path = attachment.path(style)  #path of the file
  # you can get this values from your aws a/c , most probably by going int 
  # https://console.aws.amazon.com/iam/home?#security_credential      
  signer = AwsCfSigner.new(cloudfront_pem_key_path, cloudfront_key_paid_id)
  # this configuration may vary. 
  # visit https://github.com/dylanvaughn/aws_cf_signer 
  # and check all available settings/options   
  url = signer.sign(path, :ending => Time.now + 3600)
  cloudfront_domain + url
end 

有了这个,你可以用这样的东西访问网址

  cdn_url(cloudfront_pem_key_path: '/users/downloads/pri.pem' , cloudfront_key_paid_id: '33243424XXX')

第二种方式

# A simple function to return a signed, expiring url for Amazon Cloudfront. 
# This will require openssl, digest/sha1, base64 and maybe other libraries. 
 
module CloudFront
  def get_signed_expiring_url(domain,path, expires_in, private_key_filename, key_pair_id)
 
    # AWS works on UTC, so make sure you are not using local time
    expires = (Time.now.getutc + expires_in).to_i.to_s
 
    private_key = OpenSSL::PKey::RSA.new(File.read(private_key_filename))
 
    # path should be your S3 path without a leading slash and without a file extension.
    # e.g. files/private/52
    policy = %Q[{"Statement":[{"Resource":"#{path}","Condition":{"DateLessThan":{"AWS:EpochTime":#{expires}}}}]}]
    signature = Base64.strict_encode64(private_key.sign(OpenSSL::Digest::SHA1.new, policy))
 
    # I'm not sure exactly why this is required, but it's in Amazon's perl script and seems necessary
    # Different base64 implementations maybe? 
    signature.tr!("+=/", "-_~")
    
    "#{domain}#{path}?Expires=#{expires}&Signature=#{signature}&Key-Pair-Id=#{key_pair_id}"
  end
end
 

有了这个,你可以做类似的事情

def cdn_url ( style='original',cloudfront_pem_key_path,key_pair_id)
  path = attachment.path(style)  #path of the file
  # you can get this values from your aws a/c , most probably by going int 
  CloudFront.get_signed_expiring_url 'example.cloudfront.net', path, 45.seconds ,'/users/downloads/pri.pem', 'as12XXXXX')
end
 

试一试,也许它会奏效。 请务必正确设置正确的存储桶访问策略。 如果您看到accessDenied错误, 检查一下http://www.jppinto.com/2011/12/access-denied-to-file-amazon-s3-bucket/

使用aws sdk gem

请参阅API 文档

有关为对象操作生成预签名 URL 的详细信息

提供访问密钥 ID 和秘密访问密钥:-

S3 = AWS::S3.new(
 :access_key_id => 'access_key_id',
 :secret_access_key => 'secret_access_key')

在控制器中放置这些行:--

bucket = S3.buckets['bucket_name']
s3_obj = bucket.objects["Path-to-file"]
return s3_obj.url_for(:read, :expires => 60*60).to_s

此链接将在 1 小时后失效。 之后链接将无法访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM