42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
import boto3
|
|
import os
|
|
|
|
print("[DEBUG] boto3 init...")
|
|
__session = boto3.session.Session()
|
|
__s3 = __session.client(
|
|
service_name='s3',
|
|
endpoint_url='https://storage.yandexcloud.net'
|
|
)
|
|
|
|
__bucket = os.getenv('AWS_DEFAULT_BUCKET')
|
|
|
|
|
|
def s3_upload_from_buffer(filename: str, data: bytes):
|
|
# Загрузить объекты в бакет
|
|
|
|
## Из строки
|
|
__s3.put_object(Bucket=__bucket, Key=filename, Body=data)
|
|
|
|
|
|
def s3_upload_from_file(s3_filename: str, target_file: str):
|
|
## Из файла
|
|
__s3.upload_file(target_file, __bucket, s3_filename)
|
|
|
|
|
|
# # Получить список объектов в бакете
|
|
# for key in __s3.list_objects(Bucket='bucket-name')['Contents']:
|
|
# print(key['Key'])
|
|
|
|
|
|
def s3_delete(files):
|
|
forDeletion = [{'Key': 'object_name'}, {'Key': 'script/py_script.py'}]
|
|
# Удалить несколько объектов
|
|
response = __s3.delete_objects(Bucket='bucket-name', Delete={'Objects': forDeletion})
|
|
|
|
|
|
def s3_get(file: str):
|
|
# Получить объект
|
|
return __s3.get_object(Bucket=__bucket, Key=file)['Body'].read()
|
|
|
|
|