diff --git a/app/AlbumSources/OpenStackSource.php b/app/AlbumSources/OpenStackSource.php index 9b09b7c..84af0f0 100644 --- a/app/AlbumSources/OpenStackSource.php +++ b/app/AlbumSources/OpenStackSource.php @@ -3,9 +3,14 @@ namespace App\AlbumSources; use App\Photo; +use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Stream; use Illuminate\Support\Facades\Log; +use OpenStack\Common\Error\BadResponseError; +use OpenStack\Common\Transport\Utils as TransportUtils; +use OpenStack\Identity\v2\Service as IdentityV2Service; use OpenStack\OpenStack; use function GuzzleHttp\Psr7\stream_for; @@ -37,13 +42,18 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource try { - $this->getContainer()->deleteObject($photoPath); + $this->getContainer()->getObject($photoPath)->delete(); } catch (GuzzleException $ex) { // Don't worry if the file no longer exists Log::warning('Failed deleting image from OpenStack.', ['error' => $ex->getMessage(), 'path' => $photoPath]); } + catch (BadResponseError $ex) + { + // Don't worry if the file no longer exists + Log::warning('Failed deleting image from OpenStack.', ['error' => $ex->getMessage(), 'path' => $photoPath]); + } } /** @@ -56,7 +66,7 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource { $object = $this->getContainer()->getObject($this->getPathToPhoto($photo, $thumbnail)); - return stream_for($object->getContent()); + return stream_for($object->download()); } /** @@ -112,17 +122,31 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource { $photoPath = $this->getPathToPhoto($photo, $thumbnail); - $container = $this->getContainer(); - $container->uploadObject($photoPath, fopen($tempFilename, 'r+')); + $createOptions = [ + 'name' => $photoPath, + 'content' => file_get_contents($tempFilename) + ]; + + $this->getContainer()->createObject($createOptions); } protected function getClient() { - return new OpenStack($this->configuration->auth_url, [ + $openstackOptions = [ + 'authUrl' => $this->configuration->auth_url, 'username' => $this->configuration->username, 'password' => decrypt($this->configuration->password), 'tenantName' => $this->configuration->tenant_name, - ]); + 'region' => $this->configuration->service_region, + 'identityService' => IdentityV2Service::factory( + new Client([ + 'base_uri' => TransportUtils::normalizeUrl( $this->configuration->auth_url), + 'handler' => HandlerStack::create(), + ]) + ) + ]; + + return new OpenStack($openstackOptions); } protected function getContainer() @@ -132,11 +156,9 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource protected function getStorageService() { - return $this->getClient()->objectStoreService( - $this->configuration->service_name, - $this->configuration->service_region, - 'publicURL' - ); + return $this->getClient()->objectStoreV1([ + 'catalogName' => $this->configuration->service_name + ]); } protected function getOriginalsFolder()