我正在尝试将文件(在本例中为音频/mp3)保存到 App Engine blobstore,但成功率参差不齐。一切似乎都正常,一个文件保存在 blobstore 中,类型正确,但它基本上是空的(1.5kB 与预期的 6.5kB),因此无法播放。有问题的网址是http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=revenues+in+new+york+were+56+million
应用引擎日志没有显示任何异常 - 所有部分都按预期执行......任何指针将不胜感激!
class Dictation(webapp2.RequestHandler):
def post(self):
sentence = self.request.get('words')
# Google Translate API cannot handle strings > 100 characters
sentence = sentence[:100]
# Replace the non-alphanumeric characters
# The spaces in the sentence are replaced with the Plus symbol
sentence = urllib.urlencode({'q': sentence})
# Name of the MP3 file generated using the MD5 hash
mp3_file = hashlib.md5(sentence).hexdigest()
# Save the MP3 file in this folder with the .mp3 extension
mp3_file = mp3_file + ".mp3"
# Create the full URL
url = 'http://translate.google.com/translate_tts?ie=UTF-8&tl=en&' + sentence
# upload to blobstore
mp3_file = files.blobstore.create(mime_type = 'audio/mp3', _blobinfo_uploaded_filename = mp3_file)
mp3 = urllib.urlopen(url).read()
with files.open(mp3_file, 'a') as f:
f.write(mp3)
files.finalize(mp3_file)
blob_key = files.blobstore.get_blob_key(mp3_file)
logging.info('blob_key identified as %s', blob_key)