popphp/pop-storage

Pop PHP 框架的存储组件

2.0.2 2024-04-02 00:07 UTC

This package is auto-updated.

Last update: 2024-09-02 01:02:11 UTC


README

Build Status Coverage Status

Join the chat at https://popphp.slack.com Join the chat at https://discord.gg/TZjgT74U7E

概述

pop-storage 是一个存储组件,它提供可交换的适配器,可以轻松管理并在不同的存储资源之间切换。支持的存储适配器包括

  • AWS S3
  • Microsoft Azure
  • 本地磁盘

注意: 使用 AWS S3 和 Microsoft Azure 等企业存储解决方案需要在各自的门户中创建凭据和权限。请参阅在线文档、指南和政策,了解您尝试使用此组件连接应用程序的存储平台。请谨慎授予应用程序实例访问权限和分配权限。始终遵循您选择的存储平台的推荐安全策略和指南。

pop-storagePop PHP 框架 的一个组件。

顶部

安装

使用 Composer 安装 pop-storage

composer require popphp/pop-storage

或者,在 composer.json 文件中引用它

"require": {
    "popphp/pop-storage" : "^2.0.0"
}

顶部

快速入门

可以使用以下工厂之一创建存储对象

use Pop\Storage\Storage;

$storage = Storage::createAzure('ACCOUNT_NAME', 'ACCOUNT_KEY', 'CONTAINER');

然后可以将本地文件上传到存储平台

$storage->putFile('test.pdf');

或者,可以从存储平台下载远程文件,这将返回文件内容以在应用程序中使用

$fileContents = $storage->fetchFile('test.pdf');

顶部

适配器

默认情况下,有 3 个可用的适配器。所有适配器都使用相同的接口,并可互换。只要实现相同的 Pop\Storage\StorageInterface,就可以创建其他适配器。

AWS S3

Amazon AWS S3 适配器与 AWS S3 交互,需要从 AWS 管理控制台获取以下凭据和访问信息

  • AWS 密钥
  • AWS 秘密
  • AWS 区域
  • AWS 版本(通常是 latest
  • 要访问的 AWS S3 存储桶(格式为 s3://bucket
use Pop\Storage\Storage;

$storage = Storage::createS3('AWS_BUCKET', new S3\S3Client([
    'credentials' => [
        'key'    => 'AWS_KEY',
        'secret' => 'AWS_SECRET',
    ],
    'region'  => 'AWS_REGION',
    'version' => 'AWS_VERSION'
]));

Microsoft Azure

Microsoft Azure 适配器与 Microsoft Azure 存储交互,需要从 Azure 管理控制台获取以下凭据和访问信息

  • 帐户名称
  • 帐户密钥
  • 要访问的 Azure 容器(格式为 container
use Pop\Storage\Storage;

$storage = Storage::createAzure('ACCOUNT_NAME', 'ACCOUNT_KEY', 'CONTAINER');

本地磁盘

本地磁盘适配器允许使用与其他适配器相同的接口在应用程序的本地磁盘上简单管理文件和文件夹。这在本地开发和测试时很有用,然后再切换到企业适配器进行生产。

只需要主目录作为基本位置

use Pop\Storage\Storage;

$storage = Storage::createLocal(__DIR__ . '/tmp/');

顶部

处理文件

有几种方法可以帮助将文件上传到或从存储平台下载,以及获取有关它们的通用数据和信息。

将本地文件放在远程位置

使用磁盘上的文件

$storage->putFile('test.pdf');

使用文件内容的流

$storage->putFileContents('test.pdf', $fileContents);

获取文件内容

此方法返回要用于应用程序中的文件内容

$fileContents = $storage->fetchFile('test.pdf');

获取文件信息

此方法使用自定义请求(即,HEAD 请求)来返回有关文件的一般信息,而无需下载文件内容

// Returns an array of file info:
$info = $storage->fetchFileInfo('test.pdf');

从服务器请求($_FILES 格式)上传文件

$storage->uploadFiles($_FILES);
// Where $file follows the $_FILES array format specified in PHP:
// $file = ['tmp_name' => '/tmp/Hs87jdk', 'name' => 'test.pdf', 'size' => 8574, 'error' => 0]
$storage->uploadFile($file);

列出文件

您可以列出或搜索当前位置中的文件

$files = $storage->listFiles();
$files = $storage->listFiles('test*');
$files = $storage->listFiles('*.pdf');

列出所有或搜索所有目录和文件

$all = $storage->listAll();

从一个远程位置复制或移动文件到另一个远程位置

// The source file remains
$storage->copyFile('test.pdf', 'foo/test2.pdf');
// The source file no longer exists
$storage->renameFile('test.pdf', 'foo/test2.pdf');

从同一远程存储资源的外部位置复制或移动文件

这允许您在不同的AWS存储桶或Azure容器之间复制或移动文件,这些存储桶或容器不在当前引用的存储桶或容器之外。

外部

// AWS example. The source file remains
$storage->copyFileToExternal('test.pdf', 's3://other-bucket/test.pdf');

// Azure example. The source file remains
$storage->copyFileToExternal('test.pdf', '/other-container/test.pdf');
// AWS example. The source file no longer exists
$storage->moveFileToExternal('test.pdf', 's3://other-bucket/test.pdf');

// Azure example. The source file no longer exists
$storage->moveFileToExternal('test.pdf', '/other-container/test.pdf');

从外部

// AWS example. The source file remains
$storage->copyFileFromExternal('s3://other-bucket/test.pdf', 'test.pdf');

// Azure example. The source file remains
$storage->copyFileFromExternal('/other-container/test.pdf', 'test.pdf');
// AWS example. The source file no longer exists
$storage->moveFileToExternal('s3://other-bucket/test.pdf', 'test.pdf');

// Azure example. The source file no longer exists
$storage->moveFileToExternal('/other-container/test.pdf', 'test.pdf');

删除文件

$storage->deleteFile('test.pdf');

顶部

目录

AWS和Azure存储资源不明确支持“目录”或“文件夹”。然而,它们仍然允许以“前缀”形式存在一种“类似目录”的结构。《pop-storage》组件将这些功能标准化为更“类似目录”的接口,允许更改目录、创建目录和删除目录。

注意:只有S3和本地适配器允许创建或删除空目录。Azure存储资源不允许明确创建或删除空目录。相反,当使用前缀上传新文件时,会自动创建一个“目录”(前缀)。相反,当删除最后一个使用前缀的文件时,会自动删除“目录”(前缀)。

$storage = Storage::createS3('s3://my-bucket', new S3\S3Client([
    'credentials' => [
        'key'    => 'AWS_KEY',
        'secret' => 'AWS_SECRET',
    ],
    'region'  => 'AWS_REGION',
    'version' => 'AWS_VERSION'
]));

// Create the bucket 's3://my-bucket/foo'
$storage->mkdir('foo');

// Point the adapter at 's3://my-bucket/foo'
// Any files pushed will store here
// Any delete calls will delete files from here
$storage->chdir('foo');

// Removes the bucket and its content
$storage->rmdir('foo');

列出目录

您可以在当前位置列出或搜索目录

$dirs = $storage->listDirs();
$dirs = $storage->listDirs('foo*');
$dirs = $storage->listDirs('*foo/');

列出所有或搜索所有目录和文件

$all = $storage->listAll();

顶部

辅助方法

存在许多辅助方法,可以提供有关文件状态或文件是否存在等信息。

var_dump($storage->fileExists('test.pdf'))    // Returns bool
var_dump($storage->isDir('foo'));             // Returns bool
var_dump($storage->isFile('test.pdf'));       // Returns bool
var_dump($storage->getFileSize('test.pdf'));  // Returns filesize value as an integer
var_dump($storage->getFileType('test.pdf'));  // Return either 'file' or 'dir'
var_dump($storage->getFileMTime('test.pdf')); // Returns date/time value
var_dump($storage->md5File('test.pdf'));      // Returns MD5 hash of file

顶部