saqueib
1 year ago

unable to test filesystem temporaryUrl

Posted 1 year ago by saqueib
  • Laravel Version: 5.5.11
  • PHP Version: 7.1
  • Database Driver & Version: MySQL

Description:

I have written some test to verify that a file exists on s3 and download when tried to test Storage::disk('s3')->temporaryUrl($url, $urlExpires); It gives 500 error with this msg but the code works when test by the browser.

This driver does not support creating temporary URLs

Steps To Reproduce:

Test

Storage::fake('s3');

// Create attachment
$attachment = Attachment::create([
    'name' => 'PDF Doc',
    'url' => Storage::disk('s3')->put(
        'attachments', 
        UploadedFile::fake()->create('document.pdf'), 
    1000)
]);

Storage::disk('s3')->assertExists($attachment->url);
...
$this->actingAs($user)
    ->get(route('attachments.download', $attachment->id))
    ->assertRedirect()
// it fails with with 500

Implementaion

$url = Attachment::find($id)->url;

if( Storage::disk('s3')->exists($url) ) {
    // link expiration time
    $urlExpires = Carbon::now()->addMinutes(10);

    try {
        $tempUrl = Storage::disk('s3')->temporaryUrl($url, $urlExpires);
        return redirect($tempUrl);
    } catch ( \Exception $e ) {
        // Unable to test temporaryUrl, its giving driver dont support it issue.
        return response($e->getMessage());
    }
}

One more question? Is there any way to customize the URL generated by temporaryUrl(), problem is downloaded filename is some random string rlbPsVUhqubaXGoEQk96wjPXfQTqurJvNgVjRMZ8.doc which is not very good, I like to customize the name of file to something like project-details.doc etc ?

Please sign in or create an account to participate in this conversation.