烧瓶Boto3`ValueError:在访问资源时未设置必需的参数名称' [英] Flask & Boto3 `ValueError: Required parameter name not set` on Accessing Resource

查看:79
本文介绍了烧瓶Boto3`ValueError:在访问资源时未设置必需的参数名称'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次运行我的应用程序时,它一直有效,直到我向/files路由发送请求,并在其中获得ValueError: Required parameter name not set为止.该错误未指定未设置什么parameter.

Every time I run my app, it works until I send a request to the /files route, where I get a ValueError: Required parameter name not set. The error does not specify what parameter is not set.

from flask import (
    Flask, render_template, redirect, 
    url_for, g, session, flash, request
)
from flask_session import Session
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from flask_wtf.file import FileField
from datetime import datetime
from wtforms import StringField, PasswordField, BooleanField, DateTimeField, TextField
from wtforms.validators import InputRequired, Email, Length
from flask_sqlalchemy  import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import (LoginManager, UserMixin, login_user,
                         login_required, logout_user, current_user)
from werkzeug.utils import secure_filename
from flask_s3 import FlaskS3
import boto3
from config import S3_BUCKET, S3_KEY, S3_SECRET

s3 = boto3.client(
    "s3",
    aws_access_key_id=S3_KEY,
    aws_secret_access_key=S3_SECRET
)

app = Flask(__name__)
app.config['FLASKS3_BUCKET_NAME'] = 'flaskprofileproject'
app.config['SECRET_KEY'] = "ASNDASNDASONDSAOIDMAODNAS"
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////Users/michaelaronian/Desktop/FlaskProject/database.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
login_manager = LoginManager(app)
login_manager.init_app(app)
login_manager.login_view = 'login'

# Code removed for brevity's sake

@app.route('/files')
def files():
    s3_resource = boto3.resource('s3')
    my_bucket = s3_resource.Bucket(S3_BUCKET)
    summaries = my_bucket.objects.all()

    return render_template('files.html', my_bucket=my_bucket, files=summaries)

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=4100)

我的其余应用程序运行正常.谢谢您的帮助.

The rest of my application runs fine. Thank you for your help.

推荐答案

Boto3允许3种方法来设置凭据,已记录在此处.

Boto3 allows 3 ways to set credentials, documented here.

您似乎在这里使用上面链接的第3种方法:方法参数:

It looks like you are using the 3rd method, linked above, of Method Parameters here:

s3 = boto3.client(
    "s3",
    aws_access_key_id=S3_KEY,
    aws_secret_access_key=S3_SECRET
)

问题是,您没有使用s3变量(存储boto3客户端)来访问资源.此方法创建一个低级客户端",用于非常特定地访问S3资源.因此,如果您打算这样做,请Client此处.

The problem is, you aren't using the s3 variable (storing the boto3 client) to access your resource. This method creates a "low-level client", meant for very specific access to your S3 resources. So if that is your intention, read the docs on the Client class here.

否则,您可以从环境变量Boto >就像这里的这种方法一样,然后像上面一样继续访问您的资源.

Otherwise, you can have Boto read from environment variables, like in this method here, and then go about accessing your resource as you are doing above.

您必须在本地的~/.bash_profile中设置以下环境变量,因此boto3知道如何连接到您的AWS S3存储桶.在您的~/.bash_profile中,添加:

You'll have to set the following environment variables, likely in your ~/.bash_profile on localhost, so boto3 knows how to connect to your AWS S3 Bucket. In your ~/.bash_profile, add:

export AWS_ACCESS_KEY_ID="The access key for your AWS account."
export AWS_SECRET_ACCESS_KEY="The secret key for your AWS account."
export AWS_SESSION_TOKEN="The session key for your AWS account."
# This is only needed when you are using temporary credentials, so you can probably ignore it!

在编辑此文件后,将其保存,然后运行source ~/.bash_profile将新的env变量导出到您的环境中(在与启动服务器相同的外壳中,),然后启动您的服务器.

After editing this file, save it, and run a source ~/.bash_profile to export your new env variables into your environment (in the same shell that you're starting your server in), and start your server.

这篇关于烧瓶Boto3`ValueError:在访问资源时未设置必需的参数名称'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆