title: Sqlalchemy- Create Amazon S3 Presigned URL
body: <p>I am trying to create an S3 presigned URL so that people can have temporary access to the files stored in a S3 bucket.</p>
  <p>I have a function like this to create the url:</p>
  <pre><code>def create_presigned_url(file_name):
          &quot;&quot;&quot;Generate a presigned URL to share an S3 object
              return: Presigned URL as string. If error, returns None.
          &quot;&quot;&quot;
          
          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(
                  &quot;get_object&quot;,
                  Params={&quot;Bucket&quot;: bucket_name, &quot;Key&quot;: object_name},
                  ExpiresIn=expiration,
              )
          except Exception as e:
              return None
      
          # The response contains the presigned URL
          return response
  </code></pre>
  <p>Now I want to apply this url to the table EnrollmentFormData which is defined below:</p>
  <pre><code>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(&quot;America/New_York&quot;)))
  </code></pre>
  <p>I have tried two things:</p>
  <p>First, I tried to pass the column name as a param of the function:</p>
  <pre><code> query = db.session.query(
          EnrollmentFormData.id,
          create_presigned_url(EnrollmentFormData.file_name),
          EnrollmentFormData.timestamp
      ).all()
  </code></pre>
  <p>This didn't work maybe because the function is unable to work with a SqlAlchemy type.</p>
  <p>Then I tried with the <code>@hybrid_property</code></p>
  <pre><code>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(&quot;America/New_York&quot;)))
      
      
      @hybrid_property
      def create_presigned_url(self):
          &quot;&quot;&quot;Generate a presigned URL to share an S3 object
              return: Presigned URL as string. If error, returns None.
          &quot;&quot;&quot;
          
          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(
                  &quot;get_object&quot;,
                  Params={&quot;Bucket&quot;: bucket_name, &quot;Key&quot;: object_name},
                  ExpiresIn=expiration,
              )
          except Exception as e:
              return None
      
          # The response contains the presigned URL
          return response
      
      @create_presigned_url.expression
      def create_presigned_url(self):
          &quot;&quot;&quot;Generate a presigned URL to share an S3 object
              return: Presigned URL as string. If error, returns None.
          &quot;&quot;&quot;
          
          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(
                  &quot;get_object&quot;,
                  Params={&quot;Bucket&quot;: bucket_name, &quot;Key&quot;: object_name},
                  ExpiresIn=expiration,
              )
          except Exception as e:
              print(e)
              return None
      
          # The response contains the presigned URL
          return response
  
  #query
  
  query = db.session.query(
          EnrollmentFormData.id,
          EnrollmentFormData.create_presigned_url,
          EnrollmentFormData.timestamp,
      )
  </code></pre>
  <p>The above doesn't work either</p>
  <p>The error both gives is:</p>
  <pre><code>SQL expression, column, or mapped entity expected - got ''
  </code></pre>
  <p>Sorry can't paste the whole thing because I don't want to give away access keys.</p>
  <p>Can anyone help how to get it done?</p>