设置内容类型将照片上传到S3存储桶

时间:2020-02-09 19:20:46

标签: php amazon-web-services amazon-s3

我正在尝试将照片上传到我的S3存储桶。正在上传的文件已成功将其添加到我的S3存储桶中。问题?是的,它采用“应用程序/八位字节流”的形式。鉴于以下实施方式,我需要进行哪些更改以及在哪里进行更改?

index.php

<?php
require 's3.php';

$s3 = new S3('AWS_ACCESS_KEY_ID','AWS_SECRET KEY', 'region: us-west-2');

if(isset($_FILES['file'])){

        $file = $_FILES['file'];

        $name = $file['name'];
        $tmp_name = $file['tmp_name'];
        var_dump($tmp_name);

        $extension = explode('.', $name);
        $extension = strtolower(end($extension));
        // var_dump($extension);

        $key = md5(uniqid());
        $tmp_file_name = "{$key}.{$extension}";
        $tmp_file_path = "files/{$tmp_file_name}";

        move_uploaded_file($tmp_name, $tmp_file_path);

        try{
            S3::putObject(
            $tmp_file_path,
            'mazzo-php-app',
            $tmp_file_name,
            S3::ACL_PUBLIC_READ,
            array(),
            array() 
            );
            unlink($tmp_file_path);
        } catch(S3Exception $e){
            die("There was an error uploading the file.");
        }
    }
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Upload</title>
    </head>
    <body>
        <form action="index.php" method="post" enctype="multipart/form-data">
            <input type="file" name="file">
            <input type="submit" value="Upload">
        </form>
    </body>
</html>

s3.php

// Content-Type
if (!isset($input['type']))
        {
            if (isset($requestHeaders['Content-Type']))
                $input['type'] =& $requestHeaders['Content-Type'];
            elseif (isset($input['file']))
                $input['type'] = self::__getMIMEType($input['file']);
            else
                $input['type'] = 'application/octet-stream';
        }

1 个答案:

答案 0 :(得分:1)

putObject() documentation显示一个ContentType字段:

$result = $client->putObject([
    'ACL' => 'private|public-read|public-read-write|authenticated-read|aws-exec-read|bucket-owner-read|bucket-owner-full-control',
    'Body' => <string || resource || Psr\Http\Message\StreamInterface>,
    'Bucket' => '<string>', // REQUIRED
    'ContentType' => '<string>',
    ...
]);

ContentType::描述内容格式的标准MIME类型。

相关问题