python - GAE Python存储桶上传不起作用
问题描述
我正在尝试将文件上传到我的谷歌云存储桶。但它没有出现,虽然它在一段时间前工作过,所以我知道存储桶的位置是正确的。但它是用 0b 保存它或根本没有它。我将如何解决这个问题?
路线
@app.route('/admin/create/post/<int:id>', methods=['GET', 'POST'])
def create_post(id=None):
"""Display form for creating a post."""
form = forms.Post()
if form.validate_on_submit():
models.Post.create(project_id=id,
name=form.name.data,
content=form.content.data)
post = (models.Post.select()
.where(
(models.Post.name == form.name.data),
(models.Post.content == form.content.data)).get())
for filename in request.files:
file = request.files.get(str(filename))
upload_image_file(file, post.id)
return redirect(url_for('admin'))
return render_template('create_post.html', form=form)
上传图片文件功能
def upload_image_file(filename, filecontent, post_id):
"""Save file."""
logging.debug("""upload_image_file, name={},content={},postid={}"""
.format(filename, filecontent, post_id))
path = 'post/' + '{}/'.format(post_id)
path = path.lower()
filepath = (path + str(filename)).lower()
client = _get_storage_client()
logging.debug('select storage')
bucket = client.bucket(app.config['CLOUD_STORAGE_BUCKET'])
logging.debug('select bucket')
blob = bucket.blob(filepath)
print('file read = {}'.format(filecontent))
# blob.upload_from_string(file.read())
blob.upload_from_string(
'test.txt',
content_type=filecontent)
url = blob.public_url
# try:
# url = url.decode('utf-8')
# logging.debug('decoded url = {}'.format(url))
# except Exception:
# logging.debug('Did not decode = {} | url'.format(url))
models.Media.create(
post_id=post_id,
media=url)
解决方案
您的代码中的错误。主要的一个:
你打电话upload_image_file(file, post.id)
然而,该upload_image_file()
函数需要 3 个参数:
def upload_image_file(filename, filecontent, post_id):
你的日志说什么?
推荐阅读
- haskell - 一个 Haskell 函数,它接收不同类型的 List,只返回一个 Integer List
- c# - UITableViewCell not being rendered correctly
- python - 如何计算两个指数数之差
- visual-studio - 无法在 Visual Studio 2012 中分配代码审阅者
- markdown - Slack 是否支持 Markdown 表?
- c# - 如何限制从 C# 程序执行的并行进程数?
- ios - Swift JSONDecoder - 编码键不使用下划线
- bing-api - Bing API:如何获取请求的报告列的数据类型?
- laravel - Displaying time in minutes/hours/days using Carbon or any other tools
- com - 有人可以向我展示在 C++ 应用程序中使用简单类型库的完整示例吗?