如何使用PHP在S3存储桶中创建文件夹 [英] How to create a folder within S3 bucket using PHP
问题描述
我正在尝试在S3亚马逊存储桶中创建一个文件夹,但是我发现很难找到合适的文档来充分解释所需的内容.我有以下用于创建文件夹的代码/伪代码.任何人都可以解释或提供我需要放在代码中的参数示例
I'm trying to create a folder within an S3 amazon bucket but I'm finding it difficult to find the right documentation to adequately explain what is needed. I have the following code / pseudocode for creating a folder. Can anyone explain or provide a sample of the arguments I need to place within the code
use vendor\aws\S3\S3Client;
$bucket_url = 'https://***.amazonaws.com/***/';
$folder_name = $username . '/';
$s3Client = new vendor\aws\S3\S3Client([
'version' => AWS_VERSION,
'region' => AWS_REGION,
'credentials' => [
'key' => AWS_KEY,
'secret' =>AWS_SECRET,
],
]);
$s3Client->putObject(array(
'Bucket' => AWS_BUCKET, // Defines name of Bucket
'Key' => AWS_PATH . $folder_name, //Defines Folder name
'Body' => "",
));
推荐答案
S3的存储桶中没有文件夹,但是对象(文件)的名称可以带有/s(正斜杠),并且有一些方法可以检索基于允许您模拟目录列表的前缀.不过,这意味着您无法创建一个空文件夹.
S3 doesn't have folders beyond the bucket, but objects (files) can have /s (forward slashes) in their name, and there are methods to retrieve based on a prefix that allows you to emulate a directory-list. This means though, that you can't create an empty folder.
因此,解决方法将是放置一个空的txt文件,并在病房后将其删除,但文件夹结构将保持不变.
So a work around will be put a empty txt file and delete it after wards but the folder structure will stay.
/* function to upload empty test.txt file to subfolders /folder/ on S3 bucketname */
$s3->putObjectFile(‘test.txt’, ‘bucketname’, ‘/folder/test.txt’, S3::ACL_PUBLIC_READ);
/* function to delete empty test.txt file to subfolders /folder/ on S3 bucketname */
$s3->deleteObject(‘bucketname’, ‘/folder/test.txt’);
这篇关于如何使用PHP在S3存储桶中创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!