1 回答
TA贡献1856条经验 获得超11个赞
除非您打算提供自定义 SQL 函数,否则您将希望避免使用.expression 修饰符并继续使用实例级方法。
@hybrid_property
def create_presigned_url(self):
"""Generate a presigned URL to share an S3 object
return: Presigned URL as string. If error, returns None.
"""
bucket_name = 'myBucket'
object_name = 'pdfs/{}'.format(self.file_name)
expiration = 3600
# Generate a presigned URL for the S3 object
try:
response = bucket_resource.generate_presigned_url(
"get_object",
Params={"Bucket": bucket_name, "Key": object_name},
ExpiresIn=expiration,
)
except Exception as e:
return None
# The response contains the presigned URL
return response
要使用它,您需要在实例级别而不是类级别进行操作。所以你的查询是针对实例的:
instance = db.session.query(EnrollmentFormData).filter(EnrollmentFormData.id==1).first()
print(instance.create_presigned_url)
#or
instance = db.session.query(EnrollmentFormData).get(1)
print(instance.create_presigned_url)
#or
print([x.create_presigned_url for x in db.session.query(EnrollmentFormData).all()]
添加回答
举报