Laravel Jetstream livewire - 个人资料照片未上传到 s3

问题描述 投票:0回答:3

我已经检查了 AWS S3 中的所有内容、权限、策略存储桶名称,一切看起来都很好。 我已经更新了 jetstream 配置文件中的配置

config\jetstream.php


'profile_photo_disk' => 's3',

我已经用 AWS S3 值更新了 .env 文件变量,但是个人资料照片还没有上传到 S3。

相同的凭据在简单的 Laravel 上传代码上工作,图像在 s3 上成功上传,但是通过使用 Jetstream,它不起作用,屏幕和日志文件中也没有反映错误。

任何人都可以帮忙

laravel amazon-s3 photo profile jetstream
3个回答
2
投票

库trait中写了一个函数,路径和函数如下

路径:endor\laravel\jetstrem\src\HasProfilePhoto.php

功能:

/**
 * Update the user's profile photo.
 *
 * @param  \Illuminate\Http\UploadedFile  $photo
 * @return void
 */
public function updateProfilePhoto(UploadedFile $photo)
{
    tap($this->profile_photo_path, function ($previous) use ($photo) {
        $this->forceFill([
            'profile_photo_path' => $photo->storePublicly(
                'profile-photos', ['disk' => $this->profilePhotoDisk()]
            ),
        ])->save();

        if ($previous) {
            Storage::disk($this->profilePhotoDisk())->delete($previous);
        }
    });
}

如您所见,在 s3 上上传个人资料图片时使用的函数是 storePublicly,但在我的例子中,s3 策略未定义为公开阅读,因此我在中重载了相同的函数

app\Actions\Fortify\UpdateUserProfileInformation.php

在类的顶部添加 Trait 为 use HasProfilePhoto;

/**
 * Update the user's profile photo.
 *
 * @param  \Illuminate\Http\UploadedFile  $photo
 * @return void
 * 
 * 
 */
public function updateProfilePhoto($user, UploadedFile $photo)
{
    tap($user->profile_photo_path, function ($previous) use ($photo, $user) {
        $user->forceFill([
            'profile_photo_path' => $photo->store(
                'profile-photos', ['disk' => config('jetstream.profile_photo_disk')]
            ),
        ])->save();

        if ($previous) {
            \Storage::disk(config('jetstream.profile_photo_disk'))->delete($previous);
        }
    });
}

这对我有用,我希望如果你遇到类似的问题,那么它会对你有所帮助。


0
投票

问题与S3 bucket上的CORS策略有关

我改写为使用 ->store(...) 而不是 ->storePublicly(...),这解决了我的问题。

原文:

public function updateProfilePhoto(UploadedFile $photo)
    {
        tap($this->profile_photo_path, function ($previous) use ($photo) {
            $this->forceFill([
                'profile_photo_path' => $photo->storePublicly(
                    'profile-photos', ['disk' => $this->profilePhotoDisk()]
                ),
            ])->save();

            if ($previous) {
                Storage::disk($this->profilePhotoDisk())->delete($previous);
            }
        });
    }

新:

 public function updateProfilePhoto(UploadedFile $photo)
    {
        tap($this->profile_photo_path, function ($previous) use ($photo) {
            $this->forceFill([
                'profile_photo_path' => $photo->store(
                    'profile-photos', ['disk' => $this->profilePhotoDisk()]
                ),
            ])->save();

            if ($previous) {
                Storage::disk($this->profilePhotoDisk())->delete($previous);
            }
        });
    }

0
投票

您必须修改 User.php 模型,因为特征“使用 HasProfilePhoto;”就是从那里开始的被引用。

public function updateProfilePhoto(UploadedFile $photo, $storagePath = 'profile-photos')
    {
        tap($this->profile_photo_path, function ($previous) use ($photo, $storagePath) {
            $this->forceFill([
                'profile_photo_path' => $photo->store($storagePath, $this->profilePhotoDisk()),
            ])->save();

            if ($previous) {
                Storage::disk($this->profilePhotoDisk())->delete($previous);
            }
        });
    }

    public function getTemporaryProfilePhotoUrl()
    {
        if ($this->profile_photo_path) {
            return Storage::disk($this->profilePhotoDisk())->temporaryUrl(
                $this->profile_photo_path,
                now()->addHours(1) // La URL expirará en una hora
            );
        } else {
            return $this->defaultProfilePhotoUrl();
        }
    }
{{ Auth::user()->getTemporaryProfilePhotoUrl() }}

您还可以在用户模型中创建一个方法来生成一小时的临时 url,从而为您的客户数据提供更高的安全性。

© www.soinside.com 2019 - 2024. All rights reserved.