PHP

How to create private URL on S3/CloudFront with time-limit ?

CK CK 3788 
Created at
Updated at  
917 0 0

For security reason, many companies may require one time temporary or private URL with time-limit. Some people call this as private or signed URL. Below URL is based on PHP and contains S3 signed URL generation function and CloudFront signed URL generation function. The implementation method both S3 and CloudFront is totally different.

 $awsAccessKey,
    'secret' => $awsSecretKey,
]);
$cloudFrontClient = new CloudFrontClient([
    'version'     => '2019-03-26',
    'region'      => 'us-east-1', // Change this to your desired AWS region
    'credentials' => $credentials,
]);
// Replace 'your/private/key.pem' with the path to your private key file
$privateKeyPath = 'your/private/key.pem';
// The CloudFront URL you want to grant one-time access to
$resourceUrl = 'https://your-cloudfront-url/your/resource.jpg';
// Generate a one-time CloudFront URL
$signedUrl = $cloudFrontClient->getSignedUrl([
    'url'         => $resourceUrl,
    'expires'     => time() + 3600, // URL expiration time (in seconds)
    'private_key' => $privateKeyPath,
]);
echo "One-time CloudFront URL: " . $signedUrl;


Make sure to replace 'YOUR_AWS_ACCESS_KEY', 'YOUR_AWS_SECRET_KEY', 'YOUR_CLOUDFRONT_DISTRIBUTION_ID', 'your/private/key.pem', and 'https://your-cloudfront-url/your/resource.jpg' with your actual AWS credentials, CloudFront distribution ID, private key path, and the CloudFront resource URL you want to grant one-time access to.

This example assumes you have a private key file ('your/private/key.pem') associated with your CloudFront key pair.

Additionally, make sure to handle and store your AWS credentials securely, and do not hardcode them directly in your code for production applications.

 

Tags AUTO_INCREMENT HX5V Open SSL PHP Private URL REST S3 Private URL S3 Signed URL S3 Temporary URL Sony HX5V Facebook X
Comments 0
Similar posts
  1. Function can extract file name in string having full path in PHP
    565
  2. How to set AUTO_INCREMENT ID in mySQL ?
    606
  3. Uploading files to Amazon S3 with REST API in PHP
    840
  4. Where to get AWS Access Key ?
    11,260
  5. Apache server unstable detection and the related server(mysql, apache) restart automatically
    532
  6. Image file resizing in PHP
    561
  7. Human readable random string for captchas in PHP
    557
  8. How to get the duration between days in PHP
    419
  9. File downloading function through PHP code
    531
  10. mysql optimization – if you are using Word Press, do not turn off persistent connection
    589
  11. JSON format control in PHP
    690
  12. GD library version in PHP
    503
  13. Date format validation in PHP
    570
  14. PHP function converts new line to BR tag
    558
  15. Email Validation Snipet in PHP
    378
  16. PHP socket programming to get content with post method
    409
  17. Convert plain HTML to XHTML
    451
  18. Get Remote IP Address in PHP
    494
  19. How to remove HTML tags in text string?
    405
  20. Directory listing in PHP
    511
  21. Seconds to String in PHP
    435
  22. Simple PHP code performance measuring example
    791
  23. Simple PHP calendar
    473
  24. Checking similarity between two strings in PHP
    674
  25. How to delete file on certain path ?
    388
  26. ASCII Artwork Generator from image file
    469
  27. PHP Code can know the current OS is windows or not
    582
  28. How to limit by Timeout when opening URL ?
    457
  29. How to put timeout when opening URL by fopen ?
    440
  30. How to convert W3C Date Time Format ?
    453
  1. Setup Apache+PHP+MySQL on Centos 6.7
    605
  2. mcrypt missing when using phpMyAdmin on Centos 6.7
    546
  3. PHP Code can know the current OS is windows or not
    582
  4. Checking similarity between two strings in PHP
    674
  5. Simple PHP calendar
    473
  6. Simple PHP code performance measuring example
    791
  7. Convert plain HTML to XHTML
    451
  8. GD library version in PHP
    503
  9. JSON format control in PHP
    690
  10. mysql optimization – if you are using Word Press, do not turn off persistent connection
    589
  11. File downloading function through PHP code
    531
  12. Image file resizing in PHP
    561
  13. Apache server unstable detection and the related server(mysql, apache) restart automatically
    532
  14. How to set AUTO_INCREMENT ID in mySQL ?
    606
  15. Function can extract file name in string having full path in PHP
    565
  16. Parsing XML in PHP
    1,189
  17. Unzip a ZIP file in PHP
    500
  18. Uploading files to Amazon S3 with REST API in PHP
    840
  19. Human readable random string for captchas in PHP
    557
  20. How to get browser information in PHP?
    851
  21. How to get the duration between days in PHP
    419
  22. Date format validation in PHP
    570
  23. PHP function converts new line to BR tag
    558
  24. Email Validation Snipet in PHP
    378
  25. PHP socket programming to get content with post method
    409
  26. Get Remote IP Address in PHP
    494
  27. How to remove HTML tags in text string?
    405
  28. Directory listing in PHP
    511
  29. Seconds to String in PHP
    435
  30. How to delete file on certain path ?
    388
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,710
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    197
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    575
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    252
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    326
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252