【问题标题】:GAE Python bucket upload not workingGAE Python存储桶上传不起作用
【发布时间】:2018-05-29 20:50:53
【问题描述】:

我正在尝试将文件上传到我的谷歌云存储桶。但它没有出现,虽然它在一段时间前工作过,所以我知道存储桶的位置是正确的。但它是用 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)

【问题讨论】:

    标签: python google-app-engine flask google-cloud-datastore


    【解决方案1】:

    代码中的错误。主要的:

    你拨打upload_image_file(file, post.id)

    然而,upload_image_file() 函数需要 3 个参数:

    def upload_image_file(filename, filecontent, post_id):

    你的日志是怎么说的?

    【讨论】:

      猜你喜欢
      • 2018-01-06
      • 2018-06-05
      • 2019-03-26
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      相关资源
      最近更新 更多