简体   繁体   English

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

[英]check if object exists in Cloud Files (PHP API)

I've just started working with the PHP API for Rackspace Cloud Files.我刚刚开始为 Rackspace 云文件使用 PHP API。 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:我的应用程序尝试使用以下内容通过其键(API 语言中的“名称”)获取现有缓存的 object:

$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 .问题是,如果 object 不存在,则 API 会引发致命错误,而不是简单地返回false The "right" way to do this by the API would probably be to do a API 执行此操作的“正确”方法可能是执行

$objs = $this->container->list_objects();

and then check for my $key value in that list.然后在该列表中检查我的$key值。 However, this seems way more time/CPU intensive than just returning false from the get_object request.但是,这似乎比仅从get_object请求返回false更耗费时间/CPU。

Is there a way to do a "search for object" or "check if object exists" in Cloud Files?有没有办法在云文件中进行“搜索对象”或“检查 object 是否存在”?

Thanks谢谢

I sent them a pull request and hope it'll get included.我向他们发送了一个拉取请求,并希望它会被包括在内。

https://github.com/rackspace/php-cloudfiles/pull/35 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.";
}

I have more general way to check if object exists:我有更通用的方法来检查 object 是否存在:

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

If you dump the $object, you'll see that content_length is zero.如果您转储 $object,您将看到 content_length 为零。 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.还有,在倾倒的父 object 的深处,一个 404 会返回,但它是私有的,所以你需要一些 hackin 才能得到它。

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你会在里面看到 object 的 response_status 是 404

[response_status:CF_Http:private] => 404

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.我知道我参加聚会有点晚了,但希望这对将来的某人有所帮助:您可以使用objectExists()方法来测试 object 是否可用。

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);

If you do not want to import opencloud to perform this check you can use the following:如果您不想导入 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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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