Amazon S3 PHP SDK,传输文件夹,但是跳过特定文件吗?

时间:2018-07-12 20:29:16

标签: amazon-s3

我正在使用Amazon PHP SDK将服务器上的文件夹上传到存储桶。这很好用:

$skip = ['index.html', '_metadata.txt', '_s3log.txt'];

$meta = [
    'key' => $options->EWRbackup_s3_key,
    'region' => $options->EWRbackup_s3_region,
    'bucket' => $options->EWRbackup_s3_bucket,
    'directory' => 's3://'.$options->EWRbackup_s3_bucket.'/'.$subdir,
];

$client = new S3Client([
    'version' => 'latest',
    'region'  => $meta['region'],
    'credentials'   => [
        'key'       => $meta['key'],
        'secret'    => $options->EWRbackup_s3_secret,
    ]
]);

$s3log = fopen($subpath.'/_s3log.txt', 'w+');
fwrite($s3log, "-- Connecting to ".$meta['region'].":".$meta['bucket']."...\n");

$manager = new Transfer($client, $subpath, $meta['directory'], [
    'before' => function ($command)
        {
            $filename = basename($command->get('SourceFile'));
            fwrite($this->s3log, "-- Sending file $filename...\n");
        },
]);
$manager->transfer();

fwrite($s3log, "-- Disconnecting from ".$meta['key'].":".$meta['bucket']."...");
fclose($s3log);

但是,在我使用Transfer方法上传的文件夹中,有3个文件我要跳过。它们在第一行的$skip变量中定义。我想知道是否有办法让Transfer跳过这3个文件...

1 个答案:

答案 0 :(得分:0)

我在创建的WordPress插件中修改了AWS客户端。在这种情况下,将在AWS / S3 / Transfer.php文件中管理上载。我修改了private function upload($filename)以便从before函数中查找布尔值:

private function upload($filename)
{
    $args = $this->s3Args;
    $args['SourceFile'] = $filename;
    $args['Key'] = $this->createS3Key($filename);
    $command = $this->client->getCommand('PutObject', $args);
    if ($this->before) {
        if (false!==call_user_func($this->before, $command)) {
            return $this->client->executeAsync($command);
        }
    } else {
        return $this->client->executeAsync($command);
    }
}

这将替换原始行:

    $this->before and call_user_func($this->before, $command);
    return $this->client->executeAsync($command);

使用

    if ($this->before) {
        if (false!==call_user_func($this->before, $command)) {
            return $this->client->executeAsync($command);
        }
    } else {
        return $this->client->executeAsync($command);
    }

然后,如果您不想上传此特定文件,则可以在声明的before函数中返回false

之所以能够做到这一点,是因为我可以控制AWS PHP SDK的更新时间,因此可以修改其中包含的代码。我没有在PHP SDK中找到任何钩子可以更好地实现此目的。