Php 如何使用Gaufrete在不使用捆绑包的情况下将图像上传并存储到S3中?
我找到了这个 它提供的代码示例是Php 如何使用Gaufrete在不使用捆绑包的情况下将图像上传并存储到S3中?,php,amazon-web-services,amazon-s3,Php,Amazon Web Services,Amazon S3,我找到了这个 它提供的代码示例是 <?php $amazon = new AmazonS3('myKey', 'mySecretKey', 'myToken'); $adapter = new Gaufrette\Adapter\AmazonS3($amazon, 'my_bucket'); $filesystem = new Gaufrette\Filesystem($adapter); if ( ! $filesystem->has('foo')) { $filesy
<?php
$amazon = new AmazonS3('myKey', 'mySecretKey', 'myToken');
$adapter = new Gaufrette\Adapter\AmazonS3($amazon, 'my_bucket');
$filesystem = new Gaufrette\Filesystem($adapter);
if ( ! $filesystem->has('foo')) {
$filesystem->write('foo', 'Some content');
}
echo $filesystem->read('foo');
您的示例编写了文本“某些内容”。
相反,你可以把你的图像内容
$imageData = file_get_contents('/tmpupload/picture.jpeg');
$filesystem->write('foo', $imageData);
您还应该将图像mime类型写入amazon/Gaufrete。
如果我没有弄错的话,这应该是可行的:
$filesystem->write('foo', $imageData, ['content-type' => 'image/jpeg']);
您可以将表示图像的变量替换为“某些内容”。你是如何得到这个图像的?如果它是由用户上传的,您可以在$\u文件中找到它,并且使用提供的临时文件名,您可以使用file\u get\u contents将其读取到PHP变量中。很抱歉。实际上,我最终不再使用Gaufrete了。我假设您的答案是正确的。对于0.2.x,您应该调用$filesystem->getAdapter()->setMetadata($filename,array('ContentType'=>$mimetype))
设置mime类型。这必须在写之前完成。