开发者

Is a subfolder in an S3 bucket an object?

开发者 https://www.devze.com 2023-03-15 09:23 出处:网络
I\'m trying to get the size of a subfolder in S3 using PHP (Codeigniter). If I specify a file in the folder, the data is returned, however if I specify the folder I get:

I'm trying to get the size of a subfolder in S3 using PHP (Codeigniter). If I specify a file in the folder, the data is returned, however if I specify the folder I get:

[NoSuchKey] The specified key does not exist.

Which I believe means it can't find the file/folder. Is a folder not an object you can get the size of?

I've used S3 browser for Windows and that lists the folders and their sizes.

Here's a snippet of the code I'm using:

$userdir = 'userdata/'.$row->name;
$userdata = $this->s3->getObject('bucket_name',$userdir,false);

foreac开发者_StackOverflow社区h ($userdata as $name => $val) {
    $totalSize += $val['size'];
}

$totalSize = ($totalSize / 1024 / 1024 / 1024);

Any ideas appreciated!

Thanks!


Folders don't really exist in S3. For example, let's say you create an object with a key of /folder1/folder2/mything.txt. Most of the tools that let you browse a bucket will show this as folder1 containing folder2 containing mything.txt.

While this is convenient and familiar because of the file systems we are used to, it is not the right way to think about S3 buckets and their contents. In S3, you simply have a single object in the bucket with a key of /folder1/folder2/mything.txt.

While I'm not familiar with PHP, the basic idea behind your code is solid. You need to iterate over all the items in a "folder" and total the contents to determine the size. Don't forget to iterate over the folders within your folders too.

0

精彩评论

暂无评论...
验证码 换一张
取 消