2

I'd like to create a .zip archive, upload it to Amazon S3, then delete the created .zip from the server. Steps 1 and 2 are working great, but the delete step is returning:

unlink(temp/file.zip): Resource temporarily unavailable

I've tried to unset all the related variables and resources, but I'm still getting the error.

Here's the code:

$zipFile = 'temp/file.zip';

// create the zip archive:
$z = new \ZipArchive();
$z->open($zipFile, \ZipArchive::CREATE);
$z->addEmptyDir('testdirectory');

// add a file
$filename = 'fileName.txt';
$content = 'Hello World';
$z->addFromString('testdirectory/' . $filename, $content);
$z->close();

// upload to S3
$s3 = AWS::createClient('s3');
$result = $s3->putObject(array(
    'Bucket'        =>  'my-bucket-name',
    'Key'           =>  basename($zipFile),
    'SourceFile'    =>  $zipFile
));

// check to see if the file was uploaded
if ($result['@metadata']['statusCode'] == "200") {
    $uploaded = true;
}

// delete the temp file
if ($uploaded) {
    unset($result);
    unset($s3);
    unset($z);
    if (file_exists($zipFile)) {
        unlink($zipFile);
    }
}

Some additional details: I'm using Lumen 5.4 and the aws-sdk-php-laravel package.

Any insight would be much appreciated! Thanks.

WayneC
  • 31
  • 1
  • 4
  • What is `$z->close()` returning? It returns a `bool`. Check to see if it's closing successfully. – BugHunterUK Aug 30 '17 at 17:53
  • `$z->close();` returns `bool(true)` – WayneC Aug 30 '17 at 18:19
  • Strange. I tested the code (without S3) and it worked for me. Tricky one. Could you run a script without the S3 code and see if that works. – BugHunterUK Aug 30 '17 at 18:55
  • 1
    Ran it without S3, and it worked as expected. Must have something to do with the s3 client. Will continue to debug. Thanks. – WayneC Aug 30 '17 at 20:13
  • It turns out this was a Windows issue. Running the same code on a Mac worked without any issue, and the file deleted without error. – WayneC Aug 31 '17 at 15:23

1 Answers1

9

S3 is holding resources so we have to forcefully clear the gc (Garbage Collector).

Just do gc_collect_cycles() before deleting that file.

Piyush Patel
  • 1,212
  • 15
  • 19