Issues (45)

src/Console/BatchTransferCommand.php (1 issue)

1
<?php
2
3
namespace Matecat\SimpleS3\Console;
4
5
use Aws\CommandInterface;
6
use Aws\S3\Transfer;
7
use Matecat\SimpleS3\Client;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class BatchTransferCommand extends Command
15
{
16
    /**
17
     * @var Client
18
     */
19
    private $s3Client;
20
21
    /**
22
     * CacheFlushCommand constructor.
23
     *
24
     * @param Client $s3Client
25
     * @param null   $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
26
     */
27
    public function __construct(Client $s3Client, $name = null)
28
    {
29
        parent::__construct($name);
30
31
        $this->s3Client = $s3Client;
32
    }
33
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('ss3:batch:transfer')
38
            ->setDescription('Transfer files from/to a bucket.')
39
            ->setHelp('This command transfer files from/to a bucket on S3. Remember: IT\'S PERMITTED ONLY the transfer from local to remote or vice versa.')
40
            ->addArgument('src', InputArgument::REQUIRED, 'The source')
41
            ->addArgument('dest', InputArgument::REQUIRED, 'The destination')
42
        ;
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $src = $input->getArgument('src');
48
        $dest = $input->getArgument('dest');
49
        $io = new SymfonyStyle($input, $output);
50
51
        $io->title('Starting the file transfer...(may take a while)');
52
53
        $from = 'local filesystem';
54
        $to = 'S3';
55
56
        if (strpos($src, 's3://') !== false) {
57
            $from = 'S3';
58
            $to = 'local filesystem';
59
        }
60
61
        try {
62
            $manager = new Transfer($this->s3Client->getConn(), $src, $dest, [
63
                'before' => function (CommandInterface $command) use ($output, $from, $to) {
64
                    $output->writeln('Transferring <fg=green>['.$command['Key'].']</> from '. $from .' to ' . $to);
65
                }
66
            ]);
67
            $manager->transfer();
68
69
            $output->writeln('');
70
            $io->success('The files were successfully transfered');
71
        } catch (\Exception $e) {
72
            $io->error($e->getMessage());
73
        }
74
    }
75
}
76