2017-02-18 50 views
0

我一直在嘗試使用FineUploader JS庫直接上傳到S3。我在Rails中建立並返回一個S3策略和簽名的後期操作,我嘗試了很多不同的選項,但是我一直在FineUploader中的策略中出現錯誤。使用FineUploader的Rails中的S3策略錯誤

這裏是FineUploader JS我使用:

<script> 
    $('#fine-uploader-s3').fineUploaderS3({ 
     template: 'qq-template-s3', 
     request: { 
      endpoint: "https://mybucket.s3.amazonaws.com/", 
      accessKey: MY_ACCESS_KEY 
     }, 
     signature: { 
      endpoint: "/generatesignature" 
     }, 
     uploadSuccess: { 
      endpoint: "/success", 
      params: { 
       isBrowserPreviewCapable: qq.supportedFeatures.imagePreviews 
      } 
     }, 
     iframeSupport: { 
      localBlankPagePath: "/server/success.html" 
     }, 
     cors: { 
      expected: true 
     }, 
     chunking: { 
      enabled: true 
     }, 
     resume: { 
      enabled: true 
     }, 
     deleteFile: { 
      enabled: true, 
      method: "POST", 
      endpoint: "http://s3-demo.fineuploader.com/s3demo-thumbnails-cors.php" 
     }, 
     validation: { 
      itemLimit: 5, 
      sizeLimit: 15000000 
     }, 
     thumbnails: { 
      placeholders: { 
       notAvailablePath: "/plugins/fineuploader/placeholders/not_available-generic.png", 
       waitingPath: "/plugins/fineuploader/placeholders/waiting-generic.png" 
      } 
     }, 
     callbacks: { 
      onComplete: function(id, name, response) { 
       var previewLink = qq(this.getItemByFileId(id)).getByClass('preview-link')[0]; 

       if (response.success) { 
        previewLink.setAttribute("href", response.tempLink) 
       } 
      } 
     } 
    }); 
</script> 

這裏是紅寶石

的generatesignature行動服務器端
def generatesignature 
    bucket = MY_BUCKET 
    access_key = MY_ACCESS_KEY 
    secret = MY_SECRET_KEY 
    key = "test.txt/" 
    expiration = 5.minutes.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z') 
    max_filesize = 2.megabytes 
    acl = 'public-read' 
    sas = '200' # Tells amazon to redirect after success instead of returning xml 
    policy = Base64.encode64(
    "{'expiration': '#{expiration}', 
     'conditions': [ 
     {'bucket': '#{bucket}'}, 
     {'acl': '#{acl}'}, 
     {'Cache-Control': 'max-age=31536000'}, 
     ['starts-with', '$key', '#{key}'], 
     ['content-length-range', 1, #{max_filesize}] 
     ]} 
    ").gsub(/\n|\r/, '') 
    signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret, policy)).gsub(/\n| |\r/, '') 
    {:access_key => access_key, :key => key, :policy => policy, :signature => signature, :sas => sas, :bucket => bucket, :acl => acl, :expiration => expiration} 

params[:signature]= signature 
params[:policy] = policy 
render :json => params, :status => 200 and return 
end 

試圖上傳到S3的時候,我收到的錯誤是: 「根據策略無效:策略條件失敗:[」eq「,」$ acl「,」public-read「]」

+0

聽起來像是你的水桶政策 –

+0

的問題將桶政策問題是在Ruby或在CORS設置在S3政策的形成? –

回答

0

看起來好像您使用「public-read」的ACL值,但Fine Uploader發送給S3的策略默認使用「private」ACL。如果您真的想要使用「公開閱讀」ACL,則需要更新objectProperties.acl Fine Uploader S3 configuration value。例如:

$('#fine-uploader-s3').fineUploaderS3({ 
    objectProperties: { 
    acl: 'public-read' 
    }, 
    ... 
})