我在使用默认存储桶在新创建的应用引擎应用中将图片上传到Google云端存储时遇到问题。
我使用的上传代码在部署到已经有效默认存储桶的旧应用引擎应用时效果非常好。当我创建一个新的应用引擎项目或激活appengine.google.com上的默认gcs存储桶时,我得到一个奇怪的500(503)错误。
我的上传代码使用cloudstorage客户端,如下所示:
def save_to_gcs(file_obj, obj):
# pass a file and a datastore entity to associate the file to
serving_url = ''#just assign it adn reassign later
time_stamp = int(time.time())
app_id = app_identity.get_application_id()
logging.error("got app_id .... %s" % app_id)
fname = '/%s.appspot.com/post_%s_%s.jpg' % (app_id, obj.key.id(), time_stamp)
# logging.error(file_obj)
logging.error("fname .... %s" % fname)
gcs_file = gcs.open(fname, 'w', content_type="image/jpeg")
gcs_file.write(file_obj)
logging.error("written file obj ....")# <-- this is where the logs stop
gcs_file.close()
gcs_filename = "/gs%s" % fname
serving_url = images.get_serving_url(blobstore.create_gs_key(gcs_filename))
logging.error("got serving url ....")
media_obj = save_gcs_to_media(gcs_filename, serving_url, obj)
logging.error("saved media obj ....")
return media_obj
我认为错误与file.close()方法有关,可能与默认存储桶有关。
我已经过正确的身份验证,我上传的文件大约是100kb,所以没什么大的。
任何帮助都会非常感激(事实上它会阻止我的头发先变灰,然后掉下来)。
很抱歉这个烂摊子,但这是日志:
E 11:05:54.412 - - - - COVER - - - -
E 11:05:54.413 True
E 11:05:54.413 got time stamp
E 11:05:54.413 got time file name
E 11:05:54.413 got app_id .... emile-test
E 11:05:54.413 fname .... /emile-test.appspot.com/post_5629499534213120_1436432754.jpg
E 11:05:54.696 written file obj ....
D 11:05:55.025 Tasklet is <bound method _StorageApi.urlfetch_async of <cloudstorage.storage_api._StorageApi object at 0xfbd7bf10>>
D 11:05:55.025 Got result <google.appengine.api.urlfetch._URLFetchResult object at 0xfbd9a810> from tasklet.
D 11:05:55.025 Retry in 0.1 seconds.
D 11:05:55.477 Got result <google.appengine.api.urlfetch._URLFetchResult object at 0xfbd9a9b0> from tasklet.
D 11:05:55.478 Retry in 0.2 seconds.
D 11:05:56.152 Got result <google.appengine.api.urlfetch._URLFetchResult object at 0xfbd9ab30> from tasklet.
D 11:05:56.153 Retry in 0.4 seconds.
D 11:05:57.244 Got result <google.appengine.api.urlfetch._URLFetchResult object at 0xfbd9acb0> from tasklet.
D 11:05:57.245 Retry in 0.8 seconds.
D 11:05:58.435 Got result <google.appengine.api.urlfetch._URLFetchResult object at 0xfbd7b690> from tasklet.
D 11:05:58.435 Retry in 1.6 seconds.
D 11:06:00.542 Got result <google.appengine.api.urlfetch._URLFetchResult object at 0xfe4ce630> from tasklet.
D 11:06:00.542 Retry in 3.2 seconds.
D 11:06:04.179 Tasklet failed after 7 attempts and 9.48189997673 seconds in total
**E 11:06:04.180 Expect status [200] from Google Storage. But got status 503.**
Path: '/emile-test.appspot.com/post_5629499534213120_1436432754.jpg'.
Request headers: {'content-range': 'bytes 0-108067/108068', 'x-goog-api-version': '2', 'accept-encoding': 'gzip, *'}.
Response headers: {'content-length': '0', 'x-guploader-uploadid': 'AEnB2UqzyZwtoNSy8sTEUq7a8fai-A5aDVlMccCj1UVEo72NAUUw4kyDotehcf8RDusUzm52092rdO0CR9JxIC9ba1J5Kj1xuw'}.
Body: ''.
Extra info: {'upload_path': '/emile-test.appspot.com/post_5629499534213120_1436432754.jpg?upload_id=AEnB2UqzyZwtoNSy8sTEUq7a8fai-A5aDVlMccCj1UVEo72NAUUw4kyDotehcf8RDusUzm52092rdO0CR9JxIC9ba1J5Kj1xuw'}.
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~emile-test/1.385589451941224330/main.py", line 757, in post
media_obj = utils.save_to_gcs(image, post)
File "/base/data/home/apps/s~emile-test/1.385589451941224330/utils.py", line 131, in save_to_gcs
gcs_file.close()
**File "/base/data/home/apps/s~emile-test/1.385589451941224330/cloudstorage/storage_api.py", line 750, in close**
self._flush(finish=True)
File "/base/data/home/apps/s~emile-test/1.385589451941224330/cloudstorage/storage_api.py", line 804, in _flush
self._send_data(data, self._written, file_len)
File "/base/data/home/apps/s~emile-test/1.385589451941224330/cloudstorage/storage_api.py", line 837, in _send_data
{'upload_path': self._path_with_token})
File "/base/data/home/apps/s~emile-test/1.385589451941224330/cloudstorage/errors.py", line 141, in check_status
raise ServerError(msg)
ServerError: Expect status [200] from Google Storage. But got status 503.
Path: '/emile-test.appspot.com/post_5629499534213120_1436432754.jpg'.
Request headers: {'content-range': 'bytes 0-108067/108068', 'x-goog-api-version': '2', 'accept-encoding': 'gzip, *'}.
Response headers: {'content-length': '0', 'x-guploader-uploadid': 'AEnB2UqzyZwtoNSy8sTEUq7a8fai-A5aDVlMccCj1UVEo72NAUUw4kyDotehcf8RDusUzm52092rdO0CR9JxIC9ba1J5Kj1xuw'}.
Body: ''.
Extra info: {'upload_path': '/emile-test.appspot.com/post_5629499534213120_1436432754.jpg?upload_id=AEnB2UqzyZwtoNSy8sTEUq7a8fai-A5aDVlMccCj1UVEo72NAUUw4kyDotehcf8RDusUzm52092rdO0CR9JxIC9ba1J5Kj1xuw'}.
答案 0 :(得分:1)
我今天在我自己的GAE项目中确切地解决了这个问题(相同的堆栈跟踪)(我将从blobstore中的Files API迁移到cloudstorage)。我采取的步骤是:
转到Google Developers Console
https://console.developers.google.com/project/YOUR_APP_ID/storage/browser
其中YOUR_APP_ID是没有.appspot.com后缀的应用名称,例如&#39; foo&#39;,而不是&#39; foo.appspot.com&#39;,< / p>
点击项目右侧的三个垂直点
选择的存储桶权限
我的电子邮件地址已添加为Entity = User,name = teen.com @ gmail.com,Access = Owner。
我不是百分之百确定为什么这可以解决这个问题,但它完成了这个伎俩。