使用 Laravel 5.3 在 S3 中上传文件

Upload file in S3 using Laravel 5.3

安装过程

I followed this tutorial to install aws Package in Laravel 5.3

我的代码在下面

$s3 = \App::make('aws')->createClient('s3');

$s3->putObject(array(
    'Bucket'     => 'Bucket_Name',
    'Key'        => 'AWS_ACCESS_KEY_ID',
    'SourceFile' => 'http://domainname/sample.txt',
));

我正在尝试一个包含大约 50 个字节内容的 txt 文件,但出现以下错误。

A sha256 checksum could not be calculated for the provided upload body, because it was not seekable. To prevent this error you can either 1) include the ContentMD5 or ContentSHA256 parameters with your request, 2) use a seekable stream for the body, or 3) wrap the non-seekable stream in a GuzzleHttp\Psr7\CachingStream object. You should be careful though and remember that the CachingStream utilizes PHP temp streams. This means that the stream will be temporarily stored on the local disk.

我是不是漏掉了什么?

SourceFile 必须是本地文件路径。 Body 参数允许流,因此您应该能够使用 guzzle 发出请求并将正文传递给它。

$client = new GuzzleHttp\Client();
$response = $client->get('http://domainname/sample.txt');

$s3->putObject([
    'Bucket' => 'Bucket_Name',
    'Key' => 'AWS_ACCESS_KEY_ID',
    'Body' => $response->getBody(),
]);