Question

I am migrating my media files to Amazon S3 from my third party hosted server. I managed to try a tutorial to upload through an HTML for to upload a file to Amazon S3 to see that everything is working.

I have hundreds of images on my third party hosted server - I would like to migrate them to Amazon S3. I have two options either to download them on my server and upload them one by one. Or else using PHP upload the files directly from the third party server to Amazon directly (the most convenient way).

I tried this code based on the tutorial I tried -

This is the code I am trying to use to copy the files from my server to Amazon S3:

<?php
// Bucket Name
$bucket="justanotherimageons3";
if (!class_exists('S3'))require_once('S3.php');

//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX');
if (!defined('awsSecretKey')) define('awsSecretKey', 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYY');

$tmp = 'pictures/';

$s3 = new S3(awsAccessKey, awsSecretKey);

if($s3->putObjectFile($tmp, $bucket , * , S3::ACL_PUBLIC_READ) )
{
echo "S3 Upload Successful."; 
}

?>

Unfortunately, it is not working PHP Warning: S3::inputFile(): Unable to open input file: pictures/

How do I go about it please?

Thank you

No correct solution

OTHER TIPS

You are trying to open a folder. You need to pass it a file...

$tmp = 'pictures/';

if (is_dir($tmp)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if(!is_file($file)){continue;}

            $s3 = new S3(awsAccessKey, awsSecretKey);

            if($s3->putObjectFile($tmp.$file, $bucket , * , S3::ACL_PUBLIC_READ) ){
                 echo "$file uploaded Successfully."; 
            }
        }
        closedir($dh);
    }
}

The official AWS SDK for PHP provides a very easy mechanism for uploading directories of files to S3. It's called the uploadDirectory() method. After instantiating the S3 client, you would only need to do something like this:

$s3Client->uploadDirectory(__DIR__ . '/pictures/', $bucket);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top