directus / v8-archive

Directus Database API — Wraps Custom SQL Databases with a REST/GraphQL API
https://docs.directus.io/api/reference.html
507 stars 204 forks source link

Directus not using S3 file path when configured, using /project-name/assets/ as file path #2088

Open simonking76 opened 4 years ago

simonking76 commented 4 years ago

• Version of Directus (must be the latest) v8.8.1

• Server, OS & Database Details Ubuntu 18, PHP7.2.24, MySQL 5.7.31

• Error Messages

{"error":{"code":null,"message":"Could not rewind stream","class":"RuntimeException","file":"\/var\/www\/html\/directus-v8-8-1\/directus\/vendor\/slim\/slim\/Slim\/Http\/Stream.php","line":367}}

• Steps to Reproduce

fresh install of latest directus v8.8.1

/src/schema.sql imported into database

configured /config/dev.php as environment (copying _example.php) to point to database.

installed: composer require league/flysystem-aws-s3-v3

configured config file with the S3 credentials as per: https://docs.directus.io/extensions/storage-adapters.html#using-aws-s3

Upload a file in the files section in Directus.

The file is successfully uploaded to S3, but Directus shows a broken image path in files section as its trying to load the image from /project-name/assets/

Same result when creating a collection with a file field, the file is uploaded to S3 bucket but in the file field the image is a broken path as trying to use /project-name/assets/filename.jpg

• Relevant Info, Screenshots, Schema & Logs

S3 configured in project config file:

'storage' => [ 'adapter' => 's3', // What storage adapter to use for files // Defaults to the local filesystem. Other natively supported // options include: Amazon S3, Aliyun OSS, Azure // You'll need to require the correct flysystem adapters through Composer // See https://docs.directus.io/extensions/storage-adapters.html#using-aws-s3

    'root' => '/',          // Where files are stored on disk
    'thumb_root' => '/thumbnails',    // Where thumbnails are stored on disk
    'root_url' => 'https://mybucket.s3-eu-west-2.amazonaws.com',            // Where files are accessed over the web

    'proxy_downloads' => false, // Use an internal proxy for downloading all files

    // S3
    ////////////////////////////////////////
    'key'    => 'XXXXXXXXXXXXXX',
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    'region' => 'eu-west-2',
    'version' => 'latest',
    'bucket' => 'mybucket',
    'options' => [
        'ACL' => 'public-read',
        'Cache-Control' => 'max-age=604800'
    ],
    // 'endpoint' => 's3-endpoint',
],

Can be repeated every time following the steps to reproduce.

simonking76 commented 4 years ago

This issue seems to have been introduced in v8.7.0

Using a fresh install and the default schema.sql with s3 configured I checked out each version and retested.

It started working as expected in v8.6.2 - in the File Library an uploaded image appeared correctly and it uses the s3 path rather than /project-name/assets.

vollstock commented 4 years ago

also when installing on a shared host, the file path is incorrect.

Used: http://<domain>/<project>/assets/… Expected http://<domain>/public/uploads/<project>/…

vollstock commented 4 years ago

I can see a path being built here /src/helpers/file.php

if (!function_exists('get_thumbnail_path')) {
    /**
     * Returns a relative url for the given file pointing to the thumbnailer
     *
     * @param array $thumbnail
     *
     * @return string
     */
    function get_thumbnail_path($name, $thumbnail, $addBasePath = false)
    {
        $path = '';

        $projectName = get_api_project_from_request();
        $paramsString = '?key=' . $thumbnail['key'];

        $path = $projectName . '/assets/' . $name . $paramsString;

        if ($addBasePath === true) {
            $basePath = get_base_path();
            $path = $basePath . $path;
        }

        return $path;
    }
}

However changing that has no impact.

liadgu commented 3 years ago

It seems to take the path from the append_storage_information method, depending on the proxy_downloads setting. If your proxy_downloads is set to false, then changing line 139 like so fixes it:

$data['url'] = $data['full_url'] = $fileRootUrl . $config->get('storage.root') . '/' . $row['filename_disk'];

BTW, if it's set to true, then it seems to work.

simonking76 commented 3 years ago

I tried changing the line from

$data['url'] = $data['full_url'] = $fileRootUrl . '/' . $row['filename_disk']; to $data['url'] = $data['full_url'] = $fileRootUrl . $config->get('storage.root') . '/' . $row['filename_disk'];

in directus/src/helpers/file.php which was line 145 on v8.8.1

I still get the local relative path and I see the broken image graphic instead of the upload.

image

changing proxy_downloads = true didn't work, the file path didn't show at all.

image