Monday, June 18, 2018

facing aws s3 image file path issue?

Hi All,

I have created a bitbucket and some folders inside that with following path: my_bucket\merchant\logos\
I am trying to upload a image file inside the my_bucket\merchant\logos folder but it is saving inside the my-merchant folder instead of logos.
PFB my code snippet-

config.json-
{
"AWS_STORAGE_BUCKET_NAME": "my_bucket",
"AWS_ACCESS_KEY_ID": "MY_KEY",
"AWS_SECRET_ACCESS_KEY": "SECRET_KEY",
"S3DIRECT_REGION": "us-east-1",
}

settings.py-
AWS_STORAGE_BUCKET_NAME = ENV_TOKENS.get('AWS_STORAGE_BUCKET_NAME')
AWS_ACCESS_KEY_ID = ENV_TOKENS.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = ENV_TOKENS.get('AWS_SECRET_ACCESS_KEY')
AWS_BASE_DOMAIN = 's3.amazonaws.com'
AWS_S3_CUSTOM_DOMAIN = '%s.%s' % (AWS_STORAGE_BUCKET_NAME, AWS_BASE_DOMAIN)
STATIC_URL = '/static/'
MEDIA_FILES_LOCATION = 'merchant'
MEDIA_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, MEDIA_FILES_LOCATION)

models.py-
def logo(self):
logo_name = '{}{}'.format(self.name, '.png')
s3_key_name = os.path.join(settings.MEDIA_FILES_LOCATION, 'logos', logo_name)
print("s3_key_name::",s3_key_name)
# Generate file URL
bucket_name = settings.AWS_STORAGE_BUCKET_NAME
s3_endpoint = settings.AWS_BASE_DOMAIN
file_url = 'https://{}/{}/{}'.format(s3_endpoint, bucket_name, s3_key_name)
print("file_url::",file_url)

return file_url

The print values in above functions are-
s3_key_name:: merchant\logos\example.png  file_url:: https://s3.amazonaws.com/my_bucket/merchant\logos\example.png

Can anyone please help me to solve how the set the path correct and instead of saving image url how to save actual image?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1de2f3d3-0e5e-433e-9e95-3c8aa293fcb8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment