【发布时间】:2016-04-29 21:08:07
【问题描述】:
我一直在尝试设置一个上传表单的基本 PHP 实现以上传到 Amazon 的 S3 服务,但我什么也做不了。
阅读他们的文档,他们的示例似乎都不同。 提供凭据和上传文件的正确方法是什么?
在他们的 github repo 上,它说:
// 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'
));
最近能够做到这一点的任何人都可以解释一下这里的设置吗?
【问题讨论】:
-
您是否遇到任何错误?
-
我一直使用这个指南。 docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html 还要确保你安装了 cURL 扩展。
标签: php amazon-web-services amazon-s3 amazon