Skip to content

Commit

Permalink
Adds vapor.signed_url_expires_after (#110)
Browse files Browse the repository at this point in the history
* Adds `vapor.signed_url_expires_after`

* Removes trailing comma

* Update SignedStorageUrlController.php

* Update SignedStorageUrlEndpointTest.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
nunomaduro and taylorotwell authored Nov 9, 2021
1 parent 6f9f021 commit ce1e96a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Http/Controllers/SignedStorageUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ public function store(Request $request)

$uuid = (string) Str::uuid();

$expiresAfter = config('vapor.signed_storage_url_expires_after', 5);

$signedRequest = $client->createPresignedRequest(
$this->createCommand($request, $client, $bucket, $key = ('tmp/'.$uuid)),
'+5 minutes'
sprintf('+%s minutes', $expiresAfter)
);

$uri = $signedRequest->getUri();
Expand Down
25 changes: 25 additions & 0 deletions tests/Feature/SignedStorageUrlEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,29 @@ public function test_signed_url_preserves_its_components(): void
$this->assertEquals($components->expected['port'], $components->actual['port']);
$this->assertStringStartsWith($components->expected['path'], $components->actual['path']);
}

public function test_signed_url_expires_after(): void
{
Config::set([
'filesystems.disks.s3.bucket' => $_ENV['AWS_BUCKET'] = 'storage',
'filesystems.disks.s3.key' => $_ENV['AWS_ACCESS_KEY_ID'] = 'sail',
'filesystems.disks.s3.region' => $_ENV['AWS_DEFAULT_REGION'] = 'us-east-1',
'filesystems.disks.s3.secret' => $_ENV['AWS_SECRET_ACCESS_KEY'] = 'password',
'filesystems.disks.s3.url' => $_ENV['AWS_URL'] = 'http://minio:9000',
'filesystems.disks.s3.use_path_style_endpoint' => true,
]);

Gate::define('uploadFiles', static function ($user = null, $bucket = null): bool {
return true;
});

$response = $this->json('POST', '/vapor/signed-storage-url');
parse_str($response->json()['url'], $queryParams);
$this->assertEquals(300, $queryParams['X-Amz-Expires']);

config()->set('vapor.signed_storage_url_expires_after', 6);
$response = $this->json('POST', '/vapor/signed-storage-url');
parse_str($response->json()['url'], $queryParams);
$this->assertEquals(360, $queryParams['X-Amz-Expires']);
}
}

0 comments on commit ce1e96a

Please sign in to comment.