asafdav / ng-s3upload

Upload to S3 using AngularJS
MIT License
190 stars 83 forks source link

Ruby server side code not working #22

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hi,

I am trying to use the ruby server code as my system is based on ruby on rails4, but I am facing the problem of call the server side code in the angularjs directive.

Here is my code:

<% def s3_access_token render json: { policy: s3_upload_policy, signature: s3_upload_signature, key: GLOBAL[:aws_key] } end

  def s3_upload_policy
    @policy ||= create_s3_upload_policy
  end

  def create_s3_upload_policy
    Base64.encode64(
      {
        "expiration" => 1.hour.from_now.utc.xmlschema,
        "conditions" => [ 
          { "bucket" =>  GLOBAL[:aws_bucket] },
          [ "starts-with", "$key", "" ],
          { "acl" => "public-read" },
          [ "starts-with", "$Content-Type", "" ],
          [ "content-length-range", 0, 10 * 1024 * 1024 ]
        ]
      }.to_json).gsub(/\n/,'')
  end

  def s3_upload_signature
    Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), GLOBAL[:aws_secret], s3_upload_policy)).gsub("\n","")
  end

%>

asafdav commented 10 years ago

Thanks for your reporting, can you please share with us, what was the problem ?

ghost commented 10 years ago

Hi, currently there are two problem:

  1. Because of the javascript, there is one problem:

XMLHttpRequest cannot load https://dsfdev.s3.amazonaws.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access

  1. I can disable the web security to resolve the problem temporarily.

And another problem is: AccessDeniedInvalid according to Policy: Policy Condition failed: ["eq", "$bucket", "dfsdev"]1DE92A2F063B43C9z2Ox9NGEWuk8e/vNfr139ORFxVIuxBDX+dm/wDgDNT0yPuwd1lLV6GV8imnVrjjb.

my backend code is as follows:

class UploadController < ApplicationController
  def s3_access_token
    render json: {
        policy:    s3_upload_policy,
        signature: s3_upload_signature,
       key:       "AKIAJFV6IVZXLBMZBZCA"
      }
end

protected

  def s3_upload_policy
    @policy ||= create_s3_upload_policy
  end

  def create_s3_upload_policy
    Base64.encode64(
      {
        "expiration" => 1.hour.from_now.utc.xmlschema,
        "conditions" => [ 
          { "bucket" =>  "dfsdev" },
          [ "starts-with", "$key", "" ],
          { "acl" => "public-read" },
          [ "starts-with", "$Content-Type", "" ],
          [ "content-length-range", 0, 10 * 1024 * 1024 ]
        ]
      }.to_json).gsub(/\n/,'')
  end

  def s3_upload_signature
    Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), "0l7WutMlquOOk+SFgiEmZeZE0jipF1O3UGSFVXZy", s3_upload_policy)).gsub("\n","")
  end
end

And the front end is:

<script type="text/ng-template" id="fileUploadPanel.html">
        <div s3-upload bucket="'dsfdev'" ng-model="product.remote_product_file_url"
           s3-upload-options="{getOptionsUri: '/upload/s3_access_token'}">
</script>