无法将图像上传到S3存储桶

时间:2019-10-17 08:43:41

标签: laravel amazon-s3 file-upload

我正在尝试使用vue js和laravel将图像上传到Amazon s3存储桶。但是当我上传它时,出现以下异常:-

enter image description here

这是我在控制器中写的用于上传文件的内容。

public function addProperty(Request $request)
{

    $property = new Property;
    $property->title = request('title');
    $property->property_type =  request('type');
    $property->property_sub_type = request('subtype');
    $property->address = request('address');
    $property->property_index = 400;

    #$property->save();

    if ($request->hasFile('image')) {


        $fileNameWithExtension = $request->file('image')- >getClientOriginalName();
        $fileName = pathinfo($fileNameWithExtension, PATHINFO_FILENAME);
        $extension = $request->file('image')->getClientOriginalExtension();

        $fileNameStore =$fileName.'_'.time().'_'.'.'.$extension;
        $disk = Storage::disk('s3');
        $disk->put($fileNameStore, fopen($request->file('image'), 'r+'), 'public');

        $profilePicImageUri = Storage::disk('s3')->url($fileNameStore);
        dd($profilePicImageUri);
        return $profilePicImageUri;

    }

}

这是我在Vue中所做的

onSubmit(){
            let self = this;
            let data = new FormData();
            data.append('image',this.file);
            data.append('title',this.propertyTitle);
            data.append('type',this.type);
            data.append('subtype',this.subtype);
            data.append('lat',this.lat);
            data.append('long',this.long);
            data.append('address',this.address);

            let config = {
                    headers:{
                        'Content-Type' : 'multipart/form-data'
                    }
            }
            axios.post(baseUrl + "api/admin/addproperty",data,config)
                .then(function (response) {
                    console.log(response);
            }).catch(function (error) {
                {
                    console.log(error);
                }
            })

        },

我已经在env文件中设置了AWS配置。这是我的配置

AWS_ACCESS_KEY_ID=XXXX
AWS_SECRET_ACCESS_KEY=XXX
AWS_DEFAULT_REGION=eu-central-1
AWS_BUCKET= php-laravel-upload
AWS_URL = https://php-laravel-upload.s3.eu-central-1.amazonaws.com

我不明白我在做什么错。谁能帮我吗?

1 个答案:

答案 0 :(得分:0)

那样尝试

 if ($request->hasFile('image')) {
        $fileInstance = $request->file('image');

        $fileNameStore = $fileInstance->getClientOriginalName().'_'.time().'_.'.$fileInstance->getClientOriginalExtension();
        Storage::disk('s3')->put($fileNameStore, $fileInstance, 'public');

        $profilePicImageUri = Storage::disk('s3')->url($fileNameStore);
      //  dd($profilePicImageUri);
        return $profilePicImageUri;

    }