使用SDK创建存储桶时,在AWS中启用网站托管

时间:2018-03-13 08:14:31

标签: php amazon-web-services amazon-s3

我使用的是AWS SDK,我可以创建存储桶并操纵密钥。在创建存储桶时,我还想为网站托管启用它。

这就是我用于创作的内容

    $result = $s3->createBucket([
        'Bucket' => $buck_name
    ]);

根据我的发现,这是我们添加网站配置的方式

$result = $s3->putBucketWebsite(array(
    'Bucket'        => $buck_name,    
    'IndexDocument' => array('Suffix' => 'index.html'),
    'ErrorDocument' => array('Key' => 'error.html'),
));

但这不是启用网站托管,我也上传了两个文件(索引和错误)以防万一。但是我收到了这个错误

InvalidArgumentException: Found 1 error while validating the input provided for the PutBucketWebsite operation: [WebsiteConfiguration] is missing and is a required parameter in 

1 个答案:

答案 0 :(得分:0)

试试这种方式

use Aws\S3\S3Client;

$bucket = $buck_name;

// 1. Instantiate the client.
$s3 = S3Client::factory();

// 2. Add website configuration.
$result = $s3->putBucketWebsite(array(
    'Bucket'        => $bucket,    
    'IndexDocument' => array('Suffix' => 'index.html'),
    'ErrorDocument' => array('Key' => 'error.html'),
));

// 3. Retrieve website configuration.
$result = $s3->getBucketWebsite(array(
    'Bucket' => $bucket,
));
echo $result->getPath('IndexDocument/Suffix');