如何使用流 aws s3 php 在文件末尾写入?

How to write in the end of a file using stream aws s3 php?

我正在使用上面的代码写入文件:

require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$client = S3Client::factory(array(
    'credentials' => array(
        'key'    => 'key',
        'secret' => 'secret',
    )
)); 
$client->registerStreamWrapper();
$stream = fopen("s3://bucket-name/files/filename.txt", 'w');
fwrite($stream, $message);
fclose($stream);

它有效,但我需要在文件末尾写入以保留已写入的内容...我认为使用“w+”是解决方案,但没有, 请问我该怎么做?

谢谢。

根据 php 手册 (fopen):

Mode W

Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

因此,如果您想从文件末尾开始写入, 你应该使用模式 a.

Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.