wgenial/s3-objects-stream-zip-php

S3ObjectsStreamZip 是一个 PHP 库,可以将 AWS S3 中的对象流式传输为一个 zip 文件。

4.1.0 2020-08-19 17:49 UTC

This package is auto-updated.

Last update: 2024-09-13 05:30:02 UTC


README

Packagist PHP Version Codacy GitHub Workflow Status Travis License

概述

S3ObjectsStreamZip 是一个 PHP 库,可以将 AWS S3 中的对象流式传输为一个 zip 文件。

使用 AWS SDK 版本 3 直接从 S3 流式传输对象

安装

composer require wgenial/s3-objects-stream-zip-php

用法

查看 示例 文件夹。

<?php
  include __DIR__.'/../vendor/autoload.php';

  use Aws\S3\Exception\S3Exception;
  use WGenial\S3ObjectsStreamZip\S3ObjectsStreamZip;
  use WGenial\S3ObjectsStreamZip\Exception\InvalidParamsException;

  try {
    // http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html#hardcoded-credentials
    $zipStream = new S3ObjectsStreamZip(array(
      'version' => 'latest', // https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_configuration.html#version
      'region' => 'your-aws-bucket-region', // https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_configuration.html#region
      'credentials' => array(
        'key'    => 'your-aws-key',
        'secret' => 'your-aws-secret'
      ),
      // 'endpoint' => '', // https://docs.aws.amazon.com/general/latest/gr/s3.html
      // 'bucket_endpoint' => '', // https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.S3Client.html#___construct
    ));

    $bucket = 'your-s3-bucket'; // required
    $objects = array(
      array(
        'path' => 'file-text.txt' // required
      ),
      array(
        'name' => 'file-pdf.pdf', // not required
        'path' => 'file-pdf.pdf' // required
      ),
      array(
        'path' => 'logs/file-log.txt' // required
      ),
      array(
        'name' => 'image.png', // you can rename an object to zip, not required
        'path' => 'file-image.png' // required
      )
    );

    $zipname = 'compress.zip'; // required

    $checkObjectExist = false; // no required | default = false

    $zipStream->zipObjects($bucket, $objects, $zipname, $checkObjectExist);
  }
  catch (InvalidParamsException $e) {
    echo $e->getMessage();
  }
  catch (S3Exception $e) {
    echo $e->getMessage();
  }

依赖

  • aws/aws-sdk-php
  • maennchen/zipstream-php
  • guzzlehttp/guzzle

作者

贡献者