DigitalOcean Spaces is an object storage service provided by DigitalOcean that makes it easy to store, manage, and serve large amounts of data in the cloud. You can use DigitalOcean Spaces to store files and media that you want to serve to users of your Django application. In this article, we will show you how to upload files to DigitalOcean Spaces using Django.
First, you will need to create a DigitalOcean Space and obtain your access keys. You can find instructions on how to do this in the DigitalOcean Spaces documentation.
Get $200 Free credit from Digital Ocean From this link, Go Now
Install Boto
pip install boto3
Now create file name upload_space.py
import boto3
import botocore
session = boto3.session.Session()
client = session.client('s3',
endpoint_url='https://sfo3.digitaloceanspaces.com',
# Find your endpoint in the control panel, under Settings. Prepend "https://".
config=botocore.config.Config(s3={'addressing_style': 'virtual'}),
region_name='sfo3', # Use the region in your endpoint.
aws_access_key_id='acess_key_id',
# Access key pair. You can create access key pairs using the control panel or API.
aws_secret_access_key='secreet_key') # Secret access key defined through an environment variable.
# upload file
with open('path/banner_promo.png', 'rb') as file_contents:
client.put_object(
Bucket='myBuket',
Key='banner_promo.png',
Body=file_contents,
ACL='public-read',
ContentType='image/png'
)
Now you can upload your file by running this script, bypassing your file and content type Get $200 Free credit from Digital Ocean From this link, Go Now