From 58f841a770c2218ff763c87012728dbdbb148e26 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 26 May 2021 16:35:48 +0200 Subject: [PATCH] s3 support for images --- cookbook/integration/integration.py | 10 ++++++---- recipes/settings.py | 19 ++++++++++++++++--- requirements.txt | 2 ++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cookbook/integration/integration.py b/cookbook/integration/integration.py index e4b448086..5a8063bd5 100644 --- a/cookbook/integration/integration.py +++ b/cookbook/integration/integration.py @@ -57,9 +57,8 @@ class Integration: recipe_stream.write(data) recipe_zip_obj.writestr(filename, recipe_stream.getvalue()) recipe_stream.close() - try: - recipe_zip_obj.write(r.image.path, 'image.png') + recipe_zip_obj.writestr('image.png', r.image.file.read()) except ValueError: pass @@ -147,10 +146,13 @@ class Integration: il.msg += f'{recipe.pk} - {recipe.name} \n' self.handle_duplicates(recipe, import_duplicates) except BadZipFile: - il.msg += 'ERROR ' + _('Importer expected a .zip file. Did you choose the correct importer type for your data ?') + '\n' + il.msg += 'ERROR ' + _( + 'Importer expected a .zip file. Did you choose the correct importer type for your data ?') + '\n' if len(self.ignored_recipes) > 0: - il.msg += '\n' + _('The following recipes were ignored because they already existed:') + ' ' + ', '.join(self.ignored_recipes) + '\n\n' + il.msg += '\n' + _( + 'The following recipes were ignored because they already existed:') + ' ' + ', '.join( + self.ignored_recipes) + '\n\n' il.keyword = self.keyword il.msg += (_('Imported %s recipes.') % Recipe.objects.filter(keywords=self.keyword).count()) + '\n' diff --git a/recipes/settings.py b/recipes/settings.py index e7865df97..654c4094b 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -287,15 +287,28 @@ JS_REVERSE_SCRIPT_PREFIX = os.getenv('JS_REVERSE_SCRIPT_PREFIX', os.getenv('SCRI STATIC_URL = os.getenv('STATIC_URL', '/static/') STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") -MEDIA_URL = os.getenv('MEDIA_URL', '/media/') -MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles") +if os.getenv('S3_ACCESS_KEY', ''): + DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' + + AWS_ACCESS_KEY_ID = os.getenv('S3_ACCESS_KEY', '') + AWS_SECRET_ACCESS_KEY = os.getenv('S3_SECRET_ACCESS_KEY', '') + AWS_STORAGE_BUCKET_NAME = os.getenv('S3_BUCKET_NAME', '') + AWS_QUERYSTRING_AUTH = True + + if os.getenv('S3_ENDPOINT_URL', ''): + AWS_S3_ENDPOINT_URL = os.getenv('S3_ENDPOINT_URL', '') + + MEDIA_URL = os.getenv('MEDIA_URL', '/media/') + MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles") +else: + MEDIA_URL = os.getenv('MEDIA_URL', '/media/') + MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles") # Serve static files with gzip STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' TEST_RUNNER = "cookbook.helper.CustomTestRunner.CustomTestRunner" - # settings for cross site origin (CORS) # all origins allowed to support bookmarklet # all of this may or may not work with nginx or other web servers diff --git a/requirements.txt b/requirements.txt index 7914d95e1..0ee4bad05 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,3 +36,5 @@ django-scopes==1.2.0 pytest==6.2.4 pytest-django==4.3.0 django-cors-headers==3.7.0 +django-storages==1.11.1 +boto3==1.17.80 \ No newline at end of file