2014-03-04 2 views
0

저는 Python을 사용하여 Google App Engine에서 전자 상거래 플랫폼을 만들고 있습니다. 이를 위해 별도의 엔티티에서 제공 할 제품의 세부 정보를 저장해야합니다.이 엔티티에는 모든 개별 제품의 이미지도 포함됩니다. 이제 이미지를 데이터 저장소에 저장하는 방법은 무엇입니까? 또한, 더 중요한 것은 데이터베이스에서 이미지를 추출하여 응용 프로그램에서 사용자에게 표시하는 방법입니다.Google App Engine : 데이터베이스에 이미지 저장 (Python)

편집 :

에 '이미지'실체에 내 이미지를 업로드 한 후, 나는 그것을 표시하려고했다. 여기에 같은에 대한 코드입니다 :

main.py가 template.html에,

class FileUpload(Handler): 
    def post(self): 
     file_upload = self.request.POST.get("file", None) 
     file_name = file_upload.filename 
     image = Images(id=file_name, file_name=file_name, blob=file_upload.file.read()) 
     image.put() 

     self.response.headers[b'Content-Type'] = mimetypes.guess_type(image.file_name)[0] 
     self.response.write(image.blob) 
     self.final() 

    def final(self): 
     images = db.GqlQuery("Select * FROM Images WHERE file_name = 'CN.jpg'") 
     self.render("template.html", images = images) 

class Blob(Handler): 
    def get(self): 
     self.render("blob.html") 


app = webapp2.WSGIApplication([('/', MainPage), 
           ('/signup', Register), 
           ('/login', Login), 
           ('/logout', Logout), 
           ('/mp', MP), 
           (r'/file_upload', FileUpload), 
           ('/blob', Blob)], debug=True) 

template.html

<html> 
<head> 
    <title>Template</title> 
</head> 
<body> 
    {% 
     for image in images 
    %} 
    <div>{{image.blob}}</div> 
    {%endfor%} 
</body> 
</html> 

blob.html

<!DOCTYPE HTML> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Image Upload</title> 
</head> 
<body> 
<form action="/file_upload" enctype="multipart/form-data" method="post"> 
    <div><input type="file" name="file"/></div> 
    <div><input type="submit" value="Upload"></div> 
</form> 
</body> 
</html> 

지금, 나는이었다 내가 추출하려고하는 이미지가 표시 될 것으로 기대합니다. 그러나 다음 오류가 발생합니다.

<div>{{image.blob}}</div> 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128) 
+1

블로 브 스토어를 살펴보십시오. 또한 이미지 문서를 참조하십시오. 도움이 될만한 것이 있습니다. https://developers.google.com/appengine/docs/python/images/usingimages – mgilson

답변

5

이미지를 업로드하고 데이터 저장소에 저장하고 이미지를 제공하는 기본 예입니다.

파일 업로드.PY : 화상 서버

webapp2.Route(r'/file_upload', handler='fileupload.FileUpload') 

라우팅 : <img alt="test" src="/img_serve/test.png" />

webapp2.Route(r'/img_serve/<resource:(.*)>', handler='fileupload.ImgServe')) 
형태 포스트 라우팅

<!DOCTYPE HTML> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Image Upload</title> 
</head> 
<body> 
<form action="/file_upload" enctype="multipart/form-data" method="post"> 
    <div><input type="file" name="file"/></div> 
    <div><input type="submit" value="Upload"></div> 
</form> 
</body> 
</html> 

: 여기

import webapp2 
from google.appengine.ext import ndb 
import mimetypes 


class Images(ndb.Model): 
    file_name = ndb.StringProperty() 
    blob = ndb.BlobProperty() 


class FileUpload(webapp2.RequestHandler): 

    def post(self): 

     file_upload = self.request.POST.get("file", None) 
     file_name = file_upload.filename 
     image = Images(id=file_name, file_name=file_name, blob=file_upload.file.read()) 
     image.put() 

     self.response.headers[b'Content-Type'] = mimetypes.guess_type(image.file_name)[0] 
     self.response.write(image.blob) 


class ImgServe(webapp2.Requesthandler): 

    def get(self, resource): 

     image = ndb.Key('Images', resource).get() 
     self.response.headers[b'Content-Type'] = mimetypes.guess_type(image.file_name)[0] 
     self.response.write(image.blob)    

이미지를 게시하는 정적 형태

+0

정말 감사했습니다. 감사합니다. –

+0

원래 게시물을 수정했습니다. 데이터 저장소의 '이미지'엔티티에 저장된 이미지를 표시 할 수 없습니다. –

+1

이렇게하면 이미지를 표시 할 수 없습니다. HTML 태그를 사용해야합니다.이 태그는 핸들러 (ImgServe)에 연결되며 예제에서 보여준 것처럼 이미지를 보여줍니다. 예 : test voscausa

2

Google 고성능 이미지 검색을 사용할 수 있습니다. blobstore 또는 Google Cloud Storage에 이미지를 저장하거나 업로드하고 serve_url을 만들어야합니다.

장점 :

  • 고성능
  • 구글이 이미지를 제공합니다. 핸들러가 필요 없습니다.
  • 저렴한
  • 동적 크기 조정 및 자르기.

더 : https://developers.google.com/appengine/docs/python/blobstore/ get_serving_url :의 Blob 봉사 https://developers.google.com/appengine/docs/python/images/functions

+0

로컬 컴퓨터의 이미지를 데이터 저장소에 추가 할 수 없습니까? –

+0

가능합니다. 이미지를 blob 속성에 넣을 수는 있지만 이미지의 크기는 엔티티 크기에 의해 제한됩니다. 이미지를 데이터 저장소에 저장하면 이미지를 직접 제공해야합니다. – voscausa

+0

아래 예를 참조하십시오. – voscausa

0

는 이미지의 크기와 워크 플로우를 기반으로 사이에서 결정 수있는 몇 가지 옵션이 있습니다.

  1. 이미지가 고정되어 개발 팀에서만 변경된 경우 정적 파일로 포함 할 수 있습니다.
  2. the blobstore api을 사용하면 이미지를 업로드하고 볼 수 있습니다.
  3. 업로드 /보기에도 Google Cloud Storage libraries을 사용할 수 있습니다. 나는 구글이 블롭 스토어보다는이쪽으로 움직이고 있다는 인상을 받는다.
+0

로컬 컴퓨터의 이미지를 데이터 저장소에 추가 할 수 없습니까? –

관련 문제