andrejmk's avatar

League\Flysystem delete fails

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 have to mention again that when using the read and write methods, the delete worked fine. Also, I'm not using Laravel for this project (if anyone suggest to use its filesystem abstraction).

0 likes
2 replies
willvincent's avatar

Hmm... Maybe try changing from this

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

to this

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

Because:

By default this adapter uses a lock during writes and updates. This behaviour can be altered using the second constructor argument.

andrejmk's avatar

@willvincent No, still the same. I noticed that with small files (2-3 MB) it works fine, the file gets deleted. If I use some bigger files (tried with files > 40 MB, even 1.5 GB), the file is uploaded to S3, but it's not deleted on the local server.

Please or to participate in this conversation.