Last active
May 18, 2023 19:20
-
-
Save keithweaver/69869c88c9b98dcc8d168f82a7be3807 to your computer and use it in GitHub Desktop.
Upload an image/object to an AWS S3 Bucket using 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 | |
// Installed the need packages with Composer by running: | |
// $ composer require aws/aws-sdk-php | |
$filePath = "https://example.com/test.png"; | |
require 'vendor/autoload.php'; | |
$bucketName = 'YOUR_BUCKET_NAME'; | |
$filePath = './YOUR_FILE_NAME.png'; | |
$keyName = basename($filePath); | |
$IAM_KEY = 'YOUR_SECRET_ACCESS_KEY'; | |
$IAM_SECRET = 'YOUR_SECRET_ACCESS_CODE'; | |
use Aws\S3\S3Client; | |
use Aws\S3\Exception\S3Exception; | |
// Set Amazon S3 Credentials | |
$s3 = S3Client::factory( | |
array( | |
'credentials' => array( | |
'key' => $IAM_KEY, | |
'secret' => $IAM_SECRET | |
), | |
'version' => 'latest', | |
'region' => 'us-east-2' | |
) | |
); | |
// The region matters. I'm using "US Ohio" so "us-east-2" is the corresponding | |
// region code. You can google it or upload a file to the S3 bucket and look at | |
// the public url. It will look like: | |
// https://s3.us-east-2.amazonaws.com/YOUR_BUCKET_NAME/image.png | |
// | |
// As you can see the us-east-2 in the url. | |
try { | |
// So you need to move the file on $filePath to a temporary place. | |
// The solution being used: http://stackoverflow.com/questions/21004691/downloading-a-file-and-saving-it-locally-with-php | |
if (!file_exists('/tmp/tmpfile')) { | |
mkdir('/tmp/tmpfile'); | |
} | |
// Create temp file | |
$tempFilePath = '/tmp/tmpfile/' . basename($filePath); | |
$tempFile = fopen($tempFilePath, "w") or die("Error: Unable to open file."); | |
$fileContents = file_get_contents($filePath); | |
$tempFile = file_put_contents($tempFilePath, $fileContents); | |
// Put on S3 | |
$s3->putObject( | |
array( | |
'Bucket'=>$bucketName, | |
'Key' => $keyName, | |
'SourceFile' => $tempFilePath, | |
'StorageClass' => 'REDUCED_REDUNDANCY' | |
) | |
); | |
} catch (S3Exception $e) { | |
echo $e->getMessage(); | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
?> |
I used this code. But one issue is occur. Image successfully uploaded in my Amazon S3 bucket, but that image size 0 kB. That's why when I open this image that image successfully open but nothing to see this image. Please help me to solve this problem.
is there any other way to do the same thing without credentials?
I think you can do it without credentials only if you're accessing a bucket with public access read/write. Credentials are needed when you're working with a blocked-to-public-access bucket or location in bucket and anyway when you use authentication.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@airqb check this out
add "signature"=> 'v4' it will solve the problem.