简体   繁体   English

从临时URL在S3中保存图片

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

I am developing a website on ruby on rails where users can upload pictures thanks to paperclip , it is stored in amazon S3 . 我正在开发一个基于ruby的网站,由于回形针 ,用户可以在其中上传图片,该图片存储在Amazon S3中 After, they can modify pictures thanks to aviary . 之后,他们可以借助鸟舍来修改图片。 But when i want to save the new pictures, aviary just gave me an temporary URL where i can get my modified picture. 但是,当我要保存新图片时,鸟笼只给了我一个临时URL,可以在其中获取修改过的图片。

Does paperclip can do it ? 回形针可以做到吗? I don't think it can save an picture from an URL and store it to S3 ? 我认为它不能从URL保存图片并将其存储到S3吗?

I've searched for a week now, and i don't know the best way to do it. 我已经搜索了一个星期,但我不知道这样做的最佳方法。 I've read about filepicker , but the account to store data in S3 files isn't free ... 我已经读过关于filepicker的文章 ,但是用于将数据存储在S3文件中的帐户不是免费的...

Finally i've heard about this s3 https://github.com/qoobaa/s3 , but i don't understand how to use it. 终于我听说了这个s3 https://github.com/qoobaa/s3 ,但是我不知道如何使用它。 I have installed gem s3, but when i set require 's3' , it is not recognize. 我已经安装了gem s3,但是当我设置require 's3' ,它无法识别。

What is the best to do? 最好的办法是什么?

Why don't you pass the URL that Aviary generates to your server and upload the new photo from there? 您为什么不将Aviary生成的URL传递到服务器并从那里上传新照片? The code below does that in Python/Django: 下面的代码在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')

Hope this helps. 希望这可以帮助。

Paperclip has matured a lot since this question was answered. 自从回答了这个问题以来,回形针已经成熟了很多。 If you want to save files by passing a URL, as of Paperclip v3.1.4, you can just assign the URL to your Paperclip attachment attribute. 从Paperclip v3.1.4开始,如果要通过传递URL保存文件,则只需将URL分配给Paperclip附件属性即可。

Let's say I have a class User and my attachment is called avatar . 假设我有一个User类,我的附件称为avatar We'll have the following in our User model: 我们的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/

In our view, we can define a hidden field that will accept the temporary URL received from Aviary: 在我们看来,我们可以定义一个隐藏字段,该字段将接受从Aviary收到的临时URL:

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

We can set the value of this hidden field with the Aviary onSave callback: 我们可以使用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();
  }
});

Within onSave, you can use AJAX to update the User object, use jQuery's .submit() to submit the form, or let the user submit it when they want. 在onSave中,您可以使用AJAX更新User对象,使用jQuery的.submit()提交表单,或者让用户在需要时提交表单。

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

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