PHP + S3: Permission denied while deleting a file using unlink()

1.6k views Asked by At

I am trying to solve an extremely trivial issue since long but no luck.

I want to delete a file immediately after uploading it to AWS S3 from a PHP WebServer. Following are the steps:

//Upload file to S3 using PHP SDK's S3Client::putObject method:
$result = $s3_client->putObject( array(
                'Bucket' => AWS_BUCKET_NAME,
                'Key'    => $file_name,
                'SourceFile' => $file_path,
                'Metadata'   => array(
                    'metadata_field' => 'metadata_value'
                )
            ));

//Poll the object until it is accessible
$s3_client->waitUntil('ObjectExists', array(
    'Bucket' => AWS_BUCKET_NAME,
    'Key'    => $file_name
));

//Delete the file
unlink( $file_path );

These steps work perfectly in case I upload a small file (~500KB). However, if I upload a larger file (5MB-10MB), I get the following error:

Warning: unlink(<Complete Path to File>): Permission denied in <Complete path to uploader.php> on line N

I am working on Windows and have tried elevating user permissions for the directory and file. (using chmod, chown php commands and made sure that the directory is writable and accessible)

It seems that AWS S3 PutObject method is not releasing the file handle (in case of large files only). I have also tried adding sleep() but not luck.!

Moreover, in case I skip uploading the file to S3 (just to test my delete workflow), the file gets deleted without any issue.

Please help.!

4

There are 4 answers

0
Sahil Gera On BEST ANSWER

In case anybody else is also stuck on this, I moved nginx server deployment to CentOS and this issue was not observed.

1
Omare On

Maybe you need to set the value of upload_max_filesize and post_max_size in your php.ini:

; Maximum allowed size for uploaded files. upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize post_max_size = 40M

After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.

2
Andor On

The issue has raised on https://github.com/aws/aws-sdk-php/issues/841

Try using the gc_collect_cycles() function, it solved the problem for me. See the page above for further reference.

Regards, Andor

0
Alexandre Abreu On

The waitUntil 'ObjectExists' have an timeout/max attempts by default.

You can change using:

$s3Client->waitUntil('ObjectExists', array(
    'Bucket' => AWS_BUCKET_NAME,
    'Key'    => $file_name,
    'waiter.interval'     => 10,
    'waiter.max_attempts' => 6
));