[英]how to store an image on Google Cloud Storage sent by Android using Python
我要執行以下操作:
AppEngine需要生成一個我可以用來存儲圖像的URI(我的猜測)
curl -i http://localhost:8082/upload_form
執行的代碼是
class PhotoUploadFormHandler(webapp2.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload_photo')
# The method must be "POST" and enctype must be set to "multipart/form-data".
self.response.out.write('***' + upload_url + '***')
application = webapp2.WSGIApplication([('/upload_form', PhotoUploadFormHandler),], debug=True)
輸出如下:
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
cache-control: no-cache
Content-Length: 115
Server: Development/2.0
Date: Fri, 06 Jun 2014 08:52:17 GMT
***http://localhost:8082/_ah/upload/ahNkZXZ-ZXZlbnRzcHVsc2UtZGV2ciILEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YgICAgIDYtQoM***
然后,我使用新網址執行cURL:
curl -v -H 'Content-Type: multipart/form-data' -F "file=@./photofeed.png" http://localhost:8082/_ah/upload/ahNkZXZ-ZXZlbnRzcHVsc2UtZGV2ciILEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YgICAgIDovQoM
這是命令的輸出:
* Adding handle: conn: 0x7fcb65021000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fcb65021000) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8082 (#0)
* Trying ::1...
* Connected to localhost (::1) port 8082 (#0)
> POST /_ah/upload/ahNkZXZ-ZXZlbnRzcHVsc2UtZGV2ciILEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YgICAgIDovQoM HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8082
> Accept: */*
> Content-Length: 2999
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=----------------------------76b075a1768d
>
< HTTP/1.1 100 Continue
< HTTP/1.1 404 Not Found
< Content-Length: 254
< Content-Type: text/html; charset=UTF-8
* Server Development/2.0 is not blacklisted
< Server: Development/2.0
< Date: Fri, 06 Jun 2014 08:54:38 GMT
* HTTP error before end of send, stop sending
<
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
The resource could not be found.<br /><br />
No such upload session: ahNkZXZ-ZXZlbnRzcHVsc2UtZGV2ciILEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YgICAgIDovQoM
</body>
* Closing connection 0
我在這里找到了部分答案: 在Google Cloud Storage中使用Blobstore API
我需要將圖像專門創建到Google Cloud Storage中,並且已經將CreateFile方法更改為CreateImage方法
def CreateImage(imageName, imageData):
"""Create a GCS file with GCS client lib.
Args:
filename: GCS filename.
Returns:
The corresponding string blobkey for this GCS file.
"""
# Create a GCS file with GCS client.
with gcs.open(imageName, 'w') as f:
f.write(imageData)
# Blobstore API requires extra /gs to distinguish against blobstore files.
blobstore_filename = '/gs' + imageName
# This blob_key works with blobstore APIs that do not expect a
# corresponding BlobInfo in datastore.
return blobstore.create_gs_key(blobstore_filename)
上載網址可能已過期? 只能使用幾分鍾。
另請參閱: 谷歌應用引擎; 上載到blobstore會顯示404錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.