简体   繁体   English

AWS S3 PHP 2.0 - 如何将图像从计算机上传到存储桶?

[英]AWS S3 PHP 2.0 - How to upload an image from computer to a bucket?

I've been trying to figure out how to push an image to an S3 bucket using the new PHP 2.0 SDK. 我一直试图弄清楚如何使用新的PHP 2.0 SDK将图像推送到S3存储桶。 All I've found are tutorials for how to upload an image from your web server rather than from a local computer. 我发现的所有内容都是如何从Web服务器而不是本地计算机上传图像的教程。 Here is the code that I'm using 这是我正在使用的代码

$result = $s3->putObject(array(
    'Bucket' => $bucketname,
    'Key'    => $filename,
    'SourceFile' => $path,
    'ACL'    => 'public-read',
    'ContentType' => 'image/jpeg',
    'StorageClass' => 'STANDARD'
));

$filename is just the file name that I want to appear in the bucket and $path is the local path to the file on my computer. $filename只是我想要在存储桶中显示的文件名, $path是我计算机上文件的本地路径。 This puts a file onto the bucket but when I try to open the image, it just shows up as an empty screen with the no image thumb nail. 这会将文件放入存储桶,但是当我尝试打开图像时,它只会显示为空白屏幕,没有图像缩略图。 I checked and it seems to only be uploading like 30 bytes. 我查了一下,似乎只上传了30个字节。 Can someone please point me in the right direction? 有人可以指点我正确的方向吗?

So in order to upload you need to specify the Body as well. 因此,为了上传,您还需要指定Body。 So if you're uploading from your computer this would be the code 因此,如果您从计算机上传,这将是代码

$s3 = $aws->get('s3');
$filename = $_FILES['file']['name'];
$tmpFile = $_FILES['file']['tmp_name'];
$imageType = $_FILES['file']['type'];
// Upload a publicly accessible file.
// The file size, file type, and MD5 hash are automatically calculated by the SDK
try {
    $s3->putObject(array(
        'Bucket' => $bucketname,
        'Key' => $filename,
        'Body'   => fopen($tmpFile, 'r'),
        'ACL' => 'public-read',
        'ContentType' => $imageType,
        'StorageClass' => 'STANDARD'
    ));
} catch (S3Exception $e) {
    echo "There was an error uploading the file.\n";
}

where $tmpFile is the path to your file on your computer. 其中$ tmpFile是计算机上文件的路径。 I'm using an upload mechanism hence why i use temp. 我正在使用上传机制,因此我使用temp。 but you can just put the static path. 但你可以把静态路径。

I had the same problem and Shahin's answer is correct, you can set 'Body' => fopen($tmpFile, 'r'), 我有同样的问题,Shahin的答案是正确的,你可以设置'Body' => fopen($tmpFile, 'r'),

However when I did this on my WAMP localhost, I got the error 但是,当我在我的WAMP localhost上执行此操作时,我收到了错误消息

Warning: fopen(C:\\Windows\\Temp\\phpBDF3.tmp): failed to open stream: No such file or directory

This seems to be a windows permissions issue, and you can resolve it by copying the windows temp file to another temp file in a directory where there are no permissions problems (eg web root): 这似乎是一个Windows权限问题,您可以通过将Windows临时文件复制到没有权限问题的目录中的另一个临时文件(例如Web根目录)来解决它:

// $tmpFile was 'C:\Windows\Temp\php8A16.tmp';
// create a new temp file in webroot, and copy the windows tempfile there
$new_temp_location = tmpfile();
$new_temp_location = basename($tmpFile);
copy($tmpFile, $new_temp_location);

// now put the file in the bucket
try {
    $s3->putObject(array(
        'Bucket' => $bucketname,
        'Key' => $filename,
        'Body'   => fopen($new_temp_location, 'r'),
        'ACL' => 'public-read',
        'ContentType' => $imageType,
        'StorageClass' => 'STANDARD'
    ));
} catch (S3Exception $e) {
    echo "There was an error uploading the file.\n";
}

It worked for me - hope it saves someone else some time... 它对我有用 - 希望它能节省一些时间......

EDIT: 编辑:

Or slightly simpler, you can use 或者稍微简单一点,你可以使用

'SourceFile'   => $new_temp_location,

Instead of 代替

'Body'   => fopen($new_temp_location, 'r'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM