繁体   English   中英

从临时URL在S3中保存图片

[英]Save a picture in S3 from a temporary URL

我正在开发一个基于ruby的网站,由于回形针 ,用户可以在其中上传图片,该图片存储在Amazon 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 v3.1.4开始,如果要通过传递URL保存文件,则只需将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()提交表单,或者让用户在需要时提交表单。

暂无
暂无

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

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