基本的AWS S3 PHP设置


Basic AWS S3 PHP setup

我已经尝试了一段时间来设置上传表单的基本PHP实现,以上传到亚马逊的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";
}

Onhttp://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"
));
?>

安装Composer安装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

检查composercomposer

安装/下载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";
}


?>