基本 AWS S3 PHP 设置

Basic AWS S3 PHP setup

我已经尝试了一段时间来设置一个基本的 PHP 上传表单实现以上传到 Amazon 的 S3 服务,但我什么都做不了。

通读他们的文档,他们的例子似乎都不一样。 提供凭据和上传文件的正确方法是什么?

在他们的 github 回购协议中,它说:

// Require the Composer autoloader.
require 'vendor/autoload.php';

use Aws\S3\S3Client;

// Instantiate an Amazon S3 client.
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-west-2'
]);

try {
    $s3->putObject([
        'Bucket' => 'my-bucket',
        'Key'    => 'my-object',
        'Body'   => fopen('/path/to/file', 'r'),
        'ACL'    => 'public-read',
    ]);
} catch (Aws\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n";
}

http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html 他们说:

use Aws\S3\S3Client;

$client = S3Client::factory(array(
    'profile' => '<profile in your aws credentials file>'
));

// Upload an object by streaming the contents of a file
// $pathToFile should be absolute path to a file on disk
$result = $client->putObject(array(
    'Bucket'     => $bucket,
    'Key'        => 'data_from_file.txt',
    'SourceFile' => $pathToFile,
    'Metadata'   => array(
        'Foo' => 'abc',
        'Baz' => '123'
    )
));

// We can poll the object until it is accessible
$client->waitUntil('ObjectExists', array(
    'Bucket' => $this->bucket,
    'Key'    => 'data_from_file.txt'
));

最近能够做到这一点的任何人都可以阐明这里的设置吗?

您链接到的文档中的关键是这句话:

您可以像前面的示例一样提供您的凭据配置文件, 直接指定您的访问密钥(通过密钥和秘密),或者 您可以 如果您使用的是 AWS,请选择省略任何凭证信息 EC2 实例的身份和访问管理 (IAM) 角色

因此,如果您 运行 来自 EC2 实例,您只需省略任何凭证,它应该从与实例关联的角色中获取权限。

如果您不在 AWS 中 运行,则需要为用户 运行 创建 ~/.aws/config 代码,并创建一个类似于

[profile profile_name]
aws_access_key_id = key
aws_secret_access_key = secret
region = us-east-1

那么你只需要做:

$client = S3Client::factory(array(
  'profile' => 'profile_name'
));

我最终仅通过使用嵌入式凭据就让它工作了

<?php

date_default_timezone_set("America/Denver");

require "./aws/aws-autoloader.php";

use Aws\S3\S3Client;

$s3Client = S3Client::factory(array(
    'credentials' => array(
        'key'    => 'key',
        'secret' => 'secret',
    ),
    "region" => "us-east-1",
    "version" => "latest"
));

?>

安装作曲家 安装 PHP CLI 后,使用以下命令下载 Composer 安装程序脚本:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

使用 PHP 版本

编译 Composer

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

检查作曲家 composer

Install/download aws sdk 库

composer require aws/aws-sdk-php

php -d memory_limit=-1 composer.phar require aws/aws-sdk-php

示例代码下载文件


<?php
 require 'vendor/autoload.php';

use Aws\S3\S3Client;
$bucket ="sentimentanalysis2021";
$key ="KEY";
$secret ="SecretKEY";
$region ="ap-south-1";
$location = "/var/www/html/openpbx/record/temp/";
// Establish connection with DreamObjects with an S3 client.
$client = new Aws\S3\S3Client([
    'version'     => '2006-03-01',
    'region'      => $region,
    'endpoint'    => "https://s3.$region.amazonaws.com/",
        'credentials' => [
        'key'      => $key,
        'secret'   => $secret,
    ]
]); 
//var_dump($client);
//Download File
$file_s3 = null;
 $objects = $client->getPaginator('ListObjects', ['Bucket' => $bucket]);
    foreach ($objects as $listResponse) {
        $items = $listResponse->search("Contents[?ends_with(Key,'wav')]");
        foreach($items as $item) {
//            download($client,$item['Key'],$location);
        $client->getObject(array(
            'Bucket' => $bucket,
             'Key'    => $item['Key'],
                'SaveAs' => $location.$item['Key']
        ));
        $file_s3 =$location.$item['Key'];


        }
 }

//Read Files
$objects = $client->listObjectsV2([
        'Bucket' => $bucket,
]);
foreach ($objects['Contents'] as $object){
    echo "{$object['Key']}\t{$object['LastModified']}\n";
}

function download($client,$file,$location){
$client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => $file,
    'SaveAs' => $location.$file
));
}

// Create Folder
 /* abc is the folder name */
$client->putObject(array( 
                   'Bucket' => $bucket,
                   'Key'    => "abc/",
                   'Body'   => "",
                   'ACL'    => 'public-read'
                  ));


$file_Path = '/amol/20220516141901_919033_9999.wav';
$key = basename($file_Path);
try{
    $result = $client->putObject([
        'Bucket'     => $bucket,
        'Key'        => $key,
        'SourceFile' => $file_Path,
        'ACL'        => 'private',
    ]);
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}




?>