【问题标题】:How to upload image file to Stripe from Amazon S3 using Laravel 5.5 and Intervention Image如何使用 Laravel 5.5 和 Intervention Image 从 Amazon S3 将图像文件上传到 Stripe
【发布时间】:2018-08-03 21:39:13
【问题描述】:

Laravel 5.5 应用程序。我需要从 Amazon S3 中检索驾照图像(已经可以使用),然后使用他们的 api 将其上传到 Stripe 以进行身份​​验证(无法使用)。

Stripe's documents 举个例子:

\Stripe\Stripe::setApiKey(PLATFORM_SECRET_KEY);
\Stripe\FileUpload::create(
    array(
        "purpose" => "identity_document",
        "file" => fopen('/path/to/a/file.jpg', 'r')
    ),
    array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID)
);

但是,我没有使用 fopen() 检索我的文件。 当我从 Amazon S3 检索我的图像(使用我自己的自定义方法)时,我最终得到了一个 Intervention\Image 的实例——本质上,Image::make($imageFromS3)——我不知道如何将其转换为等效的致电fopen('/path/to/a/file.jpg', 'r')。我尝试了以下方法:

$image->stream()

$image->stream()->__toString()

$image->stream('data-url')

$image->stream('data-url')->__toString()

我也尝试过跳过干预图像,只使用 Laravel 的存储检索,例如:

$image = Storage::disk('s3')->get('path/to/file.jpg');

所有这些方法都会导致从 Stripe 获得 Invalid hash 异常。

从 S3 获取文件并将其转换为等效于 fopen() 调用的正确方法是什么?

【问题讨论】:

    标签: php laravel laravel-5 stripe-connect intervention


    【解决方案1】:

    如果 S3 上的文件是公开的,您只需将 URL 传递给 Stripe:

    \Stripe\FileUpload::create(
        array(
            "purpose" => "identity_document",
            "file" => fopen(Storage::disk('s3')->url($file)),
        ),
        array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID)
    );
    

    请注意,这需要在您的 php.ini 文件中打开 allow_url_fopen

    如果没有,那么您可以先从 S3 获取文件,将其写入临时文件,然后使用 Stripe 文档中提到的 fopen() 方法:

    // Retrieve file from S3...
    $image = Storage::disk('s3')->get($file);
    
    // Create temporary file with image content...
    $tmp = tmpfile();
    fwrite($tmp, $image);
    
    // Reset file pointer to first byte so that we can read from it from the beginning...
    fseek($tmp, 0);
    
    // Upload temporary file to S3...
    \Stripe\FileUpload::create(
        array(
            "purpose" => "identity_document",
            "file" => $tmp
        ),
        array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID)
    );
    
    // Close temporary file and remove it...
    fclose($tmp);
    

    更多信息请参见https://secure.php.net/manual/en/function.tmpfile.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 2014-10-24
      • 2013-10-03
      • 1970-01-01
      • 2017-04-28
      • 2012-02-02
      • 2020-10-02
      相关资源
      最近更新 更多