On The Fly Image Transformation Signed URLs
With our Signed URLs feature, you can securely manage access to your assets on SlashedCloud. A signed URL includes `sign` and `expires` parameters: `sign` provides a unique signature to verify the URL's authenticity, while `expires` defines the expiration time for the URL, ensuring limited access to the asset.
To activate Signed URLs, first enable the feature in your account settings. Once enabled, you can generate signed URLs for your assets, incorporating these parameters to control access securely.
- Log in to the SlashedCloud dashboard
- Go to the Settings in your OTF Service
- Enable the Signed URLs feature
- Define signing secret key
- Save the new settings
The example below demonstrates how to create a signed URL for an image with a resize of 900 x 400 pixels. The URL includes the `expires` and `sign` parameters, which are required for all signed URLs.
?resize=900,400&expires=1725970683&sign=4f3acb273a191e3393f8a2fc99efad5c432ab31d6a0fcb11501e6a3dd1c57ad6
Here is an example on how to create a signed URL:
Note: Be aware that creating a signed URL requires use of sha256 HMAC and hex digest.
- Node.js
import crypto from 'crypto'; /** * Build URL with signature and expiration * @param path Path with leading / * @param secret Secret key * @param expiresIn Expiration time in seconds * @returns Signed URL */ const signPath = (path: string, secret: string, expiresIn: number): string => { // Ensure the path starts with a / if (!path.startsWith('/')) { throw new Error('Path must start with a leading /'); } // Calculate the expiration time const exp = Math.floor(Date.now() / 1000) + expiresIn; // Build the URL with expiration const uri = `${path}&expires=${exp}`; // Set the signature const sign = crypto.createHmac('sha256', secret).update(uri).digest('hex'); return `${uri}&sign=${sign}`; };
- PHP
<?php /** * Build URL with signature and expiration * @param string $path with leading / * @param string $secret secret key * @param int $expiresIn expiration time in seconds * @return string */ function signPath(string $path, string $secret, int $expiresIn) : string { // Calculate the expiration time $exp = time() + $expiresIn; // Build the URL $uri = $path . '&expires=' . $exp; // Set the signature $sign = hash_hmac('sha256', $uri, $secret); return $uri . '&sign=' . $sign; }
- Python
import hashlib import hmac import time def sign_path(path: str, secret: str, expires_in: int) -> str: # Calculate the expiration time exp = int(time.time()) + expires_in # Build the URL uri = f"${path}&expires=${exp}" # Set the signature sign = hmac.new(secret.encode(), uri.encode(), hashlib.sha256).hexdigest() return f"${uri}&sign=${sign}"