首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >google app engine的ferris2.2框架(python),将图片上传到云存储?

google app engine的ferris2.2框架(python),将图片上传到云存储?
EN

Stack Overflow用户
提问于 2015-10-05 23:57:51
回答 2查看 111关注 0票数 0

所以..。这是我上传带有Ferris2框架的图像的代码。太棒了,它起作用了。但是,看看我是如何注释掉gcs.open的(...?我不想让它被注释掉。我真的很喜欢使用这个调用直接上传到云存储,而不需要使用任何与blobs相关的东西。如果我坚持使用AClassForm和ferris框架,那么完成此任务的最简单方法是什么?

代码语言:javascript
复制
class AClassForm(forms.model_form(AClass, exclude=('image_url') ) ):
     image = FileField(u'Image File') 

class AClasses(Controller): 

    class Meta: 

        Model = AClass 

        prefixes = ('admin',) 

        components = (scaffold.Scaffolding, Upload) 

        Form = AClassForm 



    admin_list = scaffold.list 

    admin_view = scaffold.view 

    admin_edit = scaffold.edit 

    admin_delete = scaffold.delete 



    def admin_add(self): 

        self.scaffold.ModelForm = AClassForm 

        self.scaffold.form_encoding = "multipart/form-data"    

        def before_save_callback(controller,container, item): 

            image = self.request.params["image"] 

            object_name = blobstore.parse_file_info(image).gs_object_name.split('/')[-1] 

            upload_settings = settings.get("upload") 

            url =  upload_settings["url"] 

            bucket = upload_settings["bucket"] 

            #send to the cloud 

            #write a task to execute this? 

            item.image_url  =  url % (bucket, object_name) 

            #gcs_file= gcs.open("/".join(["", bucket, object_name]),   

               # 'w', content_type="image/jpeg",  

                #options={'x-goog-acl': 'public-read'} )  

            #gcs_file.write(item.image)#.file.encode('utf-8')) # 

            #gcs_file.close() 

            return 

        self.events.scaffold_before_save += before_save_callback 

        return scaffold.add(self)
EN

Stack Overflow用户

发布于 2015-10-06 21:09:44

据我所知,如果你使用的是Ferris的Upload组件,你就无法摆脱Blobstore,但下面这一点非常接近。如果您不想使用Form类,则不必使用它,我自己很少使用它。所以想象一下下面的控制器:

代码语言:javascript
复制
from ferris import Controller, route
from ferris.components.upload import Upload
import cloudstorage as gcs
from google.appengine.ext import blobstore
import logging


class ImageManager(Controller):
    class Meta:
        components = (Upload,)  

    @route
    def list(self):
        #This just passes the upload URL to use in the form
        self.context['upload_url'] = self.components.upload.generate_upload_url(uri=self.uri('image_manager:image_uploader_action'))


    @route
    def image_uploader_action(self):
        # This gets all of the uploads passed in from the form
        uploads = self.components.upload.get_uploads()

        # This is the raw google cloud object reference. 'myfile' is the name of the upload field in the html form
        file_gcs_obj_name = uploads['myfile'][0].cloud_storage.gs_object_name 

        # This is the blobstore key just for giggles
        file_blobstore_key = uploads['myfile'][0].key()

        # This will get rid of the preceeding junk you don't need i.e. "/gs/yadda/yadda"
        clean_file_name = file_gcs_obj_name[3:]

        # This is the name of the file as it was uploaded by the end-user
        file_name_friendly  = uploads['myfile'][0].filename

        # This is the actual file, with this you can do whatever you want
        the_actual_image = gcs.open(clean_file_name,'r')

        # The file name by default is long and ugly, lets make a copy of the file with a more friendly name
        new_filename = '/mydomain.appspot.com/'+file_name_friendly
        gcs.copy2(clean_file_name,new_filename)

        # We can generate a serving URL by using the blobstore API
        valid_blob_reference = blobstore.create_gs_key('/gs'+new_filename)
        file_serving_url = images.get_serving_url(valid_blob_reference)
        logging.info('the serving url is: %s'% file_serving_url)

        # delete the original image from cloud storage
        gcs.delete(clean_file_name)

        # Delete the original image from blobstore
        blobstore.delete(file_blobstore_key)

        # Close the file
        the_actual_image.close()

        return 'Done. go check the cloud storage browser'

现在您所需要的就是HTML表单。您可以使用类似以下内容:

代码语言:javascript
复制
{% extends "layouts/default.html" %}
{% block layout_content %}
<form name="myform" action="{{upload_url}}" method="POST" enctype="multipart/form-data">
    <input type="file" name="myfile" id="fileToUpload">
    <input type="submit" value="Upload File" name="submit">
</form>
{% endblock %}

Ferris仍然会在blobstore中放置一个文件,但您可以在使用cloudstorage.copy2()函数后将其删除。这个功能是相当新的,所以记得更新你的cloudstorage包,你可以从谷歌或https://pypi.python.org/pypi/GoogleAppEngineCloudStorageClient/1.9.22.1下载最新的副本,希望这能有所帮助。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32952814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档