2016-04-29 91 views
0

我一直在尝试一段时间来设置上传到亚马逊S3服务的上传表单的基本PHP实现,但我无法获得任何工作。基本的AWS S3 PHP设置

通过阅读他们的文档,他们的例子看起来都不一样。 提供凭证并上传文件的正确方法是什么?

在他们的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' 
)); 

人谁已经能够最近这样做可以阐明这里设置一些轻?

+0

您是否收到任何电子邮件rrors? –

+0

我一直使用这个指南。 http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html同时确保你已经安装了cURL扩展。 – Vector

回答

1

从您连这句话的文档的关键:

您可以提供凭证的个人资料,如前面的例子中, 直接指定您的访问密钥(通过密钥和密码),或者你可以选择 如果您正在使用AWS 身份和访问管理(IAM)的EC2实例

因此,如果你从EC2实例中运行这个,你简单的省略了任何提及角色省略任何凭证信息证书,它应该从与实例关联的角色中获取权限。

如果你没有在AWS上运行,你需要创建〜/ .aws /配置为运行代码的用户,并创建一个配置文件,看起来像

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

,那么你会只是做:

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

我最终得到它的工作只是通过使用嵌入式凭据

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

?>