Lucien Dubois
Lucien Dubois

Reputation: 1674

Change S3 Endpoint in Laravel

I used the filesystems.php file to configure my S3. When I try to put content in the bucket I receive this error:

Encountered a permanent redirect while requesting https://s3-us-west-2.amazonaws.com/MYBUCKET... Are you sure you are using the correct region for this bucket?

I then try to acces the url and I get this message:

The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

On the same page I get:

<Endpoint>s3.amazonaws.com</Endpoint>

Then how could I remove the region from the URL Laravel generates?

Upvotes: 3

Views: 16715

Answers (4)

joelennon
joelennon

Reputation: 189

The simplest approach is to create a new disk that uses the S3 driver in config/filesystems.php. You don't need to create a service provider - the S3 driver will pick up the endpoint from the disk config if supplied.

'spaces' => [
    'driver' => 's3',
    'key' => env('DO_SPACES_KEY'),
    'secret' => env('DO_SPACES_SECRET'),
    'endpoint' => 'https://nyc3.digitaloceanspaces.com',
    'region' => 'nyc3',
    'bucket' => env('DO_SPACES_BUCKET'),
],

Set the DO_SPACES_KEY, DO_SPACES_SECRET and DO_SPACES_BUCKET environment variables to the appropriate values.

Source: https://laracasts.com/discuss/channels/laravel/custom-file-driver-digital-ocean-spaces?page=1

Upvotes: 2

geoom
geoom

Reputation: 8117

You can create a customer service provider like that:

use Illuminate\Support\ServiceProvider;
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
use Aws\Laravel\AwsServiceProvider;
use Storage;

class AwsS3ServiceProvider extends ServiceProvider
{

    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('s3', function($app, $config) {
            $client = new S3Client([
                'credentials' => [
                    'key'    => $config['key'],
                    'secret' => $config['secret'],
                ],
                'region' => $config['region'],
                'version' => $config['version'],
                'endpoint' => $config['endpoint'],
                'ua_append' => [
                    'L5MOD/' . AwsServiceProvider::VERSION,
                ],
            ]);

            return new Filesystem(new AwsS3Adapter($client, $config['bucket_name']));
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

And add endpoint variable into config/filesystems.php as well:

's3' => [
    'driver' => 's3',
    'key'    => env('AWS_KEY'),
    'secret' => env('AWS_SECRET'),
    'region' => env('AWS_REGION'),
    'version' => 'latest',
    'endpoint' => env('AWS_ENDPOINT'),
    'bucket_name' => env('AWS_BUCKET_NAME')
]

Look at docs to get details about how to extend Storage facade.

Upvotes: 8

Gangadhar
Gangadhar

Reputation: 11

The specific bucket is not created in AWS, so please create a bucket in AWS, and use same bucket at filesystem config, the issue will be resolved.

Upvotes: 0

nextt1
nextt1

Reputation: 3988

You can use this package https://github.com/aws/aws-sdk-php-laravel In this package you can specify your settings in config file as desired.

Upvotes: 0

Related Questions