Laravel return false when upload file with S3 Endpoint (Custom domain)

1.1k Views Asked by At

I have working upload file feature with AWS,

This is my code for upload file

    /**
     * Create image and save it to storage.
     *
     * @param  string $key
     * @param  string $relation
     * @return void
     */
    public function createImage(string $key, $relation = 'image')
    {
        if ($file = request()->file) {
            $image = Storage::put('/news', $file);

            $this->image()->create([
                'filename' => $image,
                'url' => Storage::url($image)
            ]);
        }
    }

below my config on .env:

AWS_ACCESS_KEY_ID=key
AWS_SECRET_ACCESS_KEY=secret
AWS_BUCKET=cdn.example.com
AWS_DEFAULT_REGION=ap-southeast-1
AWS_USE_PATH_STYLE_ENDPOINT=false

With those configuration, I was able to upload the files to bucket. Then I enabled Static website hosting and create CNAME with same name as the bucket (cdn.example.com). Until this step, it is still works, but my purpose is to make the Storage::put() function return the url like 'https://cdn.example.com/news/test.jpg', (currently: https://s3.ap-southeast-1.amazonaws.com/cdn.example.com/images/news/test.jpg.

I change the config like this

AWS_ACCESS_KEY_ID=key
AWS_SECRET_ACCESS_KEY=secret
AWS_BUCKET=cdn.example.com
AWS_ENDPOINT=https://cdn.example.com
AWS_DEFAULT_REGION=ap-southeast-1
AWS_USE_PATH_STYLE_ENDPOINT=true

When I try to upload the file, the Storage::put() return false. If the AWS_USE_PATH_STYLE_ENDPOINT changed to false, the upload is working again.

So the problem is use_path_style_endpoint. Anyone knows the solution? Do I need addtional setting on the bucket?

0

There are 0 best solutions below