【问题标题】:Create & zip & Download an S3 folder/multiple-files with AWS SDK PHP使用 AWS SDK PHP 创建 & 压缩 & 下载 S3 文件夹/多个文件
【发布时间】:2020-09-01 17:17:20
【问题描述】:

我愿意创建一个php函数,在js中触发,可以:

  • 从特定文件夹中检索所有 AWS S3 存储桶文件(我也可以提供每个文件的路径)
  • 创建一个包含所有 S3 文件的 Zip
  • 按下触发器时下载 Zip (cta)

我可以使用 getObject 方法from this example 下载单个文件但是,我找不到任何信息来下载多个文件并对其进行压缩。

我尝试了 downloadBucket 方法,但是它下载了我的项目架构中的所有文件,而不是作为 zip 文件。 这是我的代码:

<?php


// AWS Info + Connection
$IAM_KEY = 'XXXXX';
$IAM_SECRET = 'XXXXX';
$region = 'XXXXX';  

require '../../vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$FolderToDownload="FolderToDownload";


// Connection OK
$s3 = S3Client::factory(
    array(
        'credentials' => array(
            'key' => $IAM_KEY,
            'secret' => $IAM_SECRET
        ),
        'version' => 'latest',
        'region'  => $region
    )
);

try {

    $bucketName = 'XXXXX';
    $destination = 'NeedAZipFileNotAFolderPath';
    $options = array('debug'=>true);

    // ADD All Files into the folder: NeedAZipFileNotAFolderPath/Files1.png...Files12.png...
    $s3->downloadBucket($destination,$bucketName,$FolderToDownload,$options);
    
    // Looking for a zip file that can be downloaded
    // Can I use downloadBucket? Is there a better way to do it?
    // if needed I can create an array of all files (paths) that needs to be added to the zip file & dl


} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
    }


?>

如果有人可以提供帮助,那就太好了。 谢谢

【问题讨论】:

    标签: php amazon-web-services amazon-s3 zip


    【解决方案1】:

    您可以使用 ZipArchive,从前缀(存储桶的文件夹)列出对象。在这种情况下,我还使用“registerStreamWrapper”来获取密钥并添加到创建的 zip 文件中。

    类似的东西:

    <?php
    
    require '/path/to/sdk-or-autoloader';
    
    $s3 = Aws\S3\S3Client::factory(/* sdk config */);
    $s3->registerStreamWrapper();
    
    $zip = new ZipArchive;
    $zip->open('/path/to/zip-you-are-creating.zip', ZipArchive::CREATE);
    
    $bucket = 'your-bucket';
    $prefix = 'your-prefix-folder'; // ex.: 'image/test/folder/'
    $objects = $s3->getIterator('ListObjects', array(
        'Bucket' => $bucket,
        'Prefix' => $prefix
    ));
    
    foreach ($objects as $object) {
        $contents = file_get_contents("s3://{$bucket}/{$object['Key']}"); // get file
        $zip->addFromString($object['Key'], $contents); // add file contents in zip
    }
    
    $zip->close();
    
    // Download de zip file
    header("Content-Description: File Transfer"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=\"/path/to/zip-you-are-creating.zip\""); 
    readfile ('/path/to/zip-you-are-creating.zip');
        
    ?>
    

    如果您愿意,也可以在下载后删除 zip 文件。

    在大多数情况下必须工作,但我不想将文件保存在服务器中,但我看不到如何通过浏览器直接从 AWS S3 存储桶下载多个对象而不保存文件我的服务器。如果有人知道,请与我们分享。

    【讨论】:

    • 我使用了与您提到的相同的方法,但我希望直接从 Aws 获取 zip
    • 我也是,但我不知道这是否可能。根据我的研究,我不这么认为。如果 API 有一个方法可以做到这一点,那将是非常实用的。
    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 2013-08-29
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多