remotestorage
Wrapper for slightly easier use of league/flysystem
with AWS S3 by our Laravel application., (*1)
Installation, Configuration, Use
Installation
Run $ composer vendor:publish
to copy the package's configuration file "/config/remotestorage.php" to your application's "/config" directory., (*2)
(assuming you're using Composer, Laravel, and AWS S3), (*3)
Configuration
Define the following environmental variables with appropriate values:, (*4)
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_DEFAULT_REGION
- AWS_BUCKET
Add the service provider (\Railroad\RemoteStorage\Providers\RemoteStorageServiceProvider
) to the 'providers
array in you application's /config/app.php:, (*5)
'providers' => [
# ...
\Railroad\RemoteStorage\Providers\RemoteStorageServiceProvider::class,
]
Run $ php artisan vendor:publish
to copy the config file and create a remotestorage.php file in your application's /config directory. This will take the values you supplied in the .env file and pass them needed., (*6)
Use
Inject the Railroad\RemoteStorage\Services\RemoteStorageService
class where needed, (*7)
/** @var Railroad\RemoteStorage\Services\RemoteStorageService $remoteStorageService */
protected $remoteStorageService;
public function __constructor(Railroad\RemoteStorage\Services\RemoteStorageService $remoteStorageService){
$this->remoteStorageService = $remoteStorageService;
}
Include namespace at top of file:, (*8)
use Railroad\RemoteStorage\Services;
... to save yourself having to specify the namespace everywhere:, (*9)
/** @var RemoteStorageService $remoteStorageService */
protected $remoteStorageService;
public function __constructor(RemoteStorageService $remoteStorageService){
$this->remoteStorageService = $remoteStorageService;
}
, (*10)
RemoteStorageService
All methods below are public., (*11)
put
Usage Example(s)
$upload = $this->remoteStorageService->put($filenameRelative, $filenameAbsolute);
/** Upload product thumbnail on remote storage using remotestorage package.
* Throw an error JSON response if the upload failed or return the uploaded thumbnail url.
*
* @param Request $request
* @return JsonResponse
*/
public function uploadThumbnail(Request $request)
{
$target = $request->get('target');
$upload = $this->remoteStorageService->put($target, $request->file('file'));
throw_if(
(!$upload),
new JsonResponse('Upload product thumbnail failed', 400)
);
return new JsonResponse(
$this->remoteStorageService->url($target), 201
);
}
Parameters
# |
name |
required |
type |
description |
1 |
filenameRelative |
yes |
string |
name to save file as on remote |
2 |
filenameAbsolute |
yes |
string |
path to file to upload |
Responses
outcome |
return data type |
return data value |
succeeded |
boolean |
true |
failed |
boolean |
false |
read
Usage Example(s)
$file = $this->remoteStorageService->read($filenameRelative);
Parameters
# |
name |
required |
type |
description |
1 |
filenameRelative |
yes |
string |
path to file name from bucket root |
Responses
outcome |
return data type |
return data value (example) |
notes about return data |
failed |
boolean |
false |
succeeded |
string |
"b"""Ø à\x00\x10JFIF\x00\x01\x01\x01\x00 \x00\x00\x00 ■\x00;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 70\n █\x00C\x00\n\x07\x07\x08\x07\x06\n\x08\x08\x08\v\n\n"
|
Raw image data as string |
exists
Usage Example(s)
$exists = $this->remoteStorageService->exists('foo/bar.jpg');
/**
* @param Request $request
* @return JsonResponse
*/
public function uploadThumbnailIfDoesNotAlreadyExist(Request $request)
{
$target = 'foo/' . $request->get('target');
if(!$this->remoteStorageService->exists('foo/')){
$upload = $this->remoteStorageService->put($target, $request->file('file'));
throw_if((!$upload), new JsonResponse('Upload product thumbnail failed', 400));
}
return new JsonResponse(['exists' => true]);
}
Parameters
# |
name |
required |
type |
description |
1 |
filenameRelative |
yes |
string |
path to file name from bucket root |
Responses
outcome |
return data type |
return data value |
exists |
boolean |
true |
does not exist |
boolean |
false |
delete
Usage Example(s)
$this->remoteStorageService->delete('foo/bar.jpg');
public function deleteThumbnail(Request $request)
{
$target = $request->get('target');
$delete = $this->remoteStorageService->delete('foo/' . $target);
throw_if((!$delete), new JsonResponse('product thumbnail deletion failed', 400));
return new JsonResponse(['deleted' => true]);
}
Parameters
# |
name |
required |
type |
description |
1 |
filenameRelative |
yes |
string |
path to file name from bucket root |
Responses
outcome |
return data type |
return data value |
exists |
boolean |
true |
does not exist |
boolean |
false |
create_dir
[TODO], (*12)
rename
[TODO], (*13)
copy
[TODO], (*14)
get_mimetype
[TODO], (*15)
get_timestamp
[TODO], (*16)
get_size
[TODO], (*17)
delete_dir
[TODO], (*18)