我正在构建一个使用zappa部署到AWS Lambda的烧瓶应用程序,并且我尝试使用Flask-s3来处理静态文件。我之前从未使用过[Flask-S3] [1]它看起来相当简单,但我得到......
AttributeError: 'FlaskS3' object has no attribute 'url_for'
他们我理解你只需要用url_for替换你的静态网址就像这样:
app.config['FLASKS3_BUCKET_NAME'] = 'mybucketname'
s3 = FlaskS3(app)
s3.url_for('static/file.jpg')
这不是很清楚,我做错了什么,但几乎没有任何在线故障排除Flask-s3。一切都有帮助。
app = Flask(__name__)
Bootstrap(app)
app.config['FLASKS3_BUCKET_NAME'] = 'mybucketname'
s3 = FlaskS3(app)
MAILGUN_API_KEY = 'key'
auth = ('api', MAILGUN_API_KEY)
@app.route("/", methods=['GET', 'POST'])
def index():
context = {
'image': url_for('static/property_home.jpeg'),
'heading': 'We sell property - At a discount!',
'landing_video': url_for('static/intro.mp4')
}
form = forms.OptIn()
if form.validate_on_submit():
validate = requests.get(
"https://api.mailgun.net/v3/address/private/validate",
auth=auth,
params={"address": form.email.data})
if validate.json()['did_you_mean'] is not None:
flash('Did you mean {}?'.format(validate.json()['did_you_mean']))
elif validate.json()['is_valid'] == True and validate.json()['is_role_address'] == False and validate.json()['is_disposable_address'] == False:
r = requests.post(
"https://api.mailgun.net/v3/lists/YOUR_DOMAIN_NAME/members",
auth=auth,
data={'subscribed': True,
'address': form.email.data})
if r.status_code == 200:
requests.post('https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages',
auth=auth,
data={"from": 'Matt@SpokaneDiscountProperties.com',
"to": form.email.data,
"subject": "Welcome to Spokane Discount Properties",
"html": open('templates/indoc.html'),
"o:tag": 'indoctrinated'})
flash('Thanks, we will notify you when we have more properties')
else:
flash('You are already subscribed, we will notify you when more properties are available')
else:
flash('Holy guacamole! Best check yo self, this is not a valid email.')
return render_template('index.html', form=form, context=context)
答案 0 :(得分:0)
来自doc here
使您的应用程序使用外部Amazon S3 URL 在提到你的应用程序的静态资产时,通过你的Flask 只需要完成FlaskS3对象的对象。
该扩展程序负责为您处理url_for
。所以你可能不需要直接调用它。
在内部,每次在您的某个应用程序中调用url_for 模板,而是调用flask_s3.url_for。如果是端点 提供被认为是指静态资产,然后是S3的URL 而是返回filename参数中指定的资产。 否则,flask_s3.url_for将调用传递给flask.url_for。
改变这个:
context = {
'image': url_for('static/property_home.jpeg'),
'heading': 'We sell property - At a discount!',
'landing_video': url_for('static/intro.mp4')
}
为:
context = {
'image': url_for('static', filename= 'property_home.jpeg'),
'heading': 'We sell property - At a discount!',
'landing_video': url_for('static', filename='intro.mp4')
}