简体   繁体   中英

Laravel and AWS PHP SDK - Unable to delete a local file after it was uploaded to S3

I am trying to delete a file from a local directory right after I have uploaded it to AWS S3. When I run it on Vagrant I get an error = Text-file:busy, and when I run it on xampp I get the error :permission denied. For some reason the AWS S3 PutObject method is not releasing the file handle. I have tried to unset the s3 object but that didn't work.

Here is the code:

    $tempName = public_path().'/path/to/file'

    //Initialize AWS
    $s3 = AWS::createClient('s3');


    //Upload image to AWS

    try {
        $reponse = $s3->putObject(array(
            'Bucket'       => 'zotamoda',
            'Key'          => $productImage->image_folder."/".$productImage->image_name,
            'SourceFile'   => $tempName,
            'ACL'          => 'public-read',
        ));
    } catch (S3Exception $e) {
        // The AWS error code (e.g., )
        echo $e->getAwsErrorCode() . "\n";
        // The bucket couldn't be created
        echo $e->getMessage() . "\n";
    }


    //Delete image from temporary location
    unlink($tempName);

You could try:

Storage::disk('s3')->put($productImage->image_folder."/".$productImage->image_name, file_get_contents($tempName), 'public');
unlink($tempName);

or, assuming that $tempName is relative to your project root:

Storage::disk('local')->delete($tempName)

I think you should try calling:

gc_collect_cycles();

before deleting the file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM