Skip to content
Advertisement

PHP S3 – How to get all versions of a specific file

I have a Laravel project and a version enabled S3 bucket. I can list the versions of all objects within the bucket using the listObjectVersions method.

My attempt to list the versions of a specific object is as follows:

     $result = $client->listObjectVersions([
            'Bucket' => Config::get('filesystems.disks.s3.bucket'),
            'Key' => $folder . '/' . 'test.png',
        ]);

This seems to get all objects within the bucket which is not what I want. Is there a way to get just one file?

I am using the AWS PHP SDK.

Thanks

Advertisement

Answer

You can use ‘Prefix’ instead of ‘Key’:

        $client = new S3Client([
            'version' => 'latest',
            'region' => 'eu-west-1'
        ]);

        $folder = $this->getS3Folder($request, $study);

        $result = $client->listObjectVersions([
            'Bucket' => Config::get('filesystems.disks.s3.bucket'),
            'Prefix' => $folder . '/' . 'test.png',
        ]);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement