Cloudflare R2 - 미리 서명된 URL 생성

3414 단어 cloudflarepythonr2
Cloudflare R2는 S3 API와 호환되므로 boto3를 사용하여 R2와 함께 작업할 수 있습니다.

import logging
import boto3
from botocore.exceptions import ClientError

from botocore.client import Config


def create_presigned_url(bucket_name, object_name, expiration=3600):
    s3_client = boto3.client('s3',
            endpoint_url='https://<ACCOUNT_ID>.r2.cloudflarestorage.com',
            aws_access_key_id='<ACCESS_KEY>',
            aws_secret_access_key='<SECRET_KEY>',
            config=Config(signature_version='s3v4'),
      )
    try:
        response = s3_client.generate_presigned_url('get_object',
                    Params={'Bucket': bucket_name,
                                    'Key': object_name},
                                     ExpiresIn=expiration)
    except ClientError as e:
        logging.error(e)
        return None

    # The response contains the presigned URL
    return response


Django와 함께 R2를 사용하려면 https://djangotherightway.com/using-cloudflare-r2-with-django-for-storage을 살펴보십시오.

좋은 웹페이지 즐겨찾기