0

如何正确设置 fopen?由于我不知道初始文件位置,因此在尝试上传时出现 500 错误。

  • 我需要使用分段上传来优化传输速度。
  • 我的网站将是无服务的,并且将在 Vapor 上运行,因此在上传到 S3 之前我无法将文件保存到本地。
  • 我想将文件流式上传到 S3 存储桶,文件是使用表单选择的,我不知道它们在客户端计算机上的初始本地路径……AWS 文档上显示了一些替代方案,但他们都假设你会知道已经是文件的初始位置...
$s3Client = new S3Client([
    'profile' => 'default',
    'region' => 'us-east-2',
    'version' => 'latest'
]);

$bucket = 'your-bucket';
$key = 'my-file.zip';

// Using stream instead of file path
$source = fopen('/path/to/large/file.zip', 'rb');

$uploader = new ObjectUploader(
    $s3Client,
    $bucket,
    $key,
    $source
);

do {
    try {
        $result = $uploader->upload();
        if ($result["@metadata"]["statusCode"] == '200') {
            print('<p>File successfully uploaded to ' . $result["ObjectURL"] . '.</p>');
        }
        print($result);
    } catch (MultipartUploadException $e) {
        rewind($source);
        $uploader = new MultipartUploader($s3Client, $source, [
            'state' => $e->getState(),
        ]);
    }
} while (!isset($result));

fclose($source);
4

0 回答 0