<gh_stars>1-10
from botocore.exceptions import ClientError
from django.conf import settings
import boto3
import logging

def create_presigned_url(object_name, expiration=3600):
    """Generate a presigned URL to share an S3 object

    :param bucket_name: string
    :param object_name: string
    :param expiration: Time in seconds for the presigned URL to remain valid
    :return: Presigned URL as string. If error, returns None.
    """

    # Generate a presigned URL for the S3 object
    s3_client = boto3.client("s3")
    try:
        response = s3_client.generate_presigned_url(
            "get_object",
            Params={"Bucket": settings.AWS_STORAGE_BUCKET, "Key": object_name},
            ExpiresIn=expiration,
        )
    except ClientError as e:
        logging.error(e)
        return None

    # The response contains the presigned URL
    return response


def create_presigned_post(object_name, fields=None, conditions=None, expiration=3600):
    """Generate a presigned URL S3 POST request to upload a file

    :param bucket_name: string
    :param object_name: string
    :param fields: Dictionary of prefilled form fields
    :param conditions: List of conditions to include in the policy
    :param expiration: Time in seconds for the presigned URL to remain valid
    :return: Dictionary with the following keys:
        url: URL to post to
        fields: Dictionary of form fields and values to submit with the POST
    :return: None if error.
    """

    # Generate a presigned S3 POST URL
    s3_client = boto3.client("s3")
    try:
        response = s3_client.generate_presigned_post(
            settings.AWS_STORAGE_BUCKET, object_name, ExpiresIn=expiration
        )
    except ClientError as e:
        logging.error(e)
        return None

    return response

from botocore.exceptions import ClientError
from django.conf import settings
import boto3
import logging

def create_presigned_url(object_name, expiration=3600):
    """Generate a presigned URL to share an S3 object

    :param bucket_name: string
    :param object_name: string
    :param expiration: Time in seconds for the presigned URL to remain valid
    :return: Presigned URL as string. If error, returns None.
    """

    # Generate a presigned URL for the S3 object
    s3_client = boto3.client("s3")
    try:
        response = s3_client.generate_presigned_url(
            "get_object",
            Params={"Bucket": settings.AWS_STORAGE_BUCKET, "Key": object_name},
            ExpiresIn=expiration,
        )
    except ClientError as e:
        logging.error(e)
        return None

    # The response contains the presigned URL
    return response


def create_presigned_post(object_name, fields=None, conditions=None, expiration=3600):
    """Generate a presigned URL S3 POST request to upload a file

    :param bucket_name: string
    :param object_name: string
    :param fields: Dictionary of prefilled form fields
    :param conditions: List of conditions to include in the policy
    :param expiration: Time in seconds for the presigned URL to remain valid
    :return: Dictionary with the following keys:
        url: URL to post to
        fields: Dictionary of form fields and values to submit with the POST
    :return: None if error.
    """

    # Generate a presigned S3 POST URL
    s3_client = boto3.client("s3")
    try:
        response = s3_client.generate_presigned_post(
            settings.AWS_STORAGE_BUCKET, object_name, ExpiresIn=expiration
        )
    except ClientError as e:
        logging.error(e)
        return None

    return response
Generate a presigned URL to share an S3 object      :param bucket_name: string     :param object_name: string     :param expiration: Time in seconds for the presigned URL to remain valid     :return: Presigned URL as string. If error, returns None. # Generate a presigned URL for the S3 object # The response contains the presigned URL Generate a presigned URL S3 POST request to upload a file      :param bucket_name: string     :param object_name: string     :param fields: Dictionary of prefilled form fields     :param conditions: List of conditions to include in the policy     :param expiration: Time in seconds for the presigned URL to remain valid     :return: Dictionary with the following keys:         url: URL to post to         fields: Dictionary of form fields and values to submit with the POST     :return: None if error. # Generate a presigned S3 POST URL