如何使用Laravel 5.5和Intervention Image从Amazon S3将图像文件上传到Stripe [英] How to upload image file to Stripe from Amazon S3 using Laravel 5.5 and Intervention Image

查看:53
本文介绍了如何使用Laravel 5.5和Intervention Image从Amazon S3将图像文件上传到Stripe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 5.5应用程序.我需要从Amazon S3检索驾驶执照的图像(已在工作),然后使用其api将其上传到Stripe进行身份验证(不起作用).

Laravel 5.5 app. I need to retrieve images of driver's licenses from Amazon S3 (already working) and then upload it to Stripe using their api for identity verification (not working).

条带的文档给出此示例:

\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')的调用.我尝试了以下方法:

However, I am not retrieving my files using fopen(). When I retrieve my image from Amazon S3 (using my own custom methods), I end up with an instance of Intervention\Image -- essentially, Image::make($imageFromS3) -- and I don't know how to convert this to the equivalent of the call to fopen('/path/to/a/file.jpg', 'r'). I have tried the following:

$image->stream()

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

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

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

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

I have also tried skipping intervention image and just using Laravel's storage retrieval, for example:

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

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

All of these approaches result in getting an Invalid hash exception from Stripe.

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

What is the proper way to get a file from S3 and convert it to the equivalent of the fopen() call?

推荐答案

如果S3上的文件是公共文件,则可以将URL传递给Stripe:

If the file on S3 is public, you could just pass the URL to 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 .

Note that this requires allow_url_fopen to be turned on in your php.ini file.

如果没有,那么您可以先从S3抓取文件,将其写入一个临时文件,然后使用Stripe文档所说的 fopen()方法:

If not, then you could grab the file from S3 first, write it to a temporary file, and then use the fopen() method that the Stripe documentation speaks of:

// 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/zh/function.tmpfile.php 了解更多信息.

这篇关于如何使用Laravel 5.5和Intervention Image从Amazon S3将图像文件上传到Stripe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆