0

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.

Andrej
  • 415
  • 1
  • 7
  • 25
  • 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. – Andrej Feb 19 '16 at 08:49
  • have you asked anybody at AWS (issue on their sdk) or anything about this? SInce flysystem basically only does opinionated php function call proxying, I doubt this has anything to do with how flysystem handles it. – Frank de Jonge Feb 19 '16 at 15:24
  • @FrankdeJonge no, but I will post a message on their forum. Although I'm not sure that has to do anything with their SDK, I'm trying to delete a file that is locally on the server, not on S3. – Andrej Feb 19 '16 at 16:13
  • Ah wait, you're right. That's an odd one. There's another thing why should would possibly fail. The permissions of the local file could prevent you from writing (so reading would work, but writing not). This could be the case if the file is written there by another user. – Frank de Jonge Feb 20 '16 at 13:20
  • A good way to find out if that's the case would be to check the error logs, the unlink function should emit a warning when it can't delete the file. – Frank de Jonge Feb 20 '16 at 13:21
  • @FrankdeJonge the error is that I don't have permission to delete the file. I'm using Windows 8.1. It's like fclose is not closing the streams. Important to note is that If I comment the writeStream function and only readStream it, the delete works fine. On Monday I'll test it on a Linux server to see if the same thing is happening there. – Andrej Feb 20 '16 at 14:59

0 Answers0