Laravel S3 集成

Laravel S3 integration

我正在阅读 laravel 的文件系统文档,这对初学者来说还不够。

我通过 composer 安装了 s3 包:

league/flysystem-aws-s3-v2 ~1.0

现在我有以下代码,我在本地上传文件,

如何使用以下代码将文件上传到 S3。

public function setPhoto($file, $saveDB = true)
    {
        $stream = Image::make($file->getRealPath())
                  ->stream('jpg', 80);
        $stream = $stream->detach();
        $photoID =  $this->id . '-medium-' . time() . '.jpg';
        $photoPath = 'photos/packages/' . $photoID;
        DataIO::deleteFile($photoPath);
        $this->photo_url = DataIO::updateStream($photoPath, $stream);
        $this->photo_path = $photoID;
        if($saveDB) {
            $this->save();
        }
    }

我已经为 s3、密钥等设置了凭据。 谢谢

文档中有执行此操作的说明Here

您在使用 Intervention 库吗?你可以这样试试:

public function setPhoto($file, $saveDB = true)
{
    $stream = Image::make($file->getRealPath())->stream('jpg', 80);
    $stream = $stream->detach();
    $photoID =  $this->id . '-medium-' . time() . '.jpg';
    $photoPath = 'photos/packages/' . $photoID;
    Storage::disk('s3')->put("$photoPath", $stream->__toString(), 'public');

    DataIO::deleteFile($photoPath);
    $this->photo_url = DataIO::updateStream($photoPath, $stream);
    $this->photo_path = $photoID;

    if($saveDB) {
       $this->save();
    }
}

但请确保您已在 config/filesystem.php

中正确设置 s3 配置