Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Last active May 18, 2023 19:20
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save keithweaver/69869c88c9b98dcc8d168f82a7be3807 to your computer and use it in GitHub Desktop.
Save keithweaver/69869c88c9b98dcc8d168f82a7be3807 to your computer and use it in GitHub Desktop.
Upload an image/object to an AWS S3 Bucket using PHP
<?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();
}
?>
@pooyan1982
Copy link

Line 10 overwrites line 5. Both assign to $filePath. One is a URL and the other is the actual file. Please correct this.

@aaronmckeehan
Copy link

This is great, had trouble all day getting the credentials to work out, but this is the first example I found that had a nested array, and it worked!

@airqb
Copy link

airqb commented Oct 5, 2018

Why i am getting this error "S3 Error upload section:The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256."

@hussain-abid
Copy link

hussain-abid commented Jan 24, 2019

@airqb check this out

$s3 = S3Client::factory(
    array(
        'credentials' => array(
            'key' => $IAM_KEY,
            'secret' => $IAM_SECRET
        ),
        'version' => 'latest',
        'region'  => 'eu-west-1',
        "signature"=> 'v4',
    )
);

add "signature"=> 'v4' it will solve the problem.

@somnathingithub
Copy link

somnathingithub commented Jun 4, 2019

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.

@Ashutosh257
Copy link

is there any other way to do the same thing without credentials?

@fabioacri81
Copy link

fabioacri81 commented Mar 13, 2020

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