양식을 사용하지 않고 Google 애플리케이션의 BLOBstore에 파일을 업로드하려고합니다. 하지만 앱에서 내 로컬 파일을 읽는 방법을 고민하고 있습니다. 나는 파이썬과 Google 애플리케이션에 아주 새로운 해요하지만 일부 잘라 내기 및 붙여 넣기 후 나는이와 결국했습니다Google App (Python 포함)에서 로컬 파일 읽기
import webapp2
import urllib
import os
from google.appengine.api import files
from poster.encode import multipart_encode
class Upload(webapp2.RequestHandler):
def get(self):
# Create the file in blobstore
file_name = files.blobstore.create(mime_type='application/octet-stream')
# Get the local file path from an url param
file_path = self.request.get('file')
# Read the file
file = open(file_path, "rb")
datagen, headers = multipart_encode({"file": file})
data = str().join(datagen) # this is supposedly memory intense and slow
# Open the blobstore file and write to it
with files.open(file_name, 'a') as f:
f.write(data)
# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
문제는 지금은 정말 로컬 파일
잡아하는 방법을 몰라
양식이없는 이유는 무엇입니까? 어떻게 사용자가 파일을 선택하고 파일 입력없이 업로드 할 것으로 예상합니까? –
파일을 저장 한 다음 업로드해야하는 독립 실행 형 프로그램을 만들고 있습니다. – Marb
로컬 파일이 파이썬 코드로 업로드 한 파일을 제출하려고하거나 파일을 제출하려고한다고 말하면 로컬 컴퓨터에 있습니까? GAE가 로컬 파일을 보안 프로토콜로 액세스 할 수 없기 때문에 전자가 가능하지 않습니다. 전적으로 나는 당신이 업로드 필드와 웹 페이지 양식이 필요하다고 말할 것입니다. – gabeio