Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask & Boto3 `ValueError: Required parameter name not set` on Accessing Resource

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.

like image 771
Mike Weston Avatar asked Aug 06 '18 22:08

Mike Weston


People also ask

What is Flask is used for?

Flask is used for developing web applications using python, implemented on Werkzeug and Jinja2. Advantages of using Flask framework are: There is a built-in development server and a fast debugger provided.

What is Flask vs Django?

Flask is a Python web framework built for rapid development, whereas Django is built for easy and simple projects. Flask offers a diversified working style, while Django offers a Monolithic working style.

Is Flask a frontend or backend?

Flask is a back-end framework, which means that it provides the technologies, tools, and modules that can be used to build the actual functionalities of the web app rather than the design or look of it. Flask is considered one of the easiest frameworks to learn for beginners.

What is called Flask?

A flask is a bottle which you use for carrying drinks around with you. He took out a metal flask from a canvas bag. Synonyms: vessel, bottle, container, Thermos flask [trademark] More Synonyms of flask. A flask of liquid is the flask and the liquid which it contains.


1 Answers

Boto3 allows 3 ways to set credentials, documented here.

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
)

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.

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.

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!

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.

like image 69
The Aelfinn Avatar answered Oct 17 '22 07:10

The Aelfinn