Skip to content

Instantly share code, notes, and snippets.

@rohitgupta27 rohitgupta27/shorten.php
Last active Dec 24, 2019

What would you like to do?
public static function getShortCode() {
do {
$shortCode = str_random(6);
// Check if we have already used this shortcode
if(!Storage::disk('s3')->exists($shortCode)) {
return $shortCode;
// Short code exists, lets try a new one
} while (true);
public static function getShortenedUrlFromS3($longUrl) {
try {
$shortCode = static::getShortCode();
// We are creating a file with the name that will be the short URL
// and setting a metatadata WebsiteRedirectionLocation that point to the long URL
// When a user opens the short link like, S3 will redirect the user
// automatically to the long URL which was provided
Storage::disk('s3')->put($shortCode, '', ['visibility' => 'public', 'WebsiteRedirectLocation' => $longUrl]);
return config('app.shortened_domain_name') . '/' . $shortCode;
} catch(\Exception $e) {
// Handle errors as appropriate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.