【问题标题】:How To upload base64 image in S3 bucket in Laravel 5.5如何在 Laravel 5.5 的 S3 存储桶中上传 base64 图像
【发布时间】:2018-03-15 15:45:00
【问题描述】:

我正在使用 laravel api 应用程序,我必须将 base64 编码图像上传到 AWS S3 存储桶。

我可以直接上传图片

$this->request->imageInput->store('public');//in local server
Storage::disk('s3')->put('FILENAME', file_get_contents('imageInput'));//in S3 server

如何将 base64 编码的图像上传到 AWS S3 存储桶并在我们可以获取图像信息的位置获得响应?

【问题讨论】:

    标签: php laravel file-upload amazon-s3 base64


    【解决方案1】:

    您可能需要使用以下方法解码此图像:

    $tmp = base64_decode($base64);
    

    然后将其存储在 amazon s3 上

    【讨论】:

    • 这真的是一个答案吗? @Vincent 我真的不明白你用这种答案的意图,你甚至没有回答这个问题,也没有提出解决方案。
    【解决方案2】:
    list($baseType, $image) = explode(';', $base64);
    list(, $image) = explode(',', $image);
    $image = base64_decode($image);
    
    $imageName = rand(111111111, 999999999) . '.png';
    $p = Storage::disk('s3')->put('filepath/' . $imageName, $image, 'public'); // old : $file
    

    关键是你需要具体给出文件名。

    适配器无法获取base64数据的真实路径。我想这是因为 base64 数据不是文件对象。

    【讨论】:

      【解决方案3】:
      $base64String= "base64 string";
      
      $image = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '',$base64String));
      
      $imageName = str_random(30) . '.png';
      
      $p = Storage::disk('s3')->put('' . $imageName, $image, 'public'); 
      
      $image_url = Storage::disk()->url($imageName);
      

      【讨论】:

        【解决方案4】:

        在 Laravel 5.7 的 S3 存储桶中上传 base64 图像

        确保您的项目已安装 Flysystem S3 驱动程序:

        composer require league/flysystem-aws-s3-v3
        

        在 config/filesystems.php 中

        'spaces' => [
          'driver' => 's3',
          'key' => env('DO_SPACES_KEY'),
          'secret' => env('DO_SPACES_SECRET'),
          'endpoint' => env('DO_SPACES_ENDPOINT'),
          'region' => env('DO_SPACES_REGION'),
          'bucket' => env('DO_SPACES_BUCKET'),
         ],
        

        在 .env 文件中

        DO_SPACES_KEY=[YOURKEY]
        DO_SPACES_SECRET=[YOURSECRET]
        DO_SPACES_ENDPOINT=https://nyc3.digitaloceanspaces.com
        DO_SPACES_REGION=nyc3
        DO_SPACES_BUCKET=[YOURBUCKET]
        

        在你的控制器文件中

        $image = request('image');//base64 string
        $file_path = 'product/'.str_random(30).time().'.jpg';
        Storage::disk('spaces')->put($file_path, base64_decode($image), 'public');
        return Storage::disk('spaces')->url($file_path);
        

        【讨论】:

          【解决方案5】:

          使用 Laravel 文件系统在 S3 中上传 base64 图像

          确保您的项目已安装 Flysystem S3 驱动程序:

          composer require league/flysystem-aws-s3-v3 ~1.0
          

          在 config/filesystems.php 中

          's3' => [
          'driver' => 's3',
          'key' => env('AWS_ACCESS_KEY_ID'),
          'secret' => env('AWS_SECRET_ACCESS_KEY'),
          'region' => env('AWS_DEFAULT_REGION'),
          'bucket' => env('AWS_BUCKET'),
          'url' => env('AWS_URL'),
          ],
          

          在 .env 文件中

          AWS_ACCESS_KEY_ID=[YOURKEY]
          AWS_SECRET_ACCESS_KEY=[YOURSECRETACCESSKEY]
          AWS_DEFAULT_REGION=[YOURREGION]
          AWS_BUCKET=[YOURBUKET]
          AWS_URL=[AWSURL]
          

          在你的控制器文件中

          存储门面用于与我们配置的 s3 磁盘进行交互。在控制器文件中,我们需要添加

          use Illuminate\Support\Facades\Storage;
          

          在你的方法中

          $image = $request->image;  // your base64 encoded
          $data = explode( ',', $image );
          $current_timestamp = Carbon::now()->timestamp;
          $imageName = rand().'jpg';
          $filenametostore='uploads/'. $imageName;
          Storage::disk('s3')->put($filenametostore, base64_decode($data[1]), 'public');
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-03-11
            • 1970-01-01
            • 1970-01-01
            • 2017-03-13
            • 1970-01-01
            • 2020-12-30
            • 2020-02-14
            • 2016-04-24
            相关资源
            最近更新 更多