Laravel s3 多桶

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35225836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:11:14  来源:igfitidea点击:

Laravel s3 multiple buckets

phplaravelamazon-web-servicesamazon-s3

提问by Leandro Ferreira

My Laravel application needs to manipulate files present in multiple buckets simultaneously into a single session. So, I couldn't find a way to change several times the current bucket, since my .envfile is like this:

我的 Laravel 应用程序需要将多个存储桶中的文件同时操作到一个会话中。所以,我找不到更改当前存储桶数次的方法,因为我的.env文件是这样的:

S3_KEY='MY-KEY'
S3_SECRET='MySeCret'
S3_REGION='us-east-1'
S3_BUCKET='my-first-used-bucket'

I found somewhere that I could do this:

我找到了可以做到这一点的地方:

Config::set('filesystems.disks.s3.bucket', 'another-bucket');

but It works only once. What I need is something like:

但它只工作一次。我需要的是这样的:

Storage::disk('s3')->put('/bucket-name/path/filename.jpg', $file, 'public');

Where /bucket-name/could be any bucket that I already create. What can I do? Thanks a lot!

/bucket-name/我已经创建的任何存储桶在哪里。我能做什么?非常感谢!

回答by Samuel Hawksby-Robinson

You are correct in that Config::set();only works once per request. My estimation is that this is done intentionally to stop the kind of thing you are attempting to do in your code example.

您是正确的,因为 Config::set();每个请求只能工作一次。我的估计是故意这样做是为了阻止您在代码示例中尝试做的事情。

In config/filesystems.php you can list any number of "disks". These are locations of your file repositories. It looks like so:

在 config/filesystems.php 中,您可以列出任意数量的“磁盘”。这些是文件存储库的位置。它看起来像这样:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root'   => storage_path('app'),
    ],

    'ftp' => [
        'driver'   => 'ftp',
        'host'     => 'ftp.example.com',
        'username' => 'your-username',
        'password' => 'your-password',

        // Optional FTP Settings...
        // 'port'     => 21,
        // 'root'     => '',
        // 'passive'  => true,
        // 'ssl'      => true,
        // 'timeout'  => 30,
    ],

    's3' => [
        'driver' => 's3',
        'key'    => env('S3_KEY',''),
        'secret' => env('S3_SECRET',''),
        'region' => env('S3_REGION',''),
        'bucket' => env('S3_BUCKET',''),
    ],
]

The Solution

解决方案

The solution is to create a new disk of the extra buckets you want to use. Treat your buckets like different disks.

解决方案是为要使用的额外存储桶创建一个新磁盘。将您的存储桶视为不同的磁盘。

Note:The user that the S3_Key belongs to needs to have permissions to perform your required actions on the S3 buckets you are setting up as additional 'disks'.

注意:S3_Key 所属的用户需要具有对您设置为附加“磁盘”的 S3 存储桶执行所需操作的权限。

'disks' => [

    //All your other 'disks'
    ...

    //My default bucket details.
    's3' => [
        'driver' => 's3',
        'key'    => env('S3_KEY',''),
        'secret' => env('S3_SECRET',''),
        'region' => env('S3_REGION',''),
        'bucket' => env('S3_BUCKET',''),
    ],

    's3MyOtherBucketName' => [
        'driver' => 's3',
        'key'    => env('S3_KEY',''),
        'secret' => env('S3_SECRET',''),
        'region' => env('S3_REGION',''),
        'bucket' => 'myOtherBucketName',
    ],

    's3YetAnotherBucketName' => [
        'driver' => 's3',
        'key'    => env('S3_KEY',''),
        'secret' => env('S3_SECRET',''),
        'region' => env('S3_REGION',''),
        'bucket' => 'yetAnotherBucketName',
    ],
]

Then whenever you want to access the bucket of your choice call it like so:

然后,每当您想访问您选择的存储桶时,请像这样调用它:

Storage::disk('s3')->put($fileName, $data);
Storage::disk('s3MyOtherBucketName')->put($anotherFileName, $moreData);
Storage::disk('s3YetAnotherBucketName')->put($yetAnotherFileName, $evenMoreData);

回答by Mario Campa

If you have dynamic buckets you also can create a new instance like this:

如果您有动态存储桶,您还可以像这样创建一个新实例:

$storage = Storage::createS3Driver([
    'driver' => 's3',
    'key'    => 'your-key',
    'secret' => 'your-secret',
    'region' => 'us-east-1',
    'bucket' => $bucketName,
]);

$storage->put('path/to/file.png', $content);

回答by Wayne Travers

You can add the buckets to the filesystems config like so:

您可以像这样将存储桶添加到文件系统配置中:

'disks' => [
    's3' => [
        'bucket1' => [
            'driver' => 's3',
            'key' => env('AWS_BUCKET1_ACCESS_KEY_ID'),
            'secret' => env('AWS_BUCKET1_SECRET_ACCESS_KEY'),
            'region' => env('AWS_BUCKET1_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET1_BUCKET'),
            'url' => env('AWS_BUCKET1_URL'),
        ],
        'bucket2' => [
            'driver' => 's3',
            'key' => env('AWS_BUCKET2_ACCESS_KEY_ID'),
            'secret' => env('AWS_BUCKET2_SECRET_ACCESS_KEY'),
            'region' => env('AWS_BUCKET2_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET2_BUCKET'),
            'url' => env('AWS_BUCKET2_URL'),
        ],
    ],
],

Then you can access the server using the following:

然后您可以使用以下命令访问服务器:

\Storage::disk('s3.bucket1')->put('path/to/file.png', $content);