donglu5041 2019-07-01 19:37
浏览 103

如何在wordpress小部件中列出s3桶对象?

I am relatively new to php and wordpress. I'm trying to make a custom widget to display all the folders and files in my bucket in a sidebar. I've tested the code outside of the widget, but when I put it inside a class object and try to drag it into my page with elementor in wordpress, I get a 500 error and I can't update the page. I feel like I'm missing something very obvious, but I've been scratching my head today.

This is running on a qa server using the aws php sdk.

<?php

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

class S3_Widget extends WP_Widget {
  // php classnames and widget name/description added
  function __construct() {
    $widget_options = array(
      'classname' => 's3_widget',
      'description' => 'Add a formatted list of s3 folders & files to the sidebar.'
    );
    parent::__construct(
      's3_widget',
      'S3 Widget',
      $widget_options
    );
  }

  // create the widget output
  function widget( $args, $instance ) {
  require_once 'vendor/aws.phar';

  $bucket = 'gravityforms-bucket';

    // Instantiate the client.
    $s3 = new S3Client([
        'profile' => 'formsS3',
        'version' => 'latest',
        'region'  => 'us-west-2'
    ]);
    echo "<ul>";
    // Use the high-level iterators (returns ALL of your objects).
    try {
    $results = $s3->getPaginator('ListObjects', [
            'Bucket' => $bucket
        ]);

    foreach ($results as $result) {
            foreach ($result['Contents'] as $object) {
                echo "<li>" . $object['Key'] . PHP_EOL . "</li>";
            }
    }
    } catch (S3Exception $e) {
        echo $e->getMessage();
    } catch (Exception $e) {
          echo "<pre>errorMessage::"; echo print_r($e->getMessage(), true); echo "</pre>";
      }

    echo "</ul>";
  }
  function update( $new_instance, $old_instance ) {    
    $instance = $old_instance;
    $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
    return $instance;
  }
}
// register the widget
function s3_register_widgets() {
  register_widget( 'S3_Widget' );
}
add_action( 'widgets_init', 's3_register_widgets' );

?>
  • 写回答

1条回答 默认 最新

  • drmy1050 2019-07-02 01:23
    关注

    See below my recommendation.

    1 - Separate the S3 functions from the widget.

    Make a PHP Class that works with S3. This class must returns an Array or List of S3 Bucket Objects.

    Test the PHP Class.

    2 - Try to integrate the S3 Class into the Widget.

    https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingPHP.html

    require 'vendor/autoload.php';
    
    use Aws\S3\S3Client;
    use Aws\S3\Exception\S3Exception;
    
    class S3_List{
    
      public function s3_listing() {
    
        $bucket = '*** Your Bucket Name ***';
    
        // Instantiate the client.
        $s3 = new S3Client([
         'version' => 'latest',
         'region'  => 'us-east-1'
        ]);
    
        // Use the high-level iterators (returns ALL of your objects).
        try {
         $results = $s3->getPaginator('ListObjects', [
             'Bucket' => $bucket
         ]);
    
         foreach ($results as $result) {
             foreach ($result['Contents'] as $object) {
                 echo $object['Key'] . PHP_EOL;
             }
         }
        } catch (S3Exception $e) {
         echo $e->getMessage() . PHP_EOL;
        }
    
        // Use the plain API (returns ONLY up to 1000 of your objects).
        try {
         $objects = $s3->listObjects([
             'Bucket' => $bucket
         ]);
         foreach ($objects['Contents']  as $object) {
             echo $object['Key'] . PHP_EOL;
         }
        } catch (S3Exception $e) {
         echo $e->getMessage() . PHP_EOL;
        }
         
      }
    
    
    }
    
    if ( !function_exists( 'get_s3_bucket' ) ) {
    
        function get_s3_bucket() {
                 $s3_list = new s3_listing();
           return $s3_list->s3_listing();
        }
    
    }

    Regards. Ed.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置