基本的AWS S3 PHP设置

布赖斯杰尔

我已经尝试了一段时间,只是设置了上传表单的基本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'
));

最近能够做到这一点的任何人都可以对此处的设置有所了解吗?

克里斯

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

您可以像上一个示例一样提供凭据配置文件,直接(通过密钥和机密)指定访问密钥,或者如果您将EC2实例使用AWS Identity and Access Management(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'
));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章