Commit 202366e1 authored by Ilya Simonov's avatar Ilya Simonov

test do s3 storage

parent d8efaf35
...@@ -100,7 +100,7 @@ class Video(BaseModel): ...@@ -100,7 +100,7 @@ class Video(BaseModel):
description = models.TextField(_('description'), blank=True, null=True) description = models.TextField(_('description'), blank=True, null=True)
local_file = models.FileField( local_file = models.FileField(
_('local file'), _('local file'),
upload_to=asset_upload, storage=S3Boto3Storage(bucket_name='cp-video-files'),
blank=True, blank=True,
null=True, null=True,
) )
......
...@@ -6,13 +6,13 @@ from django.conf import settings ...@@ -6,13 +6,13 @@ from django.conf import settings
def upload_file(content, key, content_type): def upload_file(content, key, content_type):
s3 = boto3.client( s3 = boto3.client(
's3', 's3',
aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_access_key_id=settings.FINAL_AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, aws_secret_access_key=settings.FINAL_AWS_SECRET_ACCESS_KEY,
region_name=settings.AWS_REGION_NAME region_name=settings.FINAL_AWS_REGION_NAME
) )
s3.put_object( s3.put_object(
Bucket=settings.AWS_STORAGE_BUCKET_NAME, Bucket=settings.FINAL_AWS_STORAGE_BUCKET_NAME,
Key=key, Key=key,
Body=content, Body=content,
ContentType=content_type, ContentType=content_type,
......
...@@ -7,18 +7,18 @@ from django.db.models.signals import post_save ...@@ -7,18 +7,18 @@ from django.db.models.signals import post_save
from . import models, utils, s3_uploader from . import models, utils, s3_uploader
@receiver(post_save, sender=models.Video) # @receiver(post_save, sender=models.Video)
def generate_default_thumbnail_for_video(sender, instance, **kwargs): # def generate_default_thumbnail_for_video(sender, instance, **kwargs):
if not instance.thumbnail and instance.local_file: # if not instance.thumbnail and instance.local_file:
if os.path.isfile(instance.local_file.path): # if os.path.isfile(instance.local_file.path):
thumbnail_name = utils.generate_thumbnail(instance.local_file) # thumbnail_name = utils.generate_thumbnail(instance.local_file)
key = f'videos/{thumbnail_name}' # key = f'videos/{thumbnail_name}'
thumbnail_path = f'{settings.MEDIA_ROOT}{key}' # thumbnail_path = f'{settings.MEDIA_ROOT}{key}'
#
content = utils.get_thumbnail_content(thumbnail_path) # content = utils.get_thumbnail_content(thumbnail_path)
content_type = 'image/jpeg' # content_type = 'image/jpeg'
instance.thumbnail = s3_uploader.upload_file(content, key, content_type) # instance.thumbnail = s3_uploader.upload_file(content, key, content_type)
#
instance.save(update_fields=['thumbnail']) # instance.save(update_fields=['thumbnail'])
#
os.remove(thumbnail_path) # os.remove(thumbnail_path)
...@@ -13,13 +13,15 @@ def send_video_to_s3(): ...@@ -13,13 +13,15 @@ def send_video_to_s3():
for local_video in local_videos: for local_video in local_videos:
video_name = local_video.local_file.name.split('/')[-1] video_name = local_video.local_file.name.split('/')[-1]
print('video_name', video_name)
key = asset_upload(local_video, video_name) key = asset_upload(local_video, video_name)
print('key', key)
content = local_video.local_file.read() content = local_video.local_file.read()
local_video.s3_file = upload_file(content, key, 'video/mp4') local_video.s3_file = upload_file(content, key, 'video/mp4')
local_video.status = READY local_video.status = READY
local_video.save(update_fields=['s3_file', 'status']) local_video.save(update_fields=['s3_file', 'status'])
os.remove(local_video.local_file.path) # os.remove(local_video.local_file.path)
local_videos.update(local_file=None) local_videos.update(local_file=None)
...@@ -160,9 +160,19 @@ path = f'{BASE_DIR}/cp_video/local.py' ...@@ -160,9 +160,19 @@ path = f'{BASE_DIR}/cp_video/local.py'
if os.path.exists(path): if os.path.exists(path):
from .local import * from .local import *
else: else:
AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME') FINAL_AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME')
AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID') FINAL_AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY') FINAL_AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_REGION_NAME = env('AWS_REGION_NAME') FINAL_AWS_REGION_NAME = env('AWS_REGION_NAME')
# CloudFront # CloudFront
AWS_S3_CUSTOM_DOMAIN = env('AWS_S3_CUSTOM_DOMAIN') FINAL_AWS_S3_CUSTOM_DOMAIN = env('AWS_S3_CUSTOM_DOMAIN')
AWS_ACCESS_KEY_ID = 'DO00FPURLTXA34ZCTVP7'
AWS_SECRET_ACCESS_KEY = '0gnDLjAoAF+rbKgDVWEln5K7rdPXDxq2/E7C8k3q7IU'
AWS_STORAGE_BUCKET_NAME = 'cp-video-files'
AWS_S3_ENDPOINT_URL = 'https://cp-video-files.nyc3.digitaloceanspaces.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
AWS_LOCATION = 'media'
AWS_DEFAULT_ACL = 'public-read'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment