Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter mbk24

    (@mbk24)

    function list_s3_bucket_contents() {
    
        require __DIR__ . '/../../../vendor/autoload.php';
    
        $s3 = new Aws\S3\S3Client([
    
            'version' => 'latest',
    
            'region'  => 'your-region',
    
            'credentials' => [
    
                'key'    => 'Your Key here',
    
                'secret' => 'Your secret key here',
    
            ],
    
        ]);
    
        // Output for both buckets
    
        $output = '<select name="s3-buckets">';
    
        // List buckets you want to display
    
        $buckets = ['Bucket1', 'Bucket2'];
    
        foreach ($buckets as $bucket) {
    
            $output .= "<optgroup label='{$bucket}'>";
    
            try {
    
                $results = $s3->listObjects(['Bucket' => $bucket]);
    
                foreach ($results['Contents'] as $item) {
    
                    $output .= "<option value='{$item['Key']}'>{$item['Key']}</option>";
    
                }
    
            } catch (Aws\Exception\AwsException $e) {
    
                // Output the error message if something goes wrong.
    
                $output .= "<option>Error retrieving from {$bucket}</option>";
    
            }
    
            $output .= '</optgroup>';
    
        }
    
        $output .= '</select>';
    
        return $output;
    
    }
    
    add_shortcode('s3_content_dropdown', 'list_s3_bucket_contents');
Viewing 1 replies (of 1 total)