Spaces:
Sleeping
Sleeping
File size: 1,482 Bytes
1fe2f2f 6fd592e 1fe2f2f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import boto3
from botocore.config import Config
import os
import io
endpoint = os.environ.get("AWS_ENDPOINT")
account_id = os.environ.get("AWS_ACCOUNT_ID")
aws_access_key_id= os.environ.get("AWS_ACCESS_KEY")
aws_secret_access_key= os.environ.get("AWS_SECRET_KEY")
bucket = os.environ.get("AWS_BUCKET")
config = Config(
signature_version='s3v4',
s3={'addressing_style': 'path', 'payload_signing_enabled': False}
)
s3 = boto3.client('s3',
endpoint_url = endpoint,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
config=config)
def upload_to_s3(path, name, extension):
s3.upload_file(path, bucket, name, ExtraArgs={'ContentType': f'audio/{extension}', 'ACL': 'public-read'})
url = s3.generate_presigned_url(
'get_object',
Params={'Bucket': bucket, 'Key': name},
ExpiresIn=604800
)
return url
def upload_image_to_s3(path, name, extension):
s3.upload_file(path, bucket, name, ExtraArgs={'ContentType': f'image/{extension}', 'ACL': 'public-read'})
url = s3.generate_presigned_url(
'get_object',
Params={'Bucket': bucket, 'Key': name},
ExpiresIn=604800
)
return url
def get_url(name):
url = s3.generate_presigned_url(
'get_object',
Params={'Bucket': bucket, 'Key': name},
ExpiresIn=604800
)
return url |