Last active
May 18, 2023 16:44
-
-
Save keithweaver/08c1ab13b0cc47d0b8528f4bc318b49a to your computer and use it in GitHub Desktop.
Upload image using link to AWS S3 with PHP
This file contains 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
<?php | |
// This file demonstrates file upload to an S3 bucket. This is for using file upload via a | |
// link compared to actually having the image file content. If you are doing it via image | |
// file upload, like getting the file from $_FILE, refer to this: | |
// https://gist.github.com/keithweaver/70eb06d98b008113ce97f6148fbea83d | |
// | |
// You must setup your bucket to have the proper permissions. To learn how to do this | |
// refer to: | |
// https://github.com/keithweaver/python-aws-s3 | |
// https://www.youtube.com/watch?v=v33Kl-Kx30o | |
// I will be using composer to install the needed AWS packages. | |
// The PHP SDK: | |
// https://github.com/aws/aws-sdk-php | |
// https://packagist.org/packages/aws/aws-sdk-php | |
// | |
// Run:$ composer require aws/aws-sdk-php | |
require 'vendor/autoload.php'; | |
use Aws\S3\S3Client; | |
use Aws\S3\Exception\S3Exception; | |
// AWS Info | |
$bucketName = 'SUB_YOUR_BUCKET_NAME_IN'; | |
$IAM_KEY = 'SUB_YOUR_IAM_KEY_IN'; | |
$IAM_SECRET = 'SUB_YOUR_IAM_SECRET_IN'; | |
// Connect to AWS | |
try { | |
// You may need to change the region. It will say in the URL when the bucket is open | |
// and on creation. us-east-2 is Ohio, us-east-1 is North Virgina | |
$s3 = S3Client::factory( | |
array( | |
'credentials' => array( | |
'key' => $IAM_KEY, | |
'secret' => $IAM_SECRET | |
), | |
'version' => 'latest', | |
'region' => 'us-east-2' | |
) | |
); | |
} catch (Exception $e) { | |
// We use a die, so if this fails. It stops here. Typically this is a REST call so this would | |
// return a json object. | |
die("Error: " . $e->getMessage()); | |
} | |
$fileURL = 'SUB_IN_YOUR_IMAGE_PATH'; // Change this | |
// For this, I would generate a unqiue random string for the key name. But you can do whatever. | |
$keyName = 'test_example/' . basename($fileURL); | |
$pathInS3 = 'https://s3.us-east-2.amazonaws.com/' . $bucketName . '/' . $keyName; | |
// Add it to S3 | |
try { | |
// You need a local copy of the image to upload. | |
// My solution: http://stackoverflow.com/questions/21004691/downloading-a-file-and-saving-it-locally-with-php | |
if (!file_exists('/tmp/tmpfile')) { | |
mkdir('/tmp/tmpfile'); | |
} | |
$tempFilePath = '/tmp/tmpfile/' . basename($fileURL); | |
$tempFile = fopen($tempFilePath, "w") or die("Error: Unable to open file."); | |
$fileContents = file_get_contents($fileURL); | |
$tempFile = file_put_contents($tempFilePath, $fileContents); | |
$s3->putObject( | |
array( | |
'Bucket'=>$bucketName, | |
'Key' => $keyName, | |
'SourceFile' => $tempFilePath, | |
'StorageClass' => 'REDUCED_REDUNDANCY' | |
) | |
); | |
// WARNING: You are downloading a file to your local server then uploading | |
// it to the S3 Bucket. You should delete it from this server. | |
// $tempFilePath - This is the local file path. | |
} catch (S3Exception $e) { | |
die('Error:' . $e->getMessage()); | |
} catch (Exception $e) { | |
die('Error:' . $e->getMessage()); | |
} | |
echo 'Done'; | |
// Now that you have it working, I recommend adding some checks on the files. | |
// Example: Max size, allowed file types, etc. | |
?> |
Hey keithweaver, on line 51, how would you do the randon string for the filename?
@pedrozancope, I typically name a filename starting with the Date (& possibly time), then a quasi-unique hash, then a filename.
So:
// Presuming the original filename is held in $filename_original.
$filename_s3 = date( 'Ymd_his' ).'-'.md5( time() . $filename_original ).'-'.$filename_original;
The likelihood of having a collision uploaded at the exact same second, with the exact same filename is practially zero. The benefit of this method is also that the original extension of the filename is retained.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks a lot