PHP 如何在 aws s3 存储桶中创建文件夹?

标签 php amazon-s3 directory storage

请帮帮我!我正在使用 aws-php-sdk 在 aws s3 服务器上上传文件。我能够将文件夹从我们的服务器移动到 aws s3 服务器并可以删除文件,但无法创建目录/ 我总是收到访问被拒绝的错误。

错误是

Error executing "PutObject" on "https://s3.amazonaws.com/***/***/***/picture/"; AWS HTTP error: Client error: `PUT https://s3.amazonaws.com/***/****/****/picture/` resulted in a `403 Forbidden` response: <?xml version="1.0" encoding="UTF-8"?> <Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>CE24E8 (truncated...) AccessDenied (client): Access Denied - <?xml version="1.0" encoding="UTF-8"?> <Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>CE24E8FFC9216978</RequestId><HostId>DHdN6tcopPTP9IsVu/qGWClf1+hAoWv7CNUWJRsOPWia4SfQYQ+VPYgJ3+vlqqRBNbYWB34gEQ0=</HostId></Error>


and here is my code

  $s3Client = new Aws\S3\S3Client([
            'version' => AWS_VERSION,
            'region' => AWS_REGION,
            'credentials' => [
                'key' => AWS_KEY,
                'secret' =>AWS_SECRET,
            ],
        ]);


//code to create the directory

        $s3Client->putObject(array(
            'Bucket' => AWS_BUCKET, // Defines name of Bucket
            'Key' => "picture/", //Defines Folder name
            'Body' => "",
            'ACL' => 'public-read' // Defines Permission to that folder
        ));

//I have also used along with full AWS_PATH, that is my uploading direcctory path on the aws server but none of them is working always gets the same error.

       $s3Client->putObject(array(
            'Bucket' => AWS_BUCKET, // Defines name of Bucket
            'Key' => AWS_PATH . "picture/", //Defines Folder name
            'Body' => "",
            'ACL' => 'public-read' // Defines Permission to that folder
        ));

最佳答案

我自己想出了解决方案。 实际上,在创建新目录时,我无法授予新目录权限,因为我不是存储桶的所有者。我使用存储桶作为第三方。因此,我必须从代​​码中删除 'ACL'=>'public-read',现在我的代码是

 $s3Client->putObject(array(
            'Bucket' => AWS_BUCKET, // Defines name of Bucket
            'Key' => AWS_PATH . "picture/", //Defines Folder name
            'Body' => "",
        ));

关于PHP 如何在 aws s3 存储桶中创建文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765882/

相关文章:

php - 选择选项已选择未选择

hadoop - 使用 S3AFileSystem 的 Flink 不会从 S3 读取子文件夹

amazon-web-services - 如何从 AWS S3 "ObjectRemoved:Delete"事件获取文件大小

linux - 如何从绝对路径和包文件创建 tar 存档,就好像它们是相对的一样?

php - 使用 php 将 html 连接到 mysql - 全新的 php

php - Laravel 5 项目导致 PHP/Apache 崩溃

javascript - 使用 PHP 登录 Google+

ios - 使用 iOS SDK 将文件上传到 AWS S3 时空白自定义元数据

windows - 如何通过批处理文件列出所有大小的文件夹

java - 在Java中递归地解压缩不同文件夹中的多个gzip文件