无法上传文件至Digital Ocean Spaces。

huangapple go评论59阅读模式
英文:

Can't upload files to Digital Ocean Spaces

问题

I have managed to set my environment variables for Digital Ocean,

AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
AWS_S3_ENDPOINT_URL = 'https://nyc3.digitaloceanspaces.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
# static settings
AWS_LOCATION = 'static'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

I also ran:

./manage.py collectstatic

And it ran successfully, all files uploaded to Digital Ocean.

But my challenge now is that, when I upload images with Django Admin, they don't upload to Digital Ocean, they rather upload to local folders.

What am I missing in the configuration?

英文:

I have managed to set my environment variables for Digital ocean,


AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
AWS_S3_ENDPOINT_URL = 'https://nyc3.digitaloceanspaces.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
# static settings
AWS_LOCATION = 'static'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

I also ran:

./manage.py collectstatic

And it ran successfully, all files uploaded to Digital ocean.

But my challenge now is that, when I upload Images with Django Admin, they don't upload to Digital ocean, they rather upload to local folders.

What am I missing in the configuration?

答案1

得分: 2

媒体配置缺失,您可以使用以下方式。

在您的Django设置文件中,添加以下配置选项:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage';

就这样!您的Django应用程序现在应该使用DigitalOcean Spaces进行媒体存储。您可以通过应用程序的管理面板上传文件,或使用将文件保存到媒体目录的视图来进行测试。文件应上传到DigitalOcean Space而不是您服务器的文件系统。

英文:

Media configuration is missing, you can use this.
In your Django settings file, add the following configuration options:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

That's it! Your Django application should now use DigitalOcean Spaces for media storage. You can test it by uploading a file through your application's admin panel or using a view that saves a file to your media directory. The file should be uploaded to your DigitalOcean Space instead of your server's file system.

huangapple
  • 本文由 发表于 2023年5月7日 00:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定