简体   繁体   中英

How to get folder size from folder inside Rackspace Cloud Files container using PHP?

Basically I have created a container inside Rackspace Cloud Files, lets say work . Inside work I have so many folders by username (dynamic folders as new users register and allocate 1 GB of space). Every time a user uploads a file, I want to check whether the user exceeds his limit or not before allowing him to upload. I am using the Laravel PHP Framework along with Rackspace open cloud SDK.

Questions:

  1. I want to get folder size of particular user's folder created under main container("xyz").

  2. Is there any other way to achieve whether user has exceed limit or not?

  1. I want to get folder size of particular user's folder created under main container("xyz").

When performing a container listing, you can provide a "prefix" and "format" query parameter to only return objects that begin with a specific prefix (in your case, the username), and return the results in the format JSON/XML. When results are returned in JSON or XML, they include additional information about the objects. The following is an example JSON response:

[
    {
        "bytes": 13,
        "content_type": "text/plain",
        "hash": "8ddd8be4b179a529afa5f2ffae4b9858",
        "last_modified": "2015-01-27T00:08:44.671230",
        "name": "<username>/some_object.txt"
    },
    ...
]

NOTE: With this solution, if you get back 10k objects, you will want to look into using the “marker” query parameter to page your results.

Additionally you might consider sharding your users into multiple containers. We recommend doing this to help with getting back more accurate results as far as the eventual consistency window (more on this later) is concerned. For example, create containers “work_0” through “work_9999” and determine which users go to which containers with something similar to the following:

$user_md5 = md5($username);
// Only use part of the md5sum, unless you have support for big (128-bit) integers
$user_int = hexdec(str_split($user_md5, 7)[0]);
$container_id =  $user_int % 10000;
$container_name = "work_$container_id";
print "Container for user \"$username\" is \"$container_name\"\n";
  1. Is there any other way to achieve whether user has exceed limit or not.

If you have restrictions on how quickly your users will grow, or can limit the number of total users, then you might consider creating a container per user. The downside to this, is you will limit the number of users you can support by the maximum number of containers per account (500,000). But it would allow you to use the container quota feature. Using this feature, if you attempt to PUT an object and are over the quota, you will receive a HTTP 413 response. You would also be able to check a user's usage with the "X-Container-Bytes-Used" header.

NOTE: I previously mentioned a “eventual consistency window”. In some cases, container listings/quotas might not update immediately, but they are eventually updated. In most situations this latency is either unnoticeable or minimal. However, if your use case requires that users absolutely are not allowed to upload beyond their quota, then you might have to look for an alternate method for tracking the user's usage (for example a local database).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM