简体   繁体   English

如何使用AWS PHP sdk在JSON中检索Amazon S3存储桶的内容?

[英]How to retrieve contents of Amazon S3 bucket in JSON using the AWS PHP sdk?

I want to get the list of all the filenames that are inside my Amazon S3 bucket from my PHP server. 我想从我的PHP服务器获取Amazon S3存储桶中所有文件名的列表。 Is that possible with the Amazon PHP sdk? Amazon PHP sdk有可能吗? If not, is there another way? 如果没有,还有其他方法吗? Thanks! 谢谢!

Using the official AWS SDK for PHP v2 , you can do something like the following: 使用适用于PHP v2官方AWS开发工具包 ,您可以执行以下操作:

<?php

require 'vendor/autoload.php';

use Aws\Common\Aws;

// Instantiate an S3 client
$s3 = Aws::factory('/path/to/config.php')->get('s3');

$objects = $s3->getIterator('ListObjects', array(
    'Bucket' => $bucket
));

foreach ($objects as $object) {
    echo $bucket['Name'] . '/' . $object['Key'] . PHP_EOL;
}
  1. It worked fine for me except that MaxKeys was ignored. 除了MaxKeys被忽略之外, MaxKeys我来说还不错。
  2. Is there a difference between using the above method and the one below. 使用上面的方法和下面的方法有什么区别。 Although I couldn't get to array elements within $result . 尽管我无法在$result数组元素。

     use Aws\\Common\\Aws; use Aws\\S3\\S3Client; // Prepare S3 Client to speak with AWS $s3client; $s3client = S3Client::factory(array( 'key' => '', 'secret' => '', 'region' => Region::US_EAST_1 )); // List objects from a bucket $result = $s3client->listObjects(array( 'Bucket' => $aws_bucket, 'MaxKeys' => 25, 'Marker' => 'docs/' )); $objects = $result->getIterator(); foreach ($objects as $wo) { // Prints junk echo $wo['Key'] . ' - ' . $wo['Size'] . PHP_EOL; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM