'Upload URL image to s3

I'm trying to upload an image from an external url (another website) to my s3. this is what I've done so far.

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
    
    
        // AWS Info
        $bucketName = 'my-bucket';
        $IAM_KEY = 'XXXXXXXXXXXX';
        $IAM_SECRET = 'XXXXXXXXXXXX';
    
        // Connect to AWS
        try {
            $s3 = new S3Client(
                [
                    'credentials' => [
                        'key' => $IAM_KEY,
                        'secret' => $IAM_SECRET
                    ],
                    'version' => 'latest',
                    'region'  => 'ap-southeast-1'
                ]
            );
        } catch (Exception $e) {
            die("Error: " . $e->getMessage());
        }
    
    $binary = file_get_contents('https://blabla.com/imgs.jpg');
    
    $filename = '/storage/test.jpg';
    $response = $s3->putObject(
        [
            'Bucket'        => $bucketName,
            'Key'           => 'obj-'.ceil(rand(1,100000)),
            'SourceFile'    => $filename,
            'Body'        => $binary,
            'ContentType'   => 'image/jpg',
            'StorageClass'  => 'REDUCED_REDUNDANCY'
        ]
    );
    
    dd($response);

when i run the code, i get this error

Unable to open "test.jpg" using mode "r": fopen(test.jpg): Failed to open stream: No such file or directory

how do i upload to s3? Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source