从临时URL保存S3中的图片

我正在开发一个ruby on rails的网站,用户可以通过回形针上传图片,它存储在亚马逊S3中 。 之后,他们可以通过鸟舍修改图片。 但是当我想要保存新图片时,鸟舍只给了我一个临时URL,我可以在其中获取我修改后的图片。

回形针可以做到吗? 我不认为它可以保存URL中的图片并将其存储到S3?

我现在搜索了一个星期,我不知道最好的方法。 我读过有关filepicker的内容 ,但是在S3文件中存储数据的帐户并不是免费的…

最后我听说过这个s3 https://github.com/qoobaa/s3 ,但我不明白如何使用它。 我已经安装了gem s3,但是当我设置require 's3' ,它无法识别。

什么是最好的?

为什么不将Aviary生成的URL传递给您的服务器并从那里上传新照片? 下面的代码在Python / Django中执行:

 @login_required @csrf_exempt def upload_from_url(request): origin_url = request.POST.get("origin_url") name = request.POST.get("name") try: conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY) bucket_name = settings.AWS_UGC_STORAGE_BUCKET_NAME bucket = conn.get_bucket(bucket_name) k = Key(bucket) k.key = name file_object = urllib2.urlopen(origin_url) fp = StringIO.StringIO(file_object.read()) k.set_contents_from_file(fp) return HttpResponse("Success") except Exception, e: return HttpResponse(e, mimetype='application/javascript') 

希望这可以帮助。

自回答这个问题以来,Paperclip已经成熟了很多。 如果要通过传递URL来保存文件,从Paperclip v3.1.4开始,您只需将URL分配给Paperclip附件属性即可。

假设我有一个类User ,我的附件叫做avatar 。 我们的User模型中包含以下内容:

 has_attached_file :avatar # Validate the attached image is image/jpg, image/png, etc # This is required by later releases of Paperclip validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 

在我们看来,我们可以定义一个隐藏字段,该字段将接受从Aviary收到的临时URL:

 = f.hidden_field :avatar, id: 'avatar' 

我们可以使用Aviary onSave回调设置此隐藏字段的值:

 var featherEditor = new Aviary.Feather({ apiKey: '#{ENV['AVIARY_KEY']}', onSave: function(imageID, newURL) { var img = document.getElementById(imageID); img.src = newURL; var avatar = document.getElementById('avatar'); avatar.value = newURL; featherEditor.close(); } }); 

在onSave中,您可以使用AJAX更新User对象,使用jQuery的.submit()提交表单,或者让用户在需要时提交。