php - 如何通过 PHP 访问 AWS S3?

标签 php amazon-s3

我根据 the getting started instructions found here 使用 Composer 安装适用于 PHP 的 AWS 开发工具包.我将它安装在我的 html 根目录中。我使用“AmazonS3FullAccess”的唯一许可创建了一个名为“ImageUser”的 IAM 用户并捕获了它的 key 。

Per the instructions here ,我创建了一个名为“credentials”的文件,如下所示:

[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY

是的,我用适当的键替换了那些大写单词。该文件位于 html 根目录的隐藏子目录“.aws”中。该文件的 UNIX 权限为 664。

我创建了这个简单的文件(在我的 html 根目录“t”的子目录中称为“test.php”)来测试将文件上传到 S3:

<?php
// Include the AWS SDK using the Composer autoloader.
require '../vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'testbucket';
$keyname = 'test.txt';

// Instantiate the client.
$s3 = S3Client::factory();

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname,
        'Body'   => 'Hello, world!',
        'ACL'    => 'public-read'
    ));

    // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}
?>

不幸的是,它在该行抛出一个 HTTP 错误 500:

$s3 = S3Client::factory();

是的,自动加载器目录是正确的。是的,桶存在。不,文件“test.txt”不存在。

根据上面提到的页面,“如果没有向 SDK 明确提供凭据或配置文件,并且没有在环境变量中定义凭据,但定义了凭据文件,则 SDK 将使用‘默认’配置文件。”即便如此,我也尝试在工厂声明中明确指定配置文件“默认”,只是为了获得相同的结果。

我做错了什么?

最佳答案

tldr;您有多种 AWS SDK 版本

  • 根据您在消息中提供的链接 (link),您已经安装了 php sdk v3
  • 根据您的示例,您使用 PHP sdk v2

v3 不知道 S3Client::factory 方法,所以这就是它向您抛出错误的原因。您可以继续检查您的链接以检查使用情况https://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/basic-usage.html .有几种获取s3客户端的方法

  1. 创建客户端 - 简单方法

    <?php
    // Include the SDK using the Composer autoloader
    require 'vendor/autoload.php';
    
    $s3 = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'us-east-1'
    ]);
    
  2. 创建客户端 - 使用 sdk 类

    // Use the us-west-2 region and latest version of each client.
    $sharedConfig = [
        'region'  => 'us-west-2',
        'version' => 'latest'
    ];
    
    // Create an SDK class used to share configuration across clients.
    $sdk = new Aws\Sdk($sharedConfig);
    
    // Create an Amazon S3 client using the shared configuration data.
    $s3 = $sdk->createS3();
    

一旦你有了你的客户端,你就可以使用你现有的代码(是的,这个是 v3)在 s3 上放置一个新对象,这样你就会得到类似的东西

<?php
// Include the AWS SDK using the Composer autoloader.
require '../vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'testbucket';
$keyname = 'test.txt';

// Instantiate the client.

-- select method 1 or 2 --

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname,
        'Body'   => 'Hello, world!',
        'ACL'    => 'public-read'
    ));

    // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

关于php - 如何通过 PHP 访问 AWS S3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39420302/

相关文章:

javascript - 第二个ajax调用不触发

javascript - 使用 AJAX 通过 GET 传递唯一值

javascript - 如何在 jQuery 中添加两个整数值并以 $ amount 输出

amazon-s3 - Errors::SignatureDoesNotMatch,AWS-SDK gem 在 paperclip 3.0.1 和 rails 3.2 上支持 S3

ruby-on-rails - rails Carrierwave 和 s3 : wrong number of arguments (2 for 1)

php - MYSQL/PDO PHP系统---寻找大笔画输入

php - 如何使用多个值和 SELECT 语句插入?

amazon-web-services - AWS S3 Java SDK 上传失败,Groovy 中出现 "Connection pool shutdown"

Android Amazon S3 上传视频卡住等待

python - django boto amazon s3 中的键有哪些可用属性