1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

S3ストレージへのアクセスをローカル環境で検証する

Posted at

laravel のファイルストレージを利用してminioに接続し
ローカル検証をしてみた記録です

環境

mac
laravel sailを利用したプロジェクトが作成済み

準備

  1. S3ドライバを利用するために以下パッケージをインストール

    https://laravel.com/docs/12.x/filesystem#s3-driver-configuration

    composer require league/flysystem-aws-s3-v3 "^3.0" --with-all-dependencies
    
  2. 環境変数定義
    .envファイルに追記

    AWS_ACCESS_KEY_ID=minioadmin
    AWS_SECRET_ACCESS_KEY=minioadmin
    AWS_DEFAULT_REGION=us-east-1
    AWS_BUCKET=<バケット名>
    AWS_URL=http://localhost:9000
    AWS_ENDPOINT=http://<コンテナ名>:9000
    AWS_USE_PATH_STYLE_ENDPOINT=true
    
  3. 対象のサービスは追加でコンテナを立てておく

    docker run -p 9000:9000 -p 9001:9001 -e MINIO_ROOT_USER=minioadmin -e MINIO_ROOT_PASSWORD=minioadmin --network=<プロジェクト名>_sail -v minio_data:/data --name <コンテナ名> minio/minio server --console-address ":9001" /data
    

    一度コンテナ削除したあと、docker-compose.ymlに追記して再起動しても同様です

    services:
    +    minio:
    +     image: minio/minio
    +     command: server --console-address ":9001" /data
    +     ports:
    +         - "9000:9000"
    +         - "9001:9001"
    +     environment:
    +         MINIO_ROOT_USER: minioadmin
    +         MINIO_ROOT_PASSWORD: minioadmin
    +     volumes:
    +         - minio_data:/data
    volumes:
    +    minio_data:
    
  4. バケットの作成
    1. http://localhost:9001/login にアクセス
    2. http://localhost:9001/buckets から「Create Bucket」をクリック
    3. 環境変数に指定したAWS_BUCKETをバケット名に入力、保存
    image.png

検証

laravelで利用できるtinkerを利用しました

  1. laravel-app.test-1コンテナにログイン
    docker exec -it <プロジェクト名>-laravel.test-1 bash
    
  2. tinker起動
    php aritan tinker
    

接続

root@5c512732ee1e:/var/www/html# php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.11 — cli) by Justin Hileman
> Storage::disk('s3');
= Illuminate\Filesystem\AwsS3V3Adapter {#5868}

できていない場合

  • パッケージがインストールできていないので、一度exitしたあと、再度インストールします
> Storage::disk('s3');

   Error  Class "League\Flysystem\AwsS3V3\PortableVisibilityConverter" not found.

ファイル取得

root@5c512732ee1e:/var/www/html# php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.11 — cli) by Justin Hileman
> Storage::disk('s3')->allfiles();
= []

できていない場合
一度exitしたあと、環境変数を再設定します

  • バケットが未作成、またはAWS_BUCKETに指定したバケット名がない
root@5c512732ee1e:/var/www/html# php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.11 — cli) by Justin Hileman
> Storage::disk('s3')->allfiles();

   League\Flysystem\UnableToListContents  Unable to list contents for '', deep listing

Reason: Error executing "ListObjectsV2" on "http://example-app-minio:9000/testing/?list-type=2&prefix="; AWS HTTP error: Client error: `GET http://example-app-minio:9000/testing/?list-type=2&prefix=` resulted in a `404 Not Found` response:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>NoSuchBucket</Code><Message>The specified bucket does not exist</Mes (truncated...)
 NoSuchBucket (client): The specified bucket does not exist - <?xml version="1.0" encoding="UTF-8"?>
<Error><Code>NoSuchBucket</Code><Message>The specified bucket does not exist</Message><BucketName>testing</BucketName><Resource>/testing/</Resource><RequestId>186385284B625D66</RequestId><HostId>dd9025bab4ad464b049177c95eb6ebf374d3b3fd1af9251148b658df7ac2e3e8</HostId></Error>.
  • 環境変数AWS_ENDPOINTに指定しているコンテナ名が間違っている
root@5c512732ee1e:/var/www/html# php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.11 — cli) by Justin Hileman
> Storage::disk('s3')->allfiles();

   League\Flysystem\UnableToListContents  Unable to list contents for '', deep listing

Reason: Error executing "ListObjectsV2" on "http://minio:9000/test/?list-type=2&prefix="; AWS HTTP error: cURL error 6: Could not resolve host: minio (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://minio:9000/test/?list-type=2&prefix=.

ファイル登録

root@5c512732ee1e:/var/www/html# php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.11 — cli) by Justin Hileman
> $storage = Storage::disk('s3');
= Illuminate\Filesystem\AwsS3V3Adapter {#5868}

> $storage->put('test/1/dummy.txt','ダミーテキストです');
= true

ファイルの内容を取得

root@5c512732ee1e:/var/www/html# php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.11 — cli) by Justin Hileman
> $storage = Storage::disk('s3');
= Illuminate\Filesystem\AwsS3V3Adapter {#5868}

> $storage->allfiles();
= [
    "test/1/dummy.txt",
  ]

> $storage->get('test/1/dummy.txt');
= "<E3><83><80><E3><83><9F><E3><83><BC><E3><83><86><E3><82><AD><E3><82><B9><E3><83><88><E3><81><A7><E3><81><99>"

ファイルの最終更新日を取得

root@5c512732ee1e:/var/www/html# php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.11 — cli) by Justin Hileman
> $storage = Storage::disk('s3');
= Illuminate\Filesystem\AwsS3V3Adapter {#5868}

> $storage->lastModified('test/1/dummy.txt');
= 1757397803

ファイル情報を取得するあれこれ

ファイルタイプ

$storage = Storage::disk('s3');
$storage->mimeType($data)

ファイルサイズ

$storage = Storage::disk('s3');
$storage->size($data)

ファイルパス

$storage = Storage::disk('s3');
$storage->path($data)

すべて一気に取得

root@5c512732ee1e:/var/www/html# php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.11 — cli) by Justin Hileman
> $storage = Storage::disk('s3');
= Illuminate\Filesystem\AwsS3V3Adapter {#5868}

> collect($storage->allfiles())->map(function ($data) use ($storage) { return ['filename' => $data, 'contents' => $storage->get($data), 'mimetype' => $storage->mimeType($data), 'size' => $storage->size($data), 'path' => $storage->path($data), 'updated_at' =>  $storage->lastModified($data)];
});
= Illuminate\Support\Collection {#7858
    all: [
      [
        "filename" => "test/1/dummy.txt",
        "contents" => "<E3><83><80><E3><83><9F><E3><83><BC><E3><83><86><E3><82><AD><E3><82><B9><E3><83><88><E3><81><A7><E3><81><99>",
        "mimetype" => "text/plain",
        "size" => 27,
        "path" => "test/1/dummy.txt",
        "updated_at" => 1757397803,
      ],
      [
        "filename" => "test/1/dummy2.txt",
        "contents" => "<E3><83><80><E3><83><9F><E3><83><BC><E3><83><86><E3><82><AD><E3><82><B9><E3><83><88><E3><81><A7><E3><81><99>",
        "mimetype" => "text/plain",
        "size" => 27,
        "path" => "test/1/dummy2.txt",
        "updated_at" => 1757399452,
      ],
    ],
  }

参照

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?