检查云文件中是否存在对象(PHP API)
我刚刚开始使用 Rackspace Cloud Files 的 PHP API。到目前为止一切顺利——但我将它用作穷人的内存缓存,存储序列化数据的键/值对。
我的应用程序尝试使用类似以下内容的键(API 语言中的“名称”)来获取现有的缓存对象:
$obj = $this->container->get_object($key);
问题是,如果该对象不存在,API 会抛出致命错误,而不是简单地返回 假
。通过 API 执行此操作的“正确”方法可能是执行 a
$objs = $this->container->list_objects();
,然后检查该列表中的 $key
值。然而,这似乎比仅仅从 get_object
请求返回 false
更耗费时间/CPU。
有没有办法在云文件中进行“搜索对象”或“检查对象是否存在”?
谢谢
I've just started working with the PHP API for Rackspace Cloud Files. So far so good-- but I am using it as sort of a poor man's memcache, storing key/value pairs of serialized data.
My app attempts to grab the existing cached object by its key ('name' in the API language) using something like this:
$obj = $this->container->get_object($key);
The problem is, if the object doesn't exist, the API throws a fatal error rather than simply returning false
. The "right" way to do this by the API would probably be to do a
$objs = $this->container->list_objects();
and then check for my $key
value in that list. However, this seems way more time/CPU intensive than just returning false
from the get_object
request.
Is there a way to do a "search for object" or "check if object exists" in Cloud Files?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我向他们发送了拉取请求,希望它能被包含在内。
https://github.com/rackspace/php-cloudfiles/pull/35
我的拉取请求包含一个示例,对于您来说,它类似于:
I sent them a pull request and hope it'll get included.
https://github.com/rackspace/php-cloudfiles/pull/35
My pull-request includes an example, for you it would be similar to this:
我有更通用的方法来检查对象是否存在:
I have more general way to check if object exists:
如果转储 $object,您将看到 content_length 为零。或者,最后修改的将是零长度字符串。
示例:
在转储的父对象深处,还会返回一个 404,但它是私有的,因此您需要进行一些黑客操作才能获取它。
要看到这一点,请执行以下操作:
您将在该对象内看到一个值为 404 的response_status
If you dump the $object, you'll see that content_length is zero. Or, last modified will be a zero length string.
Example:
There is also, deep in the dumped parent object, a 404 that will return, but it's private, so you'd need to some hackin' to get at it.
To see this, do the following:
You'll see inside that object a response_status that is 404
我知道我来晚了一点,但希望这会对将来的人有所帮助:您可以使用
objectExists()
方法来测试对象是否可用。使用像...
I know I'm a little late to the party, but hopefully this will help someone in the future: you can use the
objectExists()
method to test if an object is available.Use like...
如果您不想导入 opencloud 来执行此检查,您可以使用以下命令:
If you do not want to import opencloud to perform this check you can use the following: