Created
July 22, 2025 21:02
-
-
Save rwcitek/a807c9414bdc5f8be60be1d0b6a9da86 to your computer and use it in GitHub Desktop.
cloud function to get Yosemite images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This was written for Digital Ocean cloud functions | |
# It fetches the live photo of Half Dome every minute and stores in DO Spaces | |
import os | |
from datetime import datetime, timezone | |
import boto3 | |
import requests | |
# fetch env settings | |
WEBCAM_URL = os.environ.get('WEBCAM_URL', 'https://cdn.pixelcaster.com/public.pixelcaster.com/snapshots/yosemite-sentinel/latest.jpg') | |
DO_SPACES_NAME = os.environ.get('DO_SPACES_NAME', 'your-webcam-images') | |
DO_SPACES_REGION = os.environ.get('DO_SPACES_REGION', 'nyc3') | |
DO_SPACES_KEY = os.environ.get('DO_SPACES_KEY') | |
DO_SPACES_SECRET = os.environ.get('DO_SPACES_SECRET') | |
def main(args): | |
# Create timestamp | |
now_utc = datetime.now(timezone.utc).isoformat(timespec='seconds').replace("+00:00", "Z") | |
# Verify envs | |
envs = [ f"{key}={value[:10]}" for key, value in os.environ.items() if key.startswith("DO_")] | |
envs = "\n".join(envs) | |
# create boto connection | |
session = boto3.session.Session() | |
s3_client = session.client( | |
's3', | |
region_name=DO_SPACES_REGION, | |
endpoint_url=f'https://{DO_SPACES_REGION}.digitaloceanspaces.com', | |
aws_access_key_id=DO_SPACES_KEY, | |
aws_secret_access_key=DO_SPACES_SECRET | |
) | |
# fetch image | |
response = requests.get(WEBCAM_URL, stream=True) | |
# create object name | |
file_extension = "jpg" | |
spaces_object_name = f"webcam_images/{now_utc}.{file_extension}" | |
# save image to spaces | |
s3_client.upload_fileobj( | |
response.raw, | |
DO_SPACES_NAME, | |
spaces_object_name, | |
ExtraArgs={ | |
'ContentType': response.headers['Content-Type'], | |
'ACL': 'public-read', | |
} | |
) | |
# generate output | |
greeting = f''' | |
Now: {now_utc} | |
Region: {DO_SPACES_REGION} | |
output_name: {spaces_object_name} | |
{envs} | |
''' | |
print(greeting) | |
return {"body": greeting} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment