rafaeltovar/php-tus-aws-s3

该软件包已 弃用 且不再维护。未建议替代软件包。

简单、轻量级,最小化 TUS 服务器与 AWS S3 连接。基于 https://github.com/ankitpokhrel/tus-php

v1.1.1 2023-05-03 11:51 UTC

This package is auto-updated.

Last update: 2024-05-31 21:01:39 UTC


README

此项目不再维护。请使用原始项目 ankitpokhrel/tus-php 以确保定期更新。 以下是使用 AWS S3 的项目说明

Amazon Web Service S3 的 PHP TUS 协议服务器

简单、轻量级,最小化 TUS 服务器与 AWS S3 连接。基于 ankitpokhrel/tus-php

版本

如果您使用 Symfony,请查看以下表格。

Symfony 版本 php-tus-aws-s3 版本
^4.3 ~1.0
^5.0 或 ^6.0 ~1.1

安装

Composer

composer require rafaeltovar/php-tus-aws-s3:~1.x predis/predis

功能

  • 实现上传文件的 TUS 协议服务器
  • AWS S3 多部分上传
  • 直接上传到 AWS S3
  • 使用 Predis 作为数据缓存
  • Flysystem 兼容

文档

理解 TusPhpS3\Server 类构造函数

use TusPhp\Tus\Server as TusServer;

class Server
extends TusServer
{
    //...
    public function __construct(
        TusPhp\Cache\AbstractCache $cache,
        League\Flysystem\AwsS3v3\AwsS3Adapter $storage,
        TusPhpS3\Http\Request $request,
        $excludeAttrApiPath = [],
        $forceLocationSSL = true)
        {
            //...
        }
}
属性 类型 详细信息
$cache TusPhp\Cache\AbstractCache 我们使用 TusPhpS3\Cache\PredisCache 作为 Predis 客户端。
$storage League\Flysystem\AwsS3v3\AwsS3Adapter 此适配器包含 AWS S3 客户端。
$request TusPhps3\Http\Request 此对象包含一个 Symfony\Component\HttpFoundation\Request
$excludeAttrApiPath 数组 从 API 路径中排除某些部分以创建 TUS 服务器的真实 API 基础路径。例如,如果我的 API 基础路径是 https://example.com/uploads 但我的上传 PATCH 是 http://example.com/uploads/{id},则需要排除 ['id']
$forceLocationSSL 布尔值 强制 location 头属性为 https

TUS 路由

/**
 * Create new upload
 * or get server configuration
 **/
$routes->add('uploads', '/api/uploads')
        ->controller([UploadController::class, 'upload'])
        ->methods([POST, OPTIONS])

/**
 * Upload files
 * or delete uploads
 **/
$routes->add('uploads', '/api/uploads/{id}')
        ->controller([UploadController::class, 'upload'])
        ->methods([PATCH, DELETE])

运行 TUS 服务器

use TusPhpS3;

use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;

use Symfony\Component\HttpFoundation\Request as HttpRequest;

class UploadController
{
    public function upload()
    {

        // redis connection
        $predis = new Predis\Client('tcp://10.0.0.1:6379');


        // AWS S3 Client
        $S3client = new S3Client([
            'credentials' => [
                'key'    => 'your-key',
                'secret' => 'your-secret',
            ],
            'region' => 'your-region',
            'version' => 'latest|version',
        ]);

        $server = new TusPhpS3\Server(
            new TusPhpS3\Cache\PredisCache($predis),
            new AwsS3Adapter($S3client, 'your-bucket-name', 'optional/path/prefix'),
            new TusPhpS3\Http\Request(HttpRequest::createFromGlobals()),
            ['id'],
            true
        );

        return $server->serve(); // return an TusPhpS3\Http\Response
    }
}