PHP League\Flysystem delete fails when using readStream and writeStream

1.1k Views Asked by At

I'm using League\Flysystem to upload files on Amazon S3. After I upload the file, I want to delete the file from my server. At first I was using the read and write methods and after that the delete method and it worked fine. Since I have large files, using the read method is not a good solution, because it allocates a lot of memory. Now I'm using readStream and writeStream, but when writeStream finishes, I close the stream and the delete method returns false. I tried to use unlink as well, but the file is still not deleted. It looks like the file is used by another process, but I'm not sure how to release it. Here is a code sample:

$client = S3Client::factory([
        'credentials' => [
            'key'    => AWS_ACCESS_KEY,
            'secret' => AWS_SECRET_KEY,
        ],
        'region' => 'eu-west-1',
        'version' => '2006-03-01',
    ]);

$s3 = new AwsS3Adapter($client, AWS_BUCKET);
$local = new Filesystem(new Adapter($localFolder));

$config = new Config();
$config->set("visibility", "private");

$readStream = $local->readStream($fileName);
$writeStream = $s3->writeStream($file, $readStream, $config);

if (is_resource($readStream))
{
    fclose($readStream);
}

if (is_resource($writeStream))
{
    fclose($writeStream);
}

$local->delete($fileName);

I also tried this, with no luck:

$local = new Filesystem(new Adapter($localFolder), 0);

I have to mention again that when using the read and write methods, the delete worked fine.

0

There are 0 best solutions below