【问题标题】:Uploading file to specific folder in s3 bucket, sdk php [duplicate]将文件上传到s3存储桶中的特定文件夹,sdk php [重复]
【发布时间】:2020-01-30 03:46:51
【问题描述】:

我在下面有这段代码,应该将图像上传到我的 s3 存储桶,但我不知道如何告诉它上传到我的 s3 存储桶中的特定文件夹(名为 videouploads)。有人可以帮忙吗?

require 'vendor/autoload.php';

use Aws\S3\S3Client;

// Instantiate an Amazon S3 client.
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'grabage',
    'credentials' => [
        'key'    => 'garbage',
        'secret' => 'grabage'
    ]
]);


$bucketName = 'cgrabage';
$file_Path = __DIR__ . '/my-image.png';
$key = basename($file_Path);

// Upload a publicly accessible file. The file size and type are determined by the SDK.
try {
    $result = $s3->putObject([
        'Bucket' => $bucketName,
        'Key'    => $key,
        'Body'   => fopen($file_Path, 'r'),
        'ACL'    => 'public-read',
    ]);
    echo $result->get('ObjectURL');
} catch (Aws\S3\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n";
    echo $e->getMessage();
}

【问题讨论】:

  • 这能回答你的问题吗? PutObject into directory Amazon s3 / PHP
  • 是的,这在某种程度上确实回答了我的问题,但现在我收到了那个错误,指出 an error uploading the file. Error executing "PutObject" on "https://cactustestphp.s3.us west.amazonaws.com/videouploads/my-image.png"; AWS HTTP error: cURL error 3: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

标签: php amazon-web-services sdk


【解决方案1】:

你需要把目录信息放在key中。

$result = $s3->putObject([
    'Bucket' => $bucketName,
    'Key'    => 'videouploads/' . $key,
    'Body'   => fopen($file_Path, 'r'),
    'ACL'    => 'public-read',
]);

【讨论】:

  • 由于某种原因,每次我尝试运行代码时,都会出现错误提示:There was an error uploading the file. Error executing "PutObject" on "https://cactustestphp.s3.us west.amazonaws.com/videouploads/my-image.png"; AWS HTTP error: cURL error 3: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) It says wrong url,但这究竟是什么意思?我的钥匙错了吗?
  • 没关系,我知道如何解决我的问题。不过你的回答是正确的,谢谢!
  • 很高兴您找到了解决方案 :)
猜你喜欢
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 2012-05-28
  • 2022-01-11
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多