Skip to content

Instantly share code, notes, and snippets.

@ugnb
Last active May 9, 2024 13:21
Show Gist options
  • Save ugnb/2b2121e74344139e56f6784ce6449916 to your computer and use it in GitHub Desktop.
Save ugnb/2b2121e74344139e56f6784ce6449916 to your computer and use it in GitHub Desktop.
Using Python boto3 with DigitalOcean object storage
import logging
import urllib.request
import boto3
from settings import OBJECT_STORAGE_KEY, OBJECT_STORAGE_SECRET, OBJECT_STORAGE_REGION, OBJECT_STORAGE_BUCKET
logger = logging.getLogger(__name__)
s3config = {
"region_name": OBJECT_STORAGE_REGION,
"endpoint_url": "https://{}.digitaloceanspaces.com".format(OBJECT_STORAGE_REGION),
"aws_access_key_id": OBJECT_STORAGE_KEY,
"aws_secret_access_key": OBJECT_STORAGE_SECRET }
# Initializing S3.ServiceResource object - http://boto3.readthedocs.io/en/latest/reference/services/s3.html#service-resource
s3resource = boto3.resource("s3", **s3config)
# Initializing S3.Client object - http://boto3.readthedocs.io/en/latest/reference/services/s3.html#client
s3client = boto3.client("s3", **s3config)
# Iterating over existing buckets
for bucket in s3resource.buckets.all():
print(bucket)
# Uploading file from URL to existing bucket
key = "test/object/123456.png"
url = "https://www.digitalocean.com/assets/media/logos-badges/png/DO_Logo_Vertical_Blue-6321464d.png"
response = urllib.request.urlopen(url)
data = response.read()
# Returns object of type S3.Object - http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Object
s3object = s3resource.Bucket(OBJECT_STORAGE_BUCKET).put_object(Key=key, Body=data)
logger.info("Saved object of size {} for key {}".format(
s3object.content_length, s3object.key))
# Getting presigned URL for object with 1 hour expiration
object_url = s3client.generate_presigned_url(
"get_object",
Params={"Bucket": OBJECT_STORAGE_BUCKET, "Key": s3object.key},
ExpiresIn=60*60)
@DanielOX
Copy link

DanielOX commented Jun 4, 2022

Thanks! @ugnb

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