protected function execute(InputInterface $input, OutputInterface $output)
{
$errors = $this->validate($input);
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
return;
}
try {
$this->client = new \Aws\S3\S3Client(['version' => 'latest', 'region' => $this->helper->getRegion(), 'credentials' => ['key' => $this->helper->getAccessKey(), 'secret' => $this->helper->getSecretKey()]]);
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return;
}
if (!$this->client->doesBucketExist($this->helper->getBucket())) {
$output->writeln('<error>The AWS credentials you provided did not work. Please review your details and try again. You can do so using our config script.</error>');
return;
}
if ($this->coreFileStorage->getCurrentStorageCode() == \Arkade\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3) {
$output->writeln('<error>You are already using S3 as your media file storage backend!</error>');
return;
}
$output->writeln(sprintf('Uploading files to use S3.'));
if ($this->coreFileStorage->getCurrentStorageCode() == \Arkade\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_FILE_SYSTEM) {
try {
$this->client->uploadDirectory($this->storageHelper->getMediaBaseDir(), $this->helper->getBucket());
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
}
} else {
$sourceModel = $this->coreFileStorage->getStorageModel();
$destinationModel = $this->coreFileStorage->getStorageModel(\Arkade\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3);
$offset = 0;
while (($files = $sourceModel->exportFiles($offset, 1)) !== false) {
foreach ($files as $file) {
$output->writeln(sprintf('Uploading %s to use S3.', $file['directory'] . '/' . $file['filename']));
}
$destinationModel->importFiles($files);
$offset += count($files);
}
}
$output->writeln(sprintf('Finished uploading files to use S3.'));
if ($input->getOption('enable')) {
$output->writeln('Updating configuration to use S3.');
$this->state->setAreaCode('adminhtml');
$config = $this->configFactory->create();
$config->setDataByPath('system/media_storage_configuration/media_storage', \Arkade\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3);
$config->save();
$output->writeln(sprintf('<info>Magento now uses S3 for its file backend storage.</info>'));
}
}