操作S3桶

4.1.2 2024-06-17 12:46 UTC

This package is auto-updated.

Last update: 2024-09-17 13:24:35 UTC


README

codecov Build Status Type Coverage

对任何S3桶进行操作的极简抽象。

安装

composer require innmind/s3

使用

use Innmind\S3\{
    Factory,
    Region,
};
use Innmind\OperatingSystem\Factory as OSFactory;
use Innmind\Filesystem\File\Content;
use Innmind\Url\{
    Url,
    Path,
};

$os = OSFactory::build();

$bucket = Factory::of($os)->build(
    Url::of('https://acces_key:acces_secret@bucket-name.s3.region-name.scw.cloud/'),
    Region::of('region-name'),
);

$file = $bucket->get(Path::of('some-file.txt'))->match(
    static fn(Content $file) => $file,
    static fn() => throw new \RuntimeException('File does not exist'),
);
$bucket->upload(Path::of('some-other-name.txt'), $file)->match( // essentially this will copy the file
    static fn() => null, // everything ok
    static fn() => throw new \RuntimeException('Failed to upload file'),
);

为了简化某些使用,您可以在桶接口之上使用文件系统适配器。以下是一个将目录上传到桶的示例:

use Innmind\S3\Filesystem;
use Innmind\Filesystem\Name;

$data = $os->filsystem()->mount(Path::of('/var/data'));
$s3 = Filesystem\Adapter::of($bucket);
$data
    ->get(Name::of('images'))
    ->match(
        $s3->add(...),
        static fn() => null, // do something if there is no images
    );

警告

一个桶可以包含具有相同名称的文件和目录。这不支持innmind/filesystem适配器。这意味着,如果在包含具有相同名称的文件和目录的桶上使用Innmind\S3\Filesystem\Adapter,可能会导致意外的行为或异常。