示例#1
0
def with_preview_url(bank_member: BankMember) -> PreviewableBankMember:
    previewable = PreviewableBankMember(**asdict(bank_member))

    if bank_member.storage_bucket is None:
        return previewable

    previewable.preview_url = create_presigned_url(
        bucket_name=bank_member.storage_bucket,
        key=bank_member.storage_key,
        file_type=None,
        expiration=300,
        client_method="get_object",
    )
    return previewable
示例#2
0
    def get_preview_url(content_id, content_object) -> str:
        """
        Given a content_id and a content_object, returns a URL you can use to
        preview it.
        """
        content_object = t.cast(ContentObject, content_object)

        if content_object.content_ref_type == ContentRefType.DEFAULT_S3_BUCKET:
            source = S3BucketContentSource(image_bucket, image_prefix)

            preview_url = create_presigned_url(image_bucket,
                                               source.get_s3_key(content_id),
                                               None, 3600, "get_object")
        elif content_object.content_ref_type == ContentRefType.URL:
            preview_url = content_object.content_ref
        return preview_url
示例#3
0
def submit_content_request_from_s3_event_record(
    record: dict,
    dynamodb_table: Table,
    submissions_queue_url: str,
):
    """
    Converts s3 event into a ContentObject and url_submission_message using helpers
    from submit.py

    For partner bucket uploads, the content IDs are unique and (somewhat) readable but
    not reversable
      * uniqueness is provided by uuid4 which has a collision rate of 2^-36
      * readability is provided by including part of the key in the content id
      * modifications to the key mean that the original content bucket and key are
        not derivable from the content ID alone

    The original content (bucket and key) is stored in the reference url which is passed
    to the webhook via additional_fields

    Q: Why not include full key and bucket in content_id?
    A: Bucket keys often have "/" which dont work well with ContentDetails UI page
    """
    bucket: str = record["bucket"]["name"]
    key: str = record["object"]["key"]

    readable_key = key.split("/")[-1].replace("?", ".").replace("&", ".")
    content_id = f"{uuid4()}-{readable_key}"

    presigned_url = create_presigned_url(bucket, key, None, 3600, "get_object")
    reference_url = f"https://{bucket}.s3.amazonaws.com/{key}"

    record_content_submission(
        dynamodb_table,
        content_id,
        PhotoContent,
        content_ref=presigned_url,
        content_ref_type=ContentRefType.URL,
        additional_fields={f"partner_s3_reference_url:{reference_url}"},
    )
    send_submission_to_url_queue(dynamodb_table, submissions_queue_url,
                                 content_id, PhotoContent, presigned_url)
def add_bank_member(
    banks_table: BanksTable,
    sqs_client: SQSClient,
    submissions_queue_url: str,
    bank_id: str,
    content_type: t.Type[ContentType],
    storage_bucket: t.Optional[str],
    storage_key: t.Optional[str],
    raw_content: t.Optional[str],
    notes: str,
    bank_member_tags: t.Set[str],
) -> BankMember:
    """
    Write bank-member to database. Send a message to hashing lambda to extract signals.
    """
    member = banks_table.add_bank_member(
        bank_id=bank_id,
        content_type=content_type,
        storage_bucket=storage_bucket,
        storage_key=storage_key,
        raw_content=raw_content,
        notes=notes,
        bank_member_tags=bank_member_tags,
    )

    submission_message = BankSubmissionMessage(
        content_type=content_type,
        url=create_presigned_url(storage_bucket, storage_key, None, 3600,
                                 "get_object"),
        bank_id=bank_id,
        bank_member_id=member.bank_member_id,
    )
    sqs_client.send_message(
        QueueUrl=submissions_queue_url,
        MessageBody=json.dumps(submission_message.to_sqs_message()),
    )

    return member