상세내용은 아래의 링크를 참고하면 된다.
Google App Engine Blob Page (Python)
여기에서 제공되는 sample code의 일부를 아래에 발췌했다.
class MainHandler(webapp2.RequestHandler): def get(self): upload_url = blobstore.create_upload_url('/upload') self.response.out.write('<html><body>') self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url) self.response.out.write("""Upload File: <input type="file" name="file"> <br> <input type="submit" name="submit" value="Submit"> </form></body></html>""") class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): def post(self): upload_files = self.get_uploads('file') # 'file' is file upload field in the form blob_info = upload_files[0] self.redirect('/serve/%s' % blob_info.key())HTTP POST request를 통해서 선택한 file을 blob store에 저장하는 코드이다. 여기서 주의깊게 봐야 할 부분은 데이터가 저장되는 공간에 대한 url을 upload_url = blobstore.create_upload_url('/upload')를 통해 dynamic 하게 생성한다는 것이다. 한번 생성된 url은 10분 동안 유효하다. 즉, 10분 동안은 이 url을 blob store에 file을 저장하기위해 어디서든 사용할 수 있다는 뜻이다. upload_files = self.get_uploads('file')에서는 파일을 실제로 저장한다. blob_info.key()는 저장된 방금 저장된 file을 access 하기 위한 정보를 담고 있다.
위의 코드가 실행된 web page에서 소스보기를 하면 upload_url로 어떤 값이 설정됐는지 확인 할 수 있다.
다음과 같은 형태를 볼 수 있는데, action="http://xxx.appspot.com/_ah/upload/AMmfu6ZqsMW_ ... cs32MZ/" 여기서 " " 안의 값이 upload_url이다.
이 값을 사용해서 android에서 text file을 upload해 보는 코드를 아래와 같이 작성해 보았다.
static void fileup() { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(upload_url); try { MultipartEntity entity = new MultipartEntity(); entity.addPart("file", new FileBody(new File(fileNameToSend, "text/plain")); httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { } catch (IOException e) { } }코드의 일부는 http://hc.apache.org/ 에서 제공하는 http library를 사용하였다.
upload된 파일은 Google App Engine의 Blob Viewer메뉴에서 확인할 수 있다.
Android에서 GAE의 blobstore에 저장이 잘 되는것은 확인 했지만, upload url을 android상에서 바로 생성할 수 없기 때문에 조금 다른 방식이 필요하다.
To be continued...