Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created April 14, 2024 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rg3915/6d81c8321b3191f2bf6e7cc3aa1d957c to your computer and use it in GitHub Desktop.
Save rg3915/6d81c8321b3191f2bf6e7cc3aa1d957c to your computer and use it in GitHub Desktop.
Digital Ocean Storage

Alguém ai manja de como deixar a url de uma imagem no Digital Ocean Storage pública? Estou usando django-storages junto com Digital Ocean, e na doc ele diz pra usar algumas configurações semelhantes a AWS. Segui vários tutoriais

https://django-storages.readthedocs.io/en/latest/backends/digital-ocean-spaces.html

inclusive o do Vitor Freitas

https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html

Minha config ficou assim:

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
DO_STORAGE_BUCKET_NAME=
AWS_S3_ENDPOINT_URL=https://<DO_STORAGE_BUCKET_NAME>.nyc3.digitaloceanspaces.com

E meu settings.py assim

MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR.joinpath('media')


AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('DO_STORAGE_BUCKET_NAME')
AWS_S3_ENDPOINT_URL = config('AWS_S3_ENDPOINT_URL')
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
AWS_DEFAULT_ACL = 'public-read'
AWS_PUBLIC_MEDIA_LOCATION = 'media'

DEFAULT_FILE_STORAGE = 'erp.storage_backends.MediaStorage'

E o storage_backends.py

from storages.backends.s3boto3 import S3Boto3Storage


class MediaStorage(S3Boto3Storage):
    location = 'media'
    file_overwrite = False

Mas a url da imagem sai assim:

https://meusite.nyc3.digitaloceanspaces.com/meusite/media/blabla/DLO9876_G8XRIvX.jpg?AWSAccessKeyId=0000000000000&Signature=11111111111111111111&Expires=1713067505

(url com Signature)

E eu não consigo acessar a imagem nem pela própria aplicação.

O que eu esqueci de fazer?

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