首页 > 解决方案 > 我可以使用 aws-sdk 将文件从我的计算机上传到 aws s3 吗?

问题描述

我尝试使用 aws-sdk 将文件从本地上传到 aws s3,但出现错误:访问被拒绝。我附加了 AmazonS3FullAccess 策略。我正在使用终端运行代码。php upload.php

我的代码:

require 'aws/aws-autoloader.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

// Set Amazon s3 credentials
$client = S3Client::factory(
  array(
    'credentials' => array(
        'key'    => "mykey",
        'secret' => "myscret",
    ),   
    'region' => "ap-southeast-1",
    'version' => "latest"
  )
);

try {
  $client->putObject(array(
    'Bucket'=>'mybucket',
    'Key' => 'text.txt',
    'Body' => 'Hello, world!',
    'ACL'    => 'public-read',
    's3'=>'PutObject'
    // 'StorageClass' => 'REDUCED_REDUNDANCY'
  ));

} catch (S3Exception $e) {
  // Catch an S3 specific exception.
  echo $e->getMessage();
}

标签: amazon-s3

解决方案


推荐阅读