简体   繁体   English

PHP Amazon S3-将整个文件及其内容从托管服务器批量复制到Amazon S3

[英]PHP Amazon S3 - Bulk Copy whole files and their contents from hosted server to Amazon S3

I am migrating my media files to Amazon S3 from my third party hosted server. 我正在从第三方托管服务器将媒体文件迁移到Amazon S3。 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. 我设法尝试了一个通过HTML上传的教程,该教程用于将文件上传到Amazon S3,以查看一切正常。

I have hundreds of images on my third party hosted server - I would like to migrate them to Amazon S3. 我的第三方托管服务器上有数百张图像-我想将它们迁移到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). 或者使用PHP将文件直接从第三方服务器直接上传到Amazon(最方便的方法)。

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: 这是我试图用来将文件从服务器复制到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/ 不幸的是,它无法正常工作PHP警告:S3 :: inputFile():无法打开输入文件:图片/

How do I go about it please? 我该怎么办?

Thank you 谢谢

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. 用于PHP官方AWS开发工具包提供了一种非常简单的机制,用于将文件目录上传到S3。 It's called the uploadDirectory() method . 这就是所谓的uploadDirectory()方法 After instantiating the S3 client, you would only need to do something like this: 实例化S3客户端后,您只需要执行以下操作:

$s3Client->uploadDirectory(__DIR__ . '/pictures/', $bucket);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM