检查云文件中是否存在对象(PHP API)

发布于 2024-11-29 04:57:25 字数 530 浏览 0 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

差↓一点笑了 2024-12-06 04:57:25

我向他们发送了拉取请求,希望它能被包含在内。

https://github.com/rackspace/php-cloudfiles/pull/35

我的拉取请求包含一个示例,对于您来说,它类似于:

$object = new CF_Object($this->container, 'key');
if ($object->exists() === false) {
    echo "The object '{$object->name}' does not exist.";
}

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:

$object = new CF_Object($this->container, 'key');
if ($object->exists() === false) {
    echo "The object '{$object->name}' does not exist.";
}
☆獨立☆ 2024-12-06 04:57:25

我有更通用的方法来检查对象是否存在:

    try {
        $this->_container->get_object($path);
        $booExists = true;
    } catch (Exception $e) {
        $booExists = false;
    }

I have more general way to check if object exists:

    try {
        $this->_container->get_object($path);
        $booExists = true;
    } catch (Exception $e) {
        $booExists = false;
    }
凉栀 2024-12-06 04:57:25

如果转储 $object,您将看到 content_length 为零。或者,最后修改的将是零长度字符串。

示例:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->content_length);

在转储的父对象深处,还会返回一个 404,但它是私有的,因此您需要进行一些黑客操作才能获取它。

要看到这一点,请执行以下操作:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->container->cfs_http);

您将在该对象内看到一个值为 404 的response_status

[response_status:CF_Http:private] => 404

If you dump the $object, you'll see that content_length is zero. Or, last modified will be a zero length string.

Example:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->content_length);

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:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->container->cfs_http);

You'll see inside that object a response_status that is 404

[response_status:CF_Http:private] => 404
愛上了 2024-12-06 04:57:25

我知道我来晚了一点,但希望这会对将来的人有所帮助:您可以使用 objectExists() 方法来测试对象是否可用。

public static function getObject($container, $filename, $expirationTime = false)
{
    if ($container->objectExists($filename)) {

        $object = $container->getPartialObject($filename);

        // return a private, temporary url
        if ($expirationTime) {
            return $object->getTemporaryUrl($expirationTime, 'GET');
        }

        // return a public url
        return $object->getPublicUrl();
    }

    // object does not exist
    return '';
}

使用像...

// public CDN file
$photo = self::getObject($container, 'myPublicfile.jpg');

// private file; temporary link expires after 60 seconds
$photo = self::getObject($container, 'myPrivatefile.jpg', 60);

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.

public static function getObject($container, $filename, $expirationTime = false)
{
    if ($container->objectExists($filename)) {

        $object = $container->getPartialObject($filename);

        // return a private, temporary url
        if ($expirationTime) {
            return $object->getTemporaryUrl($expirationTime, 'GET');
        }

        // return a public url
        return $object->getPublicUrl();
    }

    // object does not exist
    return '';
}

Use like...

// public CDN file
$photo = self::getObject($container, 'myPublicfile.jpg');

// private file; temporary link expires after 60 seconds
$photo = self::getObject($container, 'myPrivatefile.jpg', 60);
情释 2024-12-06 04:57:25

如果您不想导入 opencloud 来执行此检查,您可以使用以下命令:

$url = 'YOUR CDN URL';
$code = FALSE;
$options['http'] = array(
    'method' => "HEAD",
    'ignore_errors' => 1,
    'max_redirects' => 0
);
$body = file_get_contents($url, NULL, stream_context_create($options));
sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);


if($code!='200') {
    echo 'failed';
} else {
    echo 'exists';
}

If you do not want to import opencloud to perform this check you can use the following:

$url = 'YOUR CDN URL';
$code = FALSE;
$options['http'] = array(
    'method' => "HEAD",
    'ignore_errors' => 1,
    'max_redirects' => 0
);
$body = file_get_contents($url, NULL, stream_context_create($options));
sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);


if($code!='200') {
    echo 'failed';
} else {
    echo 'exists';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文