Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

media file storing in aws is not working. #250

Open
saileshkush95 opened this issue Sep 9, 2020 · 5 comments
Open

media file storing in aws is not working. #250

saileshkush95 opened this issue Sep 9, 2020 · 5 comments

Comments

@saileshkush95
Copy link

I'm trying to store media files and static files in aws but it is now working for media files. Static files is working perfectly.

@barseghyanartur
Copy link
Owner

See this. That handle_uploaded_file might not work on AWS. You could come up with something using django-storages or use Boto directly. It all comes down to copying the existing file plugin, renaming it to aws_file and making correspondent changes.

PRs are welcome.

@saileshkush95
Copy link
Author

Sorry I'm little but busy, so I'm unable to follow this issue anyway thank you for your response. I'm already using django-storages and boto3. and here is my code snippets.

# AWS Configurations
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_DEFAULT_ACL = None
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_S3_FILE_OVERWRITE = False

from storages.backends.s3boto3 import S3Boto3Storage  # noqa E402

AWS_S3_REGION_NAME = config('AWS_S3_REGION_NAME')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')


class StaticRootS3Boto3Storage(S3Boto3Storage):
    default_acl = 'public-read'
    location = 'static'
    custom_domain = f'{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com'


class MediaRootS3Boto3Storage(S3Boto3Storage):
    file_overwrite = False
    default_acl = 'public-read'
    location = 'media'
    custom_domain = f'{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com'


STATICFILES_STORAGE = 'config.settings.pro.StaticRootS3Boto3Storage'
STATIC_URL = f'https://{AWS_STORAGE_BUCKET_NAME}.s3.ap-south-1.amazonaws.com/ static/'
# # endregion
# DEFAULT_FILE_STORAGE = 'config.settings.pro.MediaRootS3Boto3Storage'
# MEDIA_URL = f'https://{AWS_STORAGE_BUCKET_NAME}.s3.ap-south-1.amazonaws.com/media/'

As for now I'm storing media files in my server, But i didn't want to do that. I want to store all of the media files upload by the user in the aws storage.

To make this work do i need to override this handle_uploaded_file method

@barseghyanartur
Copy link
Owner

Yep. That's what I said. It comes down to making a new plugin with adjusted handle_uploaded_file method.

@saileshkush95
Copy link
Author

So here, handling media files is not dependent on default django storage backend.?

@barseghyanartur
Copy link
Owner

See this and this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants