2

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' => "",
    ));
KPM
  • 181
  • 4
  • 23

4 Answers4

6

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.

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’);
Piyush Patil
  • 14,512
  • 6
  • 35
  • 54
  • 2
    "So a work around will be put a empty txt file and delete it after wards but the folder structure will stay." This is not true. If no folder was created, none can persist after the object is deleted. – Matt Houser Aug 16 '16 at 01:38
  • @MattHouser Here when he put a empty txt file at "/folder/test.txt" folder is created and when he deletes "test.txt" the folder remains. – Piyush Patil Aug 16 '16 at 01:42
  • Is that a built-in side effect of the PHP client `putObjectFile` method? It's not standard behaviour for the AWS APIs. `putObjectFile` is a legacy method. Will the PHP `putObject` method, the modern version, do it too? – Matt Houser Aug 16 '16 at 01:54
  • Is the security info required to connect to the S3 bucket, just the username and password I use to log in through the console? The docs aren't very clear about this. Thanks for the answer. – KPM Aug 16 '16 at 02:14
  • 1
    @KPM You need an access key and secret key or use IAM roles (if running on an EC2 instance): http://docs.aws.amazon.com/aws-sdk-php/v2/guide/credentials.html – Matt Houser Aug 16 '16 at 02:18
6

Amazon S3 does not have a concept of folders. For S3, all objects are simply a key name with data.

Folders are a human concept which use the '/' character to separate the folders. But S3 does not care.

When you use many third-party tools (and even the AWS Management Console), the tools often will look at the object keys under your prefix and when it sees a '/' in it, it will interpret it as a folder.

But there's no way to "create a folder".

If you you simply PutObject an object with a key with your desired full path (for example, "my/desired/folder/structure/file.txt"), Amazon S3 will put it there. It's not like many filesystems where the folder must exist before a file can be created.

The closest thing to "creating a folder" you could do is to create a 0-byte object with a '/' at the end of it's key. For example "my/desired/folder/structure/". But it will just be another object in the bucket. It won't have any effect on the creation or operation of the bucket or any other objects in the bucket.

Matt Houser
  • 33,983
  • 6
  • 70
  • 88
4

Amazon S3 doesn't really have directories:

In Amazon S3, buckets and objects are the primary resources, where objects are stored in buckets. Amazon S3 has a flat structure with no hierarchy like you would see in a typical file system. However, for the sake of organizational simplicity, the Amazon S3 console supports the folder concept as a means of grouping objects. Amazon S3 does this by using key name prefixes for objects.

Instead, it fakes it based on the name of an object's key. Just upload an object using a key like some/directory/file.txt and many tools, including the S3 interface in the AWS console, will act as if you have an object called file.txt in a directory called directory in a directory called some.

See also Amazon S3 boto - how to create a folder?

Community
  • 1
  • 1
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
0
$client->putObject([
  'Bucket' => 'bucket',
  'Key' => 'folder/', ]);

For 'version' => '2006-03-01',

icalvete
  • 987
  • 2
  • 16
  • 50