DevOps-Life

[Python] AWS presignedUrl 만들기 본문

IT/Python

[Python] AWS presignedUrl 만들기

인풀 2024. 4. 24. 15:34
반응형
SMALL

 

JAVA에서는 post 방식을 지원하지 않아 찾아본 python 방식

업로드용 presign url을 만들기위한 베이직 소스 

 

import logging
import boto3
from botocore.exceptions import ClientError


def create_presigned_post(bucket_name, 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(bucket_name,
                                                     object_name,
                                                     Fields=fields,
                                                     Conditions=conditions,
                                                     ExpiresIn=expiration)
    except ClientError as e:
        logging.error(e)
        return None

    # The response contains the presigned URL and required fields
    return response

출처 : https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html

 

Presigned URLs - Boto3 1.34.90 documentation

Previous File transfer configuration

boto3.amazonaws.com

 

  • bucket_name: 파일을 업로드할 S3 버킷의 이름입니다.
  • object_name: 업로드할 파일의 경로와 이름을 나타냅니다.
  • fields: 미리 채워진 양식 필드의 딕셔너리입니다. 이는 S3 POST 요청에 함께 제출할 양식 필드 및 값입니다.
  • conditions: 정책에 포함할 조건의 목록입니다. 이는 POST 요청이 유효한지를 검증하는 데 사용됩니다.
  • expiration: 사전 서명된 URL이 유효한 기간(초 단위)입니다.

JAVA 에서 PUT 일 경우 URL 에 Access key가 노출 되어 
API Gateway 와 Lambda를 통하여 url 를 발급 받는 방법으로 해결하려 한다 

 

끝!

반응형
LIST
Comments