亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Sqlalchemy - 創建 Amazon S3 預簽名 URL

Sqlalchemy - 創建 Amazon S3 預簽名 URL

喵喵時光機 2023-06-13 15:26:30
我正在嘗試創建一個 S3 預簽名 URL,以便人們可以臨時訪問存儲在 S3 存儲桶中的文件。我有一個這樣的函數來創建 url:def create_presigned_url(file_name):        """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(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現在我想將此 url 應用于下面定義的表 EnrollmentFormData:class EnrollmentFormData(db.Model):    __tablename__ = 'enrollment_form_data'    id = db.Column(db.Integer, primary_key=True)    full_name = db.Column(db.String(300), nullable=False)    employee_number = db.Column(db.String(12),nullable=False)    department_code = db.Column(db.String(12), nullable=False)    sign_image = db.Column(db.BLOB, nullable=False)    aws_path = db.Column(db.String(2083), nullable=False)    file_name = db.Column(db.String(2083), nullable=False)    timestamp = db.Column(db.DateTime, nullable=False, default=lambda:     datetime.datetime.now(pytz.timezone("America/New_York")))我嘗試了兩件事:首先,我嘗試將列名作為函數的參數傳遞: query = db.session.query(        EnrollmentFormData.id,        create_presigned_url(EnrollmentFormData.file_name),        EnrollmentFormData.timestamp    ).all()這可能不起作用,因為該函數無法使用 SqlAlchemy 類型。以上也不行兩者都給出的錯誤是:SQL expression, column, or mapped entity expected - got ''抱歉無法粘貼全部內容,因為我不想泄露訪問密鑰。任何人都可以幫助完成它嗎?
查看完整描述

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()]


查看完整回答
反對 回復 2023-06-13
  • 1 回答
  • 0 關注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號