Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (19.1k points)

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 filehandle. 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);

1 Answer

0 votes
by (44.4k points)
edited by

Try this:

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)

If you are keen to learn about the various AWS skills then this AWS Course page availed by Intellipaat is the right course for you. This course will help you learn to master AWS skills and ace the AWS certification exam! 

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...