|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Matecat\SimpleS3\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Aws\CommandInterface; |
|
6
|
|
|
use Aws\S3\Transfer; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Matecat\SimpleS3\Client; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
14
|
|
|
|
|
15
|
|
|
class BatchTransferCommand extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Client |
|
19
|
|
|
*/ |
|
20
|
|
|
private Client $s3Client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* CacheFlushCommand constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Client $s3Client |
|
26
|
|
|
* @param string|null $name |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Client $s3Client, ?string $name = null) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($name); |
|
31
|
|
|
|
|
32
|
|
|
$this->s3Client = $s3Client; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function configure(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this |
|
38
|
|
|
->setName('ss3:batch:transfer') |
|
39
|
|
|
->setDescription('Transfer files from/to a bucket.') |
|
40
|
|
|
->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.') |
|
41
|
|
|
->addArgument('src', InputArgument::REQUIRED, 'The source') |
|
42
|
|
|
->addArgument('dest', InputArgument::REQUIRED, 'The destination'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
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 (str_contains($src, 's3://')) { |
|
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
|
|
|
|
|
72
|
|
|
return 0; |
|
73
|
|
|
} catch (Exception $e) { |
|
74
|
|
|
$io->error($e->getMessage()); |
|
75
|
|
|
|
|
76
|
|
|
return 1; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|